Set type to NOTUSED if lookup fail.
[ira/wip.git] / source3 / nsswitch / wbinfo.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.0
4
5    Winbind status program.
6
7    Copyright (C) Tim Potter 2000
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "winbindd.h"
26 #include "debug.h"
27
28 /* Prototypes from common.h */
29
30 NSS_STATUS winbindd_request(int req_type, 
31                             struct winbindd_request *request,
32                             struct winbindd_response *response);
33
34 /* List groups a user is a member of */
35
36 static BOOL wbinfo_get_usergroups(char *user)
37 {
38         struct winbindd_request request;
39         struct winbindd_response response;
40         NSS_STATUS result;
41         int i;
42         
43         ZERO_STRUCT(response);
44
45         /* Send request */
46
47         fstrcpy(request.data.username, user);
48
49         result = winbindd_request(WINBINDD_GETGROUPS, &request, &response);
50
51         if (result != NSS_STATUS_SUCCESS) {
52                 return False;
53         }
54
55         for (i = 0; i < response.data.num_entries; i++) {
56                 printf("%d\n", (int)((gid_t *)response.extra_data)[i]);
57         }
58
59         return True;
60 }
61
62 /* List trusted domains */
63
64 static BOOL wbinfo_list_domains(void)
65 {
66         struct winbindd_response response;
67         fstring name;
68
69         ZERO_STRUCT(response);
70
71         /* Send request */
72
73         if (winbindd_request(WINBINDD_LIST_TRUSTDOM, NULL, &response) !=
74             NSS_STATUS_SUCCESS) {
75                 return False;
76         }
77
78         /* Display response */
79
80         if (response.extra_data) {
81                 while(next_token((char **)&response.extra_data, name, ",", 
82                                  sizeof(fstring))) {
83                         printf("%s\n", name);
84                 }
85         }
86
87         return True;
88 }
89
90 /* Check trust account password */
91
92 static BOOL wbinfo_check_secret(void)
93 {
94         struct winbindd_response response;
95         BOOL result;
96
97         ZERO_STRUCT(response);
98
99         result = winbindd_request(WINBINDD_CHECK_MACHACC, NULL, &response) ==
100                 NSS_STATUS_SUCCESS;
101
102         if (result) {
103
104                 if (response.data.num_entries == 0) {
105                         printf("Secret is good\n");
106                 } else {
107                         printf("Secret is bad\n0x%08x\n", 
108                                response.data.num_entries);
109                 }
110
111                 return True;
112         }
113
114         return False;
115 }
116
117 /* Convert uid to sid */
118
119 static BOOL wbinfo_uid_to_sid(uid_t uid)
120 {
121         struct winbindd_request request;
122         struct winbindd_response response;
123
124         ZERO_STRUCT(request);
125         ZERO_STRUCT(response);
126
127         /* Send request */
128
129         request.data.uid = uid;
130         if (winbindd_request(WINBINDD_UID_TO_SID, &request, &response) !=
131             NSS_STATUS_SUCCESS) {
132                 return False;
133         }
134
135         /* Display response */
136
137         printf("%s\n", response.data.sid.sid);
138
139         return True;
140 }
141
142 /* Convert gid to sid */
143
144 static BOOL wbinfo_gid_to_sid(gid_t gid)
145 {
146         struct winbindd_request request;
147         struct winbindd_response response;
148
149         ZERO_STRUCT(request);
150         ZERO_STRUCT(response);
151
152         /* Send request */
153
154         request.data.gid = gid;
155         if (winbindd_request(WINBINDD_GID_TO_SID, &request, &response) !=
156             NSS_STATUS_SUCCESS) {
157                 return False;
158         }
159
160         /* Display response */
161
162         printf("%s\n", response.data.sid.sid);
163
164         return True;
165 }
166
167 /* Convert sid to uid */
168
169 static BOOL wbinfo_sid_to_uid(char *sid)
170 {
171         struct winbindd_request request;
172         struct winbindd_response response;
173
174         ZERO_STRUCT(request);
175         ZERO_STRUCT(response);
176
177         /* Send request */
178
179         fstrcpy(request.data.sid, sid);
180         if (winbindd_request(WINBINDD_SID_TO_UID, &request, &response) !=
181             NSS_STATUS_SUCCESS) {
182                 return False;
183         }
184
185         /* Display response */
186
187         printf("%d\n", (int)response.data.uid);
188
189         return True;
190 }
191
192 static BOOL wbinfo_sid_to_gid(char *sid)
193 {
194         struct winbindd_request request;
195         struct winbindd_response response;
196
197         ZERO_STRUCT(request);
198         ZERO_STRUCT(response);
199
200         /* Send request */
201
202         fstrcpy(request.data.sid, sid);
203         if (winbindd_request(WINBINDD_SID_TO_GID, &request, &response) !=
204             NSS_STATUS_SUCCESS) {
205                 return False;
206         }
207
208         /* Display response */
209
210         printf("%d\n", (int)response.data.gid);
211
212         return True;
213 }
214
215 /* Convert sid to string */
216
217 static BOOL wbinfo_lookupsid(char *sid)
218 {
219         struct winbindd_request request;
220         struct winbindd_response response;
221
222         ZERO_STRUCT(request);
223         ZERO_STRUCT(response);
224
225         /* Send off request */
226
227         fstrcpy(request.data.sid, sid);
228         if (winbindd_request(WINBINDD_LOOKUPSID, &request, &response) !=
229             NSS_STATUS_SUCCESS) {
230                 return False;
231         }
232
233         /* Display response */
234
235         printf("%s %d\n", response.data.name.name, response.data.name.type);
236
237         return True;
238 }
239
240 /* Convert string to sid */
241
242 static BOOL wbinfo_lookupname(char *name)
243 {
244         struct winbindd_request request;
245         struct winbindd_response response;
246
247         /*
248          * Don't do the lookup if the name has no separator.
249          */
250  
251         if (!strchr(name, *lp_winbind_separator()))
252                 return False;
253
254         /* Send off request */
255
256         ZERO_STRUCT(request);
257         ZERO_STRUCT(response);
258
259         fstrcpy(request.data.name, name);
260         if (winbindd_request(WINBINDD_LOOKUPNAME, &request, &response) !=
261             NSS_STATUS_SUCCESS) {
262                 return False;
263         }
264
265         /* Display response */
266
267         printf("%s %d\n", response.data.sid.sid, response.data.sid.type);
268
269         return True;
270 }
271
272 /* Authenticate a user with a plaintext password */
273
274 static BOOL wbinfo_auth(char *username)
275 {
276         struct winbindd_request request;
277         struct winbindd_response response;
278         NSS_STATUS result;
279         char *p;
280
281         /*
282          * Don't do the lookup if the name has no separator.
283          */
284  
285         if (!strchr(username, *lp_winbind_separator()))
286                 return False;
287
288         /* Send off request */
289
290         ZERO_STRUCT(request);
291         ZERO_STRUCT(response);
292
293         p = strchr(username, '%');
294
295         if (p) {
296                 *p = 0;
297                 fstrcpy(request.data.auth.user, username);
298                 fstrcpy(request.data.auth.pass, p + 1);
299                 *p = '%';
300         } else
301                 fstrcpy(request.data.auth.user, username);
302
303         result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
304
305         /* Display response */
306
307         printf("plaintext password authentication %s\n", 
308                (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
309
310         return result == NSS_STATUS_SUCCESS;
311 }
312
313 /* Authenticate a user with a challenge/response */
314
315 static BOOL wbinfo_auth_crap(char *username)
316 {
317         struct winbindd_request request;
318         struct winbindd_response response;
319         NSS_STATUS result;
320         fstring pass;
321         char *p;
322
323         /*
324          * Don't do the lookup if the name has no separator.
325          */
326  
327         if (!strchr(username, *lp_winbind_separator()))
328                 return False;
329
330         /* Send off request */
331
332         ZERO_STRUCT(request);
333         ZERO_STRUCT(response);
334
335         p = strchr(username, '%');
336
337         if (p) {
338                 *p = 0;
339                 fstrcpy(request.data.auth_crap.user, username);
340                 fstrcpy(pass, p + 1);
341                 *p = '%';
342         } else
343                 fstrcpy(request.data.auth_crap.user, username);
344
345         generate_random_buffer(request.data.auth_crap.chal, 8, False);
346         
347         SMBencrypt((uchar *)pass, request.data.auth_crap.chal, 
348                    (uchar *)request.data.auth_crap.lm_resp);
349         SMBNTencrypt((uchar *)pass, request.data.auth_crap.chal,
350                      (uchar *)request.data.auth_crap.nt_resp);
351
352         request.data.auth_crap.lm_resp_len = 24;
353         request.data.auth_crap.nt_resp_len = 24;
354
355         result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
356
357         /* Display response */
358
359         printf("challenge/response password authentication %s\n", 
360                (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
361
362         return result == NSS_STATUS_SUCCESS;
363 }
364
365 /* Print domain users */
366
367 static BOOL print_domain_users(void)
368 {
369         struct winbindd_response response;
370         fstring name;
371
372         /* Send request to winbind daemon */
373
374         ZERO_STRUCT(response);
375
376         if (winbindd_request(WINBINDD_LIST_USERS, NULL, &response) !=
377             NSS_STATUS_SUCCESS) {
378                 return False;
379         }
380
381         /* Look through extra data */
382
383         if (!response.extra_data) {
384                 return False;
385         }
386
387         while(next_token((char **)&response.extra_data, name, ",", 
388                          sizeof(fstring))) {
389                 printf("%s\n", name);
390         }
391         
392         return True;
393 }
394
395 /* Print domain groups */
396
397 static BOOL print_domain_groups(void)
398 {
399         struct winbindd_response response;
400         fstring name;
401
402         ZERO_STRUCT(response);
403
404         if (winbindd_request(WINBINDD_LIST_GROUPS, NULL, &response) !=
405             NSS_STATUS_SUCCESS) {
406                 return False;
407         }
408
409         /* Look through extra data */
410
411         if (!response.extra_data) {
412                 return False;
413         }
414
415         while(next_token((char **)&response.extra_data, name, ",", 
416                          sizeof(fstring))) {
417                 printf("%s\n", name);
418         }
419         
420         return True;
421 }
422
423 /* Print program usage */
424
425 static void usage(void)
426 {
427         printf("Usage: wbinfo -ug | -n name | -sSY sid | -UG uid/gid | -tm "
428                "| -a user%%password\n");
429         printf("\t-u\t\t\tlists all domain users\n");
430         printf("\t-g\t\t\tlists all domain groups\n");
431         printf("\t-n name\t\t\tconverts name to sid\n");
432         printf("\t-s sid\t\t\tconverts sid to name\n");
433         printf("\t-U uid\t\t\tconverts uid to sid\n");
434         printf("\t-G gid\t\t\tconverts gid to sid\n");
435         printf("\t-S sid\t\t\tconverts sid to uid\n");
436         printf("\t-Y sid\t\t\tconverts sid to gid\n");
437         printf("\t-t\t\t\tcheck shared secret\n");
438         printf("\t-m\t\t\tlist trusted domains\n");
439         printf("\t-r user\t\t\tget user groups\n");
440         printf("\t-a user%%password\tauthenticate user\n");
441 }
442
443 /* Main program */
444
445 int main(int argc, char **argv)
446 {
447         extern pstring global_myname;
448         int opt;
449
450         /* Samba client initialisation */
451
452         if (!*global_myname) {
453                 char *p;
454
455                 fstrcpy(global_myname, myhostname());
456                 p = strchr(global_myname, '.');
457                 if (p) {
458                         *p = 0;
459                 }
460         }
461
462         if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
463                 DEBUG(0, ("error opening config file\n"));
464                 exit(1);
465         }
466         
467         load_interfaces();
468
469         /* Parse command line options */
470
471         if (argc == 1) {
472                 usage();
473                 return 1;
474         }
475
476         while ((opt = getopt(argc, argv, "ugs:n:U:G:S:Y:tmr:a:")) != EOF) {
477                 switch (opt) {
478                 case 'u':
479                         if (!print_domain_users()) {
480                                 printf("Error looking up domain users\n");
481                                 return 1;
482                         }
483                         break;
484                 case 'g':
485                         if (!print_domain_groups()) {
486                                 printf("Error looking up domain groups\n");
487                                 return 1;
488                         }
489                         break;
490                 case 's':
491                         if (!wbinfo_lookupsid(optarg)) {
492                                 printf("Could not lookup sid %s\n", optarg);
493                                 return 1;
494                         }
495                         break;
496                 case 'n':
497                         if (!wbinfo_lookupname(optarg)) {
498                                 printf("Could not lookup name %s\n", optarg);
499                                 return 1;
500                         }
501                         break;
502                 case 'U':
503                         if (!wbinfo_uid_to_sid(atoi(optarg))) {
504                                 printf("Could not convert uid %s to sid\n",
505                                        optarg);
506                                 return 1;
507                         }
508                         break;
509                 case 'G':
510                         if (!wbinfo_gid_to_sid(atoi(optarg))) {
511                                 printf("Could not convert gid %s to sid\n",
512                                        optarg);
513                                 return 1;
514                         }
515                         break;
516                 case 'S':
517                         if (!wbinfo_sid_to_uid(optarg)) {
518                                 printf("Could not convert sid %s to uid\n",
519                                        optarg);
520                                 return 1;
521                         }
522                         break;
523                 case 'Y':
524                         if (!wbinfo_sid_to_gid(optarg)) {
525                                 printf("Could not convert sid %s to gid\n",
526                                        optarg);
527                                 return 1;
528                         }
529                         break;
530                 case 't':
531                         if (!wbinfo_check_secret()) {
532                                 printf("Could not check secret\n");
533                                 return 1;
534                         }
535                         break;
536                 case 'm':
537                         if (!wbinfo_list_domains()) {
538                                 printf("Could not list trusted domains\n");
539                                 return 1;
540                         }
541                         break;
542                 case 'r':
543                         if (!wbinfo_get_usergroups(optarg)) {
544                                 printf("Could not get groups for user %s\n", 
545                                        optarg);
546                                 return 1;
547                         }
548                         break;
549                 case 'a': {
550                         BOOL got_error = False;
551
552                         if (!wbinfo_auth(optarg)) {
553                                 printf("Could not authenticate user %s with "
554                                        "plaintext password\n", optarg);
555                                 got_error = True;
556                         }
557
558                         if (!wbinfo_auth_crap(optarg)) {
559                                 printf("Could not authenticate user %s with "
560                                        "challenge/response\n", optarg);
561                                 got_error = True;
562                         }
563
564                         if (got_error)
565                                 return 1;
566                         break;
567                                 
568                 }
569                       /* Invalid option */
570
571                 default:
572                         usage();
573                         return 1;
574                 }
575         }
576         
577         /* Clean exit */
578
579         return 0;
580 }