Removed version number from file header.
[ira/wip.git] / source3 / 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 /* Check trust account password */
161
162 static BOOL wbinfo_check_secret(void)
163 {
164         struct winbindd_response response;
165         BOOL result;
166
167         ZERO_STRUCT(response);
168
169         result = winbindd_request(WINBINDD_CHECK_MACHACC, NULL, &response) ==
170                 NSS_STATUS_SUCCESS;
171
172         if (result) {
173
174                 if (response.data.num_entries == 0) {
175                         printf("Secret is good\n");
176                 } else {
177                         printf("Secret is bad\n0x%08x\n", 
178                                response.data.num_entries);
179                 }
180
181                 return True;
182         }
183
184         return False;
185 }
186
187 /* Convert uid to sid */
188
189 static BOOL wbinfo_uid_to_sid(uid_t uid)
190 {
191         struct winbindd_request request;
192         struct winbindd_response response;
193
194         ZERO_STRUCT(request);
195         ZERO_STRUCT(response);
196
197         /* Send request */
198
199         request.data.uid = uid;
200         if (winbindd_request(WINBINDD_UID_TO_SID, &request, &response) !=
201             NSS_STATUS_SUCCESS) {
202                 return False;
203         }
204
205         /* Display response */
206
207         printf("%s\n", response.data.sid.sid);
208
209         return True;
210 }
211
212 /* Convert gid to sid */
213
214 static BOOL wbinfo_gid_to_sid(gid_t gid)
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.gid = gid;
225         if (winbindd_request(WINBINDD_GID_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 sid to uid */
238
239 static BOOL wbinfo_sid_to_uid(char *sid)
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         fstrcpy(request.data.sid, sid);
250         if (winbindd_request(WINBINDD_SID_TO_UID, &request, &response) !=
251             NSS_STATUS_SUCCESS) {
252                 return False;
253         }
254
255         /* Display response */
256
257         printf("%d\n", (int)response.data.uid);
258
259         return True;
260 }
261
262 static BOOL wbinfo_sid_to_gid(char *sid)
263 {
264         struct winbindd_request request;
265         struct winbindd_response response;
266
267         ZERO_STRUCT(request);
268         ZERO_STRUCT(response);
269
270         /* Send request */
271
272         fstrcpy(request.data.sid, sid);
273         if (winbindd_request(WINBINDD_SID_TO_GID, &request, &response) !=
274             NSS_STATUS_SUCCESS) {
275                 return False;
276         }
277
278         /* Display response */
279
280         printf("%d\n", (int)response.data.gid);
281
282         return True;
283 }
284
285 /* Convert sid to string */
286
287 static BOOL wbinfo_lookupsid(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 off request */
296
297         fstrcpy(request.data.sid, sid);
298         if (winbindd_request(WINBINDD_LOOKUPSID, &request, &response) !=
299             NSS_STATUS_SUCCESS) {
300                 return False;
301         }
302
303         /* Display response */
304
305         printf("[%s]\\[%s] %d\n", response.data.name.dom_name, response.data.name.name, response.data.name.type);
306
307         return True;
308 }
309
310 /* Convert string to sid */
311
312 static BOOL wbinfo_lookupname(char *name)
313 {
314         struct winbindd_request request;
315         struct winbindd_response response;
316
317         /* Send off request */
318
319         ZERO_STRUCT(request);
320         ZERO_STRUCT(response);
321
322         parse_wbinfo_domain_user(name, request.data.name.dom_name, request.data.name.name);
323
324         if (winbindd_request(WINBINDD_LOOKUPNAME, &request, &response) !=
325             NSS_STATUS_SUCCESS) {
326                 return False;
327         }
328
329         /* Display response */
330
331         printf("%s %d\n", response.data.sid.sid, response.data.sid.type);
332
333         return True;
334 }
335
336 /* Authenticate a user with a plaintext password */
337
338 static BOOL wbinfo_auth(char *username)
339 {
340         struct winbindd_request request;
341         struct winbindd_response response;
342         NSS_STATUS result;
343         char *p;
344
345         /* Send off request */
346
347         ZERO_STRUCT(request);
348         ZERO_STRUCT(response);
349
350         p = strchr(username, '%');
351
352         if (p) {
353                 *p = 0;
354                 fstrcpy(request.data.auth.user, username);
355                 fstrcpy(request.data.auth.pass, p + 1);
356                 *p = '%';
357         } else
358                 fstrcpy(request.data.auth.user, username);
359
360         result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
361
362         /* Display response */
363
364         printf("plaintext password authentication %s\n", 
365                (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
366
367         return result == NSS_STATUS_SUCCESS;
368 }
369
370 /* Authenticate a user with a challenge/response */
371
372 static BOOL wbinfo_auth_crap(char *username)
373 {
374         struct winbindd_request request;
375         struct winbindd_response response;
376         NSS_STATUS result;
377         fstring name_user;
378         fstring name_domain;
379         fstring pass;
380         char *p;
381
382         /* Send off request */
383
384         ZERO_STRUCT(request);
385         ZERO_STRUCT(response);
386
387         p = strchr(username, '%');
388
389         if (p) {
390                 *p = 0;
391                 fstrcpy(pass, p + 1);
392         }
393                 
394         parse_wbinfo_domain_user(username, name_domain, name_user);
395
396         fstrcpy(request.data.auth_crap.user, name_user);
397
398         fstrcpy(request.data.auth_crap.domain, name_domain);
399
400         generate_random_buffer(request.data.auth_crap.chal, 8, False);
401         
402         SMBencrypt((uchar *)pass, request.data.auth_crap.chal, 
403                    (uchar *)request.data.auth_crap.lm_resp);
404         SMBNTencrypt((uchar *)pass, request.data.auth_crap.chal,
405                      (uchar *)request.data.auth_crap.nt_resp);
406
407         request.data.auth_crap.lm_resp_len = 24;
408         request.data.auth_crap.nt_resp_len = 24;
409
410         result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
411
412         /* Display response */
413
414         printf("challenge/response password authentication %s\n", 
415                (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
416
417         return result == NSS_STATUS_SUCCESS;
418 }
419
420 /* Print domain users */
421
422 static BOOL print_domain_users(void)
423 {
424         struct winbindd_response response;
425         char *extra_data;
426         fstring name;
427
428         /* Send request to winbind daemon */
429
430         ZERO_STRUCT(response);
431
432         if (winbindd_request(WINBINDD_LIST_USERS, NULL, &response) !=
433             NSS_STATUS_SUCCESS) {
434                 return False;
435         }
436
437         /* Look through extra data */
438
439         if (!response.extra_data)
440                 return False;
441
442         extra_data = (char *)response.extra_data;
443
444         while(next_token(&extra_data, name, ",", sizeof(fstring)))
445                 printf("%s\n", name);
446         
447         SAFE_FREE(response.extra_data);
448
449         return True;
450 }
451
452 /* Print domain groups */
453
454 static BOOL print_domain_groups(void)
455 {
456         struct winbindd_response response;
457         char *extra_data;
458         fstring name;
459
460         ZERO_STRUCT(response);
461
462         if (winbindd_request(WINBINDD_LIST_GROUPS, NULL, &response) !=
463             NSS_STATUS_SUCCESS) {
464                 return False;
465         }
466
467         /* Look through extra data */
468
469         if (!response.extra_data)
470                 return False;
471
472         extra_data = (char *)response.extra_data;
473
474         while(next_token(&extra_data, name, ",", sizeof(fstring)))
475                 printf("%s\n", name);
476
477         SAFE_FREE(response.extra_data);
478         
479         return True;
480 }
481
482 /* Set the authorised user for winbindd access in secrets.tdb */
483
484 static BOOL wbinfo_set_auth_user(char *username)
485 {
486         char *password;
487
488         /* Separate into user and password */
489
490         password = strchr(username, '%');
491
492         if (password) {
493                 *password = 0;
494                 password++;
495         } else
496                 password = "";
497
498         /* Store in secrets.tdb */
499
500         if (!secrets_store(SECRETS_AUTH_USER, username, strlen(username) + 1) ||
501             !secrets_store(SECRETS_AUTH_PASSWORD, password, strlen(password) + 1)) {
502                 fprintf(stderr, "error storing authenticated user info\n");
503                 return False;
504         }
505
506         return True;
507 }
508
509 static BOOL wbinfo_ping(void)
510 {
511         NSS_STATUS result;
512         
513         result = winbindd_request(WINBINDD_PING, NULL, NULL);
514
515         /* Display response */
516
517         printf("'ping' to winbindd %s\n", 
518                (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
519
520         return result == NSS_STATUS_SUCCESS;
521 }
522
523 /* Print program usage */
524
525 static void usage(void)
526 {
527         printf("Usage: wbinfo -ug | -n name | -sSY sid | -UG uid/gid | -tm "
528                "| -a user%%password\n");
529         printf("\t-u\t\t\tlists all domain users\n");
530         printf("\t-g\t\t\tlists all domain groups\n");
531         printf("\t-n name\t\t\tconverts name to sid\n");
532         printf("\t-s sid\t\t\tconverts sid to name\n");
533         printf("\t-U uid\t\t\tconverts uid to sid\n");
534         printf("\t-G gid\t\t\tconverts gid to sid\n");
535         printf("\t-S sid\t\t\tconverts sid to uid\n");
536         printf("\t-Y sid\t\t\tconverts sid to gid\n");
537         printf("\t-t\t\t\tcheck shared secret\n");
538         printf("\t-m\t\t\tlist trusted domains\n");
539         printf("\t-r user\t\t\tget user groups\n");
540         printf("\t-a user%%password\tauthenticate user\n");
541         printf("\t-p 'ping' winbindd to see if it is alive\n");
542 }
543
544 /* Main program */
545
546 enum {
547         OPT_SET_AUTH_USER = 1000
548 };
549
550 int main(int argc, char **argv)
551 {
552         extern pstring global_myname;
553         int opt;
554
555         poptContext pc;
556         static char *string_arg;
557         static int int_arg;
558         BOOL got_command = False;
559
560         struct poptOption long_options[] = {
561
562                 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
563                 { "help", 'h', POPT_ARG_NONE, 0, 'h' },
564                 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u' },
565                 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g' },
566                 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n' },
567                 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's' },
568                 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U' },
569                 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G' },
570                 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S' },
571                 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y' },
572                 { "check-secret", 't', POPT_ARG_NONE, 0, 't' },
573                 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm' },
574                 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r' },
575                 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a' },
576                 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER },
577                 { "ping", 'p', POPT_ARG_NONE, 0, 'p' },
578                 { 0, 0, 0, 0 }
579         };
580
581         /* Samba client initialisation */
582
583         if (!*global_myname) {
584                 char *p;
585
586                 fstrcpy(global_myname, myhostname());
587                 p = strchr(global_myname, '.');
588                 if (p) {
589                         *p = 0;
590                 }
591         }
592
593         if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
594                 fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
595                         dyn_CONFIGFILE, strerror(errno));
596                 exit(1);
597         }
598
599         load_interfaces();
600
601         /* Parse command line options */
602
603         if (argc == 1) {
604                 usage();
605                 return 1;
606         }
607
608         /* Parse options */
609
610         pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
611
612         while((opt = poptGetNextOpt(pc)) != -1) {
613                 if (got_command) {
614                         fprintf(stderr, "No more than one command may be specified "
615                                 "at once.\n");
616                         exit(1);
617                 }
618                 got_command = True;
619         }
620
621         pc = poptGetContext(NULL, argc, (const char **)argv, long_options, 
622                             POPT_CONTEXT_KEEP_FIRST);
623
624         while((opt = poptGetNextOpt(pc)) != -1) {
625                 switch (opt) {
626                 case 'h':
627                         usage();
628                         exit(0);
629                 case 'u':
630                         if (!print_domain_users()) {
631                                 printf("Error looking up domain users\n");
632                                 return 1;
633                         }
634                         break;
635                 case 'g':
636                         if (!print_domain_groups()) {
637                                 printf("Error looking up domain groups\n");
638                                 return 1;
639                         }
640                         break;
641                 case 's':
642                         if (!wbinfo_lookupsid(string_arg)) {
643                                 printf("Could not lookup sid %s\n", string_arg);
644                                 return 1;
645                         }
646                         break;
647                 case 'n':
648                         if (!wbinfo_lookupname(string_arg)) {
649                                 printf("Could not lookup name %s\n", string_arg);
650                                 return 1;
651                         }
652                         break;
653                 case 'U':
654                         if (!wbinfo_uid_to_sid(int_arg)) {
655                                 printf("Could not convert uid %d to sid\n", int_arg);
656                                 return 1;
657                         }
658                         break;
659                 case 'G':
660                         if (!wbinfo_gid_to_sid(int_arg)) {
661                                 printf("Could not convert gid %d to sid\n",
662                                        int_arg);
663                                 return 1;
664                         }
665                         break;
666                 case 'S':
667                         if (!wbinfo_sid_to_uid(string_arg)) {
668                                 printf("Could not convert sid %s to uid\n",
669                                        string_arg);
670                                 return 1;
671                         }
672                         break;
673                 case 'Y':
674                         if (!wbinfo_sid_to_gid(string_arg)) {
675                                 printf("Could not convert sid %s to gid\n",
676                                        string_arg);
677                                 return 1;
678                         }
679                         break;
680                 case 't':
681                         if (!wbinfo_check_secret()) {
682                                 printf("Could not check secret\n");
683                                 return 1;
684                         }
685                         break;
686                 case 'm':
687                         if (!wbinfo_list_domains()) {
688                                 printf("Could not list trusted domains\n");
689                                 return 1;
690                         }
691                         break;
692                 case 'r':
693                         if (!wbinfo_get_usergroups(string_arg)) {
694                                 printf("Could not get groups for user %s\n", 
695                                        string_arg);
696                                 return 1;
697                         }
698                         break;
699                 case 'a': {
700                         BOOL got_error = False;
701
702                         if (!wbinfo_auth(string_arg)) {
703                                 printf("Could not authenticate user %s with "
704                                        "plaintext password\n", string_arg);
705                                 got_error = True;
706                         }
707
708                         if (!wbinfo_auth_crap(string_arg)) {
709                                 printf("Could not authenticate user %s with "
710                                        "challenge/response\n", string_arg);
711                                 got_error = True;
712                         }
713                         
714                         if (got_error)
715                                 return 1;
716                         break;
717                 }
718                 case 'p': {
719
720                         if (!wbinfo_ping()) {
721                                 printf("could not ping winbindd!\n");
722                                 return 1;
723                         }
724                         break;
725                 }
726                 case OPT_SET_AUTH_USER:
727                         if (!(wbinfo_set_auth_user(string_arg))) {
728                                 return 1;
729                         }
730                         break;
731                 default:
732                         fprintf(stderr, "Invalid option\n");
733                         usage();
734                         return 1;
735                 }
736         }
737
738         /* Clean exit */
739
740         return 0;
741 }