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