Deal with systems that don't initialize birthtime correctly.
[ira/wip.git] / source3 / lib / netapi / examples / user / user_getinfo.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  NetUserGetInfo query
4  *  Copyright (C) Guenther Deschner 2008
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <sys/types.h>
21 #include <inttypes.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <netapi.h>
27
28 #include "common.h"
29
30 int main(int argc, const char **argv)
31 {
32         NET_API_STATUS status;
33         struct libnetapi_ctx *ctx = NULL;
34         const char *hostname = NULL;
35         const char *username = NULL;
36         uint8_t *buffer = NULL;
37         uint32_t level = 0;
38         char *sid_str = NULL;
39
40         struct USER_INFO_0 *u0;
41         struct USER_INFO_1 *u1;
42         struct USER_INFO_10 *u10;
43         struct USER_INFO_20 *u20;
44         struct USER_INFO_23 *u23;
45
46         poptContext pc;
47         int opt;
48
49         struct poptOption long_options[] = {
50                 POPT_AUTOHELP
51                 POPT_COMMON_LIBNETAPI_EXAMPLES
52                 POPT_TABLEEND
53         };
54
55         status = libnetapi_init(&ctx);
56         if (status != 0) {
57                 return status;
58         }
59
60         pc = poptGetContext("user_getinfo", argc, argv, long_options, 0);
61
62         poptSetOtherOptionHelp(pc, "hostname username level");
63         while((opt = poptGetNextOpt(pc)) != -1) {
64         }
65
66         if (!poptPeekArg(pc)) {
67                 poptPrintHelp(pc, stderr, 0);
68                 goto out;
69         }
70         hostname = poptGetArg(pc);
71
72         if (!poptPeekArg(pc)) {
73                 poptPrintHelp(pc, stderr, 0);
74                 goto out;
75         }
76         username = poptGetArg(pc);
77
78         if (poptPeekArg(pc)) {
79                 level = atoi(poptGetArg(pc));
80         }
81
82         /* NetUserGetInfo */
83
84         status = NetUserGetInfo(hostname,
85                                 username,
86                                 level,
87                                 &buffer);
88         if (status != 0) {
89                 printf("NetUserGetInfo failed with: %s\n",
90                         libnetapi_get_error_string(ctx, status));
91                 goto out;
92         }
93
94         switch (level) {
95                 case 0:
96                         u0 = (struct USER_INFO_0 *)buffer;
97                         printf("name: %s\n", u0->usri0_name);
98                         break;
99                 case 1:
100                         u1 = (struct USER_INFO_1 *)buffer;
101                         printf("name: %s\n", u1->usri1_name);
102                         printf("password: %s\n", u1->usri1_password);
103                         printf("password_age: %d\n", u1->usri1_password_age);
104                         printf("priv: %d\n", u1->usri1_priv);
105                         printf("homedir: %s\n", u1->usri1_home_dir);
106                         printf("comment: %s\n", u1->usri1_comment);
107                         printf("flags: 0x%08x\n", u1->usri1_flags);
108                         printf("script: %s\n", u1->usri1_script_path);
109                         break;
110                 case 10:
111                         u10 = (struct USER_INFO_10 *)buffer;
112                         printf("name: %s\n", u10->usri10_name);
113                         printf("comment: %s\n", u10->usri10_comment);
114                         printf("usr_comment: %s\n", u10->usri10_usr_comment);
115                         printf("full_name: %s\n", u10->usri10_full_name);
116                         break;
117                 case 20:
118                         u20 = (struct USER_INFO_20 *)buffer;
119                         printf("name: %s\n", u20->usri20_name);
120                         printf("comment: %s\n", u20->usri20_comment);
121                         printf("flags: 0x%08x\n", u20->usri20_flags);
122                         printf("rid: %d\n", u20->usri20_user_id);
123                         break;
124                 case 23:
125                         u23 = (struct USER_INFO_23 *)buffer;
126                         printf("name: %s\n", u23->usri23_name);
127                         printf("comment: %s\n", u23->usri23_comment);
128                         printf("flags: 0x%08x\n", u23->usri23_flags);
129                         if (ConvertSidToStringSid(u23->usri23_user_sid,
130                                                   &sid_str)) {
131                                 printf("user_sid: %s\n", sid_str);
132                                 free(sid_str);
133                         }
134                         break;
135                 default:
136                         break;
137         }
138
139  out:
140         libnetapi_free(ctx);
141         poptFreeContext(pc);
142
143         return status;
144 }