added 'wbinfo --sequence' to show sequence numbers of all domains
[sfrench/samba-autobuild/.git] / source / nsswitch / wbinfo.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Winbind status program.
5
6    Copyright (C) Tim Potter      2000
7    Copyright (C) Andrew Bartlett 2002
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 static char get_winbind_separator(void)
35 {
36         struct winbindd_response response;
37         char winbind_separator;
38
39         ZERO_STRUCT(response);
40
41         /* Send off request */
42
43         if (winbindd_request(WINBINDD_INFO, NULL, &response) !=
44             NSS_STATUS_SUCCESS) {
45                 printf("could not obtain winbind seperator!\n");
46                 exit(1);
47         }
48
49         winbind_separator = response.data.info.winbind_separator;
50
51         if (!winbind_separator) {
52                 printf("winbind separator was NULL!\n");
53                 exit(1);
54         }
55         
56         return winbind_separator;
57
58 }
59
60 static char *get_winbind_domain(void)
61 {
62         struct winbindd_response response;
63         static fstring winbind_domain;
64
65         ZERO_STRUCT(response);
66
67         /* Send off request */
68
69         if (winbindd_request(WINBINDD_DOMAIN_NAME, NULL, &response) !=
70             NSS_STATUS_SUCCESS) {
71                 printf("could not obtain winbind domain name!\n");
72                 exit(1);
73         }
74
75         fstrcpy(winbind_domain, response.data.domain_name);
76
77         return winbind_domain;
78
79 }
80
81 /* Copy of parse_domain_user from winbindd_util.c.  Parse a string of the
82    form DOMAIN/user into a domain and a user */
83
84 static BOOL parse_wbinfo_domain_user(const char *domuser, fstring domain, fstring user)
85 {
86
87         char *p = strchr(domuser,get_winbind_separator());
88
89         if (!p) {
90                 fstrcpy(user, domuser);
91                 fstrcpy(domain, get_winbind_domain());
92                 return True;
93         }
94         
95         fstrcpy(user, p+1);
96         fstrcpy(domain, domuser);
97         domain[PTR_DIFF(p, domuser)] = 0;
98         strupper(domain);
99         return True;
100 }
101
102 /* List groups a user is a member of */
103
104 static BOOL wbinfo_get_usergroups(char *user)
105 {
106         struct winbindd_request request;
107         struct winbindd_response response;
108         NSS_STATUS result;
109         int i;
110         
111         ZERO_STRUCT(response);
112
113         /* Send request */
114
115         fstrcpy(request.data.username, user);
116
117         result = winbindd_request(WINBINDD_GETGROUPS, &request, &response);
118
119         if (result != NSS_STATUS_SUCCESS)
120                 return False;
121
122         for (i = 0; i < response.data.num_entries; i++)
123                 printf("%d\n", (int)((gid_t *)response.extra_data)[i]);
124
125         SAFE_FREE(response.extra_data);
126
127         return True;
128 }
129
130 /* List trusted domains */
131
132 static BOOL wbinfo_list_domains(void)
133 {
134         struct winbindd_response response;
135         fstring name;
136
137         ZERO_STRUCT(response);
138
139         /* Send request */
140
141         if (winbindd_request(WINBINDD_LIST_TRUSTDOM, NULL, &response) !=
142             NSS_STATUS_SUCCESS) {
143                 return False;
144         }
145
146         /* Display response */
147
148         if (response.extra_data) {
149                 char *extra_data = (char *)response.extra_data;
150
151                 while(next_token(&extra_data, name, ",", sizeof(fstring)))
152                         printf("%s\n", name);
153
154                 SAFE_FREE(response.extra_data);
155         }
156
157         return True;
158 }
159
160
161 /* show sequence numbers */
162 static BOOL wbinfo_show_sequence(void)
163 {
164         struct winbindd_response response;
165         fstring name;
166
167         ZERO_STRUCT(response);
168
169         /* Send request */
170         if (winbindd_request(WINBINDD_SHOW_SEQUENCE, NULL, &response) !=
171             NSS_STATUS_SUCCESS) {
172                 return False;
173         }
174
175         /* Display response */
176         if (response.extra_data) {
177                 char *extra_data = (char *)response.extra_data;
178                 printf("%s", extra_data);
179                 SAFE_FREE(response.extra_data);
180         }
181
182         return True;
183 }
184
185 /* Check trust account password */
186
187 static BOOL wbinfo_check_secret(void)
188 {
189         struct winbindd_response response;
190         BOOL result;
191
192         ZERO_STRUCT(response);
193
194         result = winbindd_request(WINBINDD_CHECK_MACHACC, NULL, &response) ==
195                 NSS_STATUS_SUCCESS;
196
197         if (result) {
198
199                 if (response.data.num_entries == 0) {
200                         printf("Secret is good\n");
201                 } else {
202                         printf("Secret is bad\n0x%08x\n", 
203                                response.data.num_entries);
204                 }
205
206                 return True;
207         }
208
209         return False;
210 }
211
212 /* Convert uid to sid */
213
214 static BOOL wbinfo_uid_to_sid(uid_t uid)
215 {
216         struct winbindd_request request;
217         struct winbindd_response response;
218
219         ZERO_STRUCT(request);
220         ZERO_STRUCT(response);
221
222         /* Send request */
223
224         request.data.uid = uid;
225         if (winbindd_request(WINBINDD_UID_TO_SID, &request, &response) !=
226             NSS_STATUS_SUCCESS) {
227                 return False;
228         }
229
230         /* Display response */
231
232         printf("%s\n", response.data.sid.sid);
233
234         return True;
235 }
236
237 /* Convert gid to sid */
238
239 static BOOL wbinfo_gid_to_sid(gid_t gid)
240 {
241         struct winbindd_request request;
242         struct winbindd_response response;
243
244         ZERO_STRUCT(request);
245         ZERO_STRUCT(response);
246
247         /* Send request */
248
249         request.data.gid = gid;
250         if (winbindd_request(WINBINDD_GID_TO_SID, &request, &response) !=
251             NSS_STATUS_SUCCESS) {
252                 return False;
253         }
254
255         /* Display response */
256
257         printf("%s\n", response.data.sid.sid);
258
259         return True;
260 }
261
262 /* Convert sid to uid */
263
264 static BOOL wbinfo_sid_to_uid(char *sid)
265 {
266         struct winbindd_request request;
267         struct winbindd_response response;
268
269         ZERO_STRUCT(request);
270         ZERO_STRUCT(response);
271
272         /* Send request */
273
274         fstrcpy(request.data.sid, sid);
275         if (winbindd_request(WINBINDD_SID_TO_UID, &request, &response) !=
276             NSS_STATUS_SUCCESS) {
277                 return False;
278         }
279
280         /* Display response */
281
282         printf("%d\n", (int)response.data.uid);
283
284         return True;
285 }
286
287 static BOOL wbinfo_sid_to_gid(char *sid)
288 {
289         struct winbindd_request request;
290         struct winbindd_response response;
291
292         ZERO_STRUCT(request);
293         ZERO_STRUCT(response);
294
295         /* Send request */
296
297         fstrcpy(request.data.sid, sid);
298         if (winbindd_request(WINBINDD_SID_TO_GID, &request, &response) !=
299             NSS_STATUS_SUCCESS) {
300                 return False;
301         }
302
303         /* Display response */
304
305         printf("%d\n", (int)response.data.gid);
306
307         return True;
308 }
309
310 /* Convert sid to string */
311
312 static BOOL wbinfo_lookupsid(char *sid)
313 {
314         struct winbindd_request request;
315         struct winbindd_response response;
316
317         ZERO_STRUCT(request);
318         ZERO_STRUCT(response);
319
320         /* Send off request */
321
322         fstrcpy(request.data.sid, sid);
323         if (winbindd_request(WINBINDD_LOOKUPSID, &request, &response) !=
324             NSS_STATUS_SUCCESS) {
325                 return False;
326         }
327
328         /* Display response */
329
330         printf("[%s]\\[%s] %d\n", response.data.name.dom_name, response.data.name.name, response.data.name.type);
331
332         return True;
333 }
334
335 /* Convert string to sid */
336
337 static BOOL wbinfo_lookupname(char *name)
338 {
339         struct winbindd_request request;
340         struct winbindd_response response;
341
342         /* Send off request */
343
344         ZERO_STRUCT(request);
345         ZERO_STRUCT(response);
346
347         parse_wbinfo_domain_user(name, request.data.name.dom_name, request.data.name.name);
348
349         if (winbindd_request(WINBINDD_LOOKUPNAME, &request, &response) !=
350             NSS_STATUS_SUCCESS) {
351                 return False;
352         }
353
354         /* Display response */
355
356         printf("%s %d\n", response.data.sid.sid, response.data.sid.type);
357
358         return True;
359 }
360
361 /* Authenticate a user with a plaintext password */
362
363 static BOOL wbinfo_auth(char *username)
364 {
365         struct winbindd_request request;
366         struct winbindd_response response;
367         NSS_STATUS result;
368         char *p;
369
370         /* Send off request */
371
372         ZERO_STRUCT(request);
373         ZERO_STRUCT(response);
374
375         p = strchr(username, '%');
376
377         if (p) {
378                 *p = 0;
379                 fstrcpy(request.data.auth.user, username);
380                 fstrcpy(request.data.auth.pass, p + 1);
381                 *p = '%';
382         } else
383                 fstrcpy(request.data.auth.user, username);
384
385         result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
386
387         /* Display response */
388
389         printf("plaintext password authentication %s\n", 
390                (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
391
392         return result == NSS_STATUS_SUCCESS;
393 }
394
395 /* Authenticate a user with a challenge/response */
396
397 static BOOL wbinfo_auth_crap(char *username)
398 {
399         struct winbindd_request request;
400         struct winbindd_response response;
401         NSS_STATUS result;
402         fstring name_user;
403         fstring name_domain;
404         fstring pass;
405         char *p;
406
407         /* Send off request */
408
409         ZERO_STRUCT(request);
410         ZERO_STRUCT(response);
411
412         p = strchr(username, '%');
413
414         if (p) {
415                 *p = 0;
416                 fstrcpy(pass, p + 1);
417         }
418                 
419         parse_wbinfo_domain_user(username, name_domain, name_user);
420
421         fstrcpy(request.data.auth_crap.user, name_user);
422
423         fstrcpy(request.data.auth_crap.domain, name_domain);
424
425         generate_random_buffer(request.data.auth_crap.chal, 8, False);
426         
427         SMBencrypt((uchar *)pass, request.data.auth_crap.chal, 
428                    (uchar *)request.data.auth_crap.lm_resp);
429         SMBNTencrypt((uchar *)pass, request.data.auth_crap.chal,
430                      (uchar *)request.data.auth_crap.nt_resp);
431
432         request.data.auth_crap.lm_resp_len = 24;
433         request.data.auth_crap.nt_resp_len = 24;
434
435         result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
436
437         /* Display response */
438
439         printf("challenge/response password authentication %s\n", 
440                (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
441
442         return result == NSS_STATUS_SUCCESS;
443 }
444
445 /* Print domain users */
446
447 static BOOL print_domain_users(void)
448 {
449         struct winbindd_response response;
450         char *extra_data;
451         fstring name;
452
453         /* Send request to winbind daemon */
454
455         ZERO_STRUCT(response);
456
457         if (winbindd_request(WINBINDD_LIST_USERS, NULL, &response) !=
458             NSS_STATUS_SUCCESS) {
459                 return False;
460         }
461
462         /* Look through extra data */
463
464         if (!response.extra_data)
465                 return False;
466
467         extra_data = (char *)response.extra_data;
468
469         while(next_token(&extra_data, name, ",", sizeof(fstring)))
470                 printf("%s\n", name);
471         
472         SAFE_FREE(response.extra_data);
473
474         return True;
475 }
476
477 /* Print domain groups */
478
479 static BOOL print_domain_groups(void)
480 {
481         struct winbindd_response response;
482         char *extra_data;
483         fstring name;
484
485         ZERO_STRUCT(response);
486
487         if (winbindd_request(WINBINDD_LIST_GROUPS, NULL, &response) !=
488             NSS_STATUS_SUCCESS) {
489                 return False;
490         }
491
492         /* Look through extra data */
493
494         if (!response.extra_data)
495                 return False;
496
497         extra_data = (char *)response.extra_data;
498
499         while(next_token(&extra_data, name, ",", sizeof(fstring)))
500                 printf("%s\n", name);
501
502         SAFE_FREE(response.extra_data);
503         
504         return True;
505 }
506
507 /* Set the authorised user for winbindd access in secrets.tdb */
508
509 static BOOL wbinfo_set_auth_user(char *username)
510 {
511         char *password;
512
513         /* Separate into user and password */
514
515         password = strchr(username, '%');
516
517         if (password) {
518                 *password = 0;
519                 password++;
520         } else
521                 password = "";
522
523         /* Store in secrets.tdb */
524
525         if (!secrets_store(SECRETS_AUTH_USER, username, strlen(username) + 1) ||
526             !secrets_store(SECRETS_AUTH_PASSWORD, password, strlen(password) + 1)) {
527                 fprintf(stderr, "error storing authenticated user info\n");
528                 return False;
529         }
530
531         return True;
532 }
533
534 static BOOL wbinfo_ping(void)
535 {
536         NSS_STATUS result;
537         
538         result = winbindd_request(WINBINDD_PING, NULL, NULL);
539
540         /* Display response */
541
542         printf("'ping' to winbindd %s\n", 
543                (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
544
545         return result == NSS_STATUS_SUCCESS;
546 }
547
548 /* Print program usage */
549
550 static void usage(void)
551 {
552         printf("Usage: wbinfo -ug | -n name | -sSY sid | -UG uid/gid | -tm "
553                "| -a user%%password\n");
554         printf("\t-u\t\t\tlists all domain users\n");
555         printf("\t-g\t\t\tlists all domain groups\n");
556         printf("\t-n name\t\t\tconverts name to sid\n");
557         printf("\t-s sid\t\t\tconverts sid to name\n");
558         printf("\t-U uid\t\t\tconverts uid to sid\n");
559         printf("\t-G gid\t\t\tconverts gid to sid\n");
560         printf("\t-S sid\t\t\tconverts sid to uid\n");
561         printf("\t-Y sid\t\t\tconverts sid to gid\n");
562         printf("\t-t\t\t\tcheck shared secret\n");
563         printf("\t-m\t\t\tlist trusted domains\n");
564         printf("\t-r user\t\t\tget user groups\n");
565         printf("\t-a user%%password\tauthenticate user\n");
566         printf("\t-p 'ping' winbindd to see if it is alive\n");
567         printf("\t--sequence\t\tshow sequence numbers of all domains\n");
568 }
569
570 /* Main program */
571
572 enum {
573         OPT_SET_AUTH_USER = 1000
574 };
575
576 int main(int argc, char **argv)
577 {
578         extern pstring global_myname;
579         int opt;
580
581         poptContext pc;
582         static char *string_arg;
583         static int int_arg;
584         BOOL got_command = False;
585         enum {OPT_SEQUENCE=1};
586
587         struct poptOption long_options[] = {
588
589                 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
590                 { "help", 'h', POPT_ARG_NONE, 0, 'h' },
591                 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u' },
592                 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g' },
593                 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n' },
594                 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's' },
595                 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U' },
596                 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G' },
597                 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S' },
598                 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y' },
599                 { "check-secret", 't', POPT_ARG_NONE, 0, 't' },
600                 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm' },
601                 { "sequence", OPT_SEQUENCE, POPT_ARG_NONE, 0, OPT_SEQUENCE },
602                 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r' },
603                 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a' },
604                 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER },
605                 { "ping", 'p', POPT_ARG_NONE, 0, 'p' },
606                 { 0, 0, 0, 0 }
607         };
608
609         /* Samba client initialisation */
610
611         if (!*global_myname) {
612                 char *p;
613
614                 fstrcpy(global_myname, myhostname());
615                 p = strchr(global_myname, '.');
616                 if (p) {
617                         *p = 0;
618                 }
619         }
620
621         if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
622                 fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
623                         dyn_CONFIGFILE, strerror(errno));
624                 exit(1);
625         }
626
627         load_interfaces();
628
629         /* Parse command line options */
630
631         if (argc == 1) {
632                 usage();
633                 return 1;
634         }
635
636         /* Parse options */
637
638         pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
639
640         while((opt = poptGetNextOpt(pc)) != -1) {
641                 if (got_command) {
642                         fprintf(stderr, "No more than one command may be specified "
643                                 "at once.\n");
644                         exit(1);
645                 }
646                 got_command = True;
647         }
648
649         pc = poptGetContext(NULL, argc, (const char **)argv, long_options, 
650                             POPT_CONTEXT_KEEP_FIRST);
651
652         while((opt = poptGetNextOpt(pc)) != -1) {
653                 switch (opt) {
654                 case 'h':
655                         usage();
656                         exit(0);
657                 case 'u':
658                         if (!print_domain_users()) {
659                                 printf("Error looking up domain users\n");
660                                 return 1;
661                         }
662                         break;
663                 case 'g':
664                         if (!print_domain_groups()) {
665                                 printf("Error looking up domain groups\n");
666                                 return 1;
667                         }
668                         break;
669                 case 's':
670                         if (!wbinfo_lookupsid(string_arg)) {
671                                 printf("Could not lookup sid %s\n", string_arg);
672                                 return 1;
673                         }
674                         break;
675                 case 'n':
676                         if (!wbinfo_lookupname(string_arg)) {
677                                 printf("Could not lookup name %s\n", string_arg);
678                                 return 1;
679                         }
680                         break;
681                 case 'U':
682                         if (!wbinfo_uid_to_sid(int_arg)) {
683                                 printf("Could not convert uid %d to sid\n", int_arg);
684                                 return 1;
685                         }
686                         break;
687                 case 'G':
688                         if (!wbinfo_gid_to_sid(int_arg)) {
689                                 printf("Could not convert gid %d to sid\n",
690                                        int_arg);
691                                 return 1;
692                         }
693                         break;
694                 case 'S':
695                         if (!wbinfo_sid_to_uid(string_arg)) {
696                                 printf("Could not convert sid %s to uid\n",
697                                        string_arg);
698                                 return 1;
699                         }
700                         break;
701                 case 'Y':
702                         if (!wbinfo_sid_to_gid(string_arg)) {
703                                 printf("Could not convert sid %s to gid\n",
704                                        string_arg);
705                                 return 1;
706                         }
707                         break;
708                 case 't':
709                         if (!wbinfo_check_secret()) {
710                                 printf("Could not check secret\n");
711                                 return 1;
712                         }
713                         break;
714                 case 'm':
715                         if (!wbinfo_list_domains()) {
716                                 printf("Could not list trusted domains\n");
717                                 return 1;
718                         }
719                         break;
720                 case OPT_SEQUENCE:
721                         if (!wbinfo_show_sequence()) {
722                                 printf("Could not show sequence numbers\n");
723                                 return 1;
724                         }
725                         break;
726                 case 'r':
727                         if (!wbinfo_get_usergroups(string_arg)) {
728                                 printf("Could not get groups for user %s\n", 
729                                        string_arg);
730                                 return 1;
731                         }
732                         break;
733                 case 'a': {
734                         BOOL got_error = False;
735
736                         if (!wbinfo_auth(string_arg)) {
737                                 printf("Could not authenticate user %s with "
738                                        "plaintext password\n", string_arg);
739                                 got_error = True;
740                         }
741
742                         if (!wbinfo_auth_crap(string_arg)) {
743                                 printf("Could not authenticate user %s with "
744                                        "challenge/response\n", string_arg);
745                                 got_error = True;
746                         }
747                         
748                         if (got_error)
749                                 return 1;
750                         break;
751                 }
752                 case 'p': {
753
754                         if (!wbinfo_ping()) {
755                                 printf("could not ping winbindd!\n");
756                                 return 1;
757                         }
758                         break;
759                 }
760                 case OPT_SET_AUTH_USER:
761                         if (!(wbinfo_set_auth_user(string_arg))) {
762                                 return 1;
763                         }
764                         break;
765                 default:
766                         fprintf(stderr, "Invalid option\n");
767                         usage();
768                         return 1;
769                 }
770         }
771
772         /* Clean exit */
773
774         return 0;
775 }