Oops, the -a option disappeared.
[nivanova/samba-autobuild/.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 /* Set the authorised user for winbindd access in secrets.tdb */
424
425 static BOOL wbinfo_set_auth_user(char *username)
426 {
427         char *password;
428
429         /* Separate into user and password */
430
431         password = strchr(username, '%');
432
433         if (password) {
434                 *password = 0;
435                 password++;
436         } else
437                 password = "";
438
439         /* Store in secrets.tdb */
440
441         if (!secrets_store(SECRETS_AUTH_USER, username, strlen(username) + 1) ||
442             !secrets_store(SECRETS_AUTH_PASSWORD, password, strlen(password) + 1)) {
443                 fprintf(stderr, "error storing authenticated user info\n");
444                 return False;
445         }
446
447         return True;
448 }
449
450 /* Print program usage */
451
452 static void usage(void)
453 {
454         printf("Usage: wbinfo -ug | -n name | -sSY sid | -UG uid/gid | -tm "
455                "| -a user%%password\n");
456         printf("\t-u\t\t\tlists all domain users\n");
457         printf("\t-g\t\t\tlists all domain groups\n");
458         printf("\t-n name\t\t\tconverts name to sid\n");
459         printf("\t-s sid\t\t\tconverts sid to name\n");
460         printf("\t-U uid\t\t\tconverts uid to sid\n");
461         printf("\t-G gid\t\t\tconverts gid to sid\n");
462         printf("\t-S sid\t\t\tconverts sid to uid\n");
463         printf("\t-Y sid\t\t\tconverts sid to gid\n");
464         printf("\t-t\t\t\tcheck shared secret\n");
465         printf("\t-m\t\t\tlist trusted domains\n");
466         printf("\t-r user\t\t\tget user groups\n");
467         printf("\t-a user%%password\tauthenticate user\n");
468 }
469
470 /* Main program */
471
472 enum {
473         OPT_SET_AUTH_USER = 1000
474 };
475
476 int main(int argc, char **argv)
477 {
478         extern pstring global_myname;
479         int opt;
480
481         poptContext pc;
482         static char *string_arg;
483         static int int_arg;
484         BOOL got_command = False;
485
486         struct poptOption long_options[] = {
487
488                 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
489
490                 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u' },
491                 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g' },
492                 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n' },
493                 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's' },
494                 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U' },
495                 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G' },
496                 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S' },
497                 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y' },
498                 { "check-secret", 't', POPT_ARG_NONE, 0, 't' },
499                 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm' },
500                 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r' },
501                 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a' },
502                 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER },
503                 { 0, 0, 0, 0 }
504         };
505
506         /* Samba client initialisation */
507
508         if (!*global_myname) {
509                 char *p;
510
511                 fstrcpy(global_myname, myhostname());
512                 p = strchr(global_myname, '.');
513                 if (p) {
514                         *p = 0;
515                 }
516         }
517
518         if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
519                 fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
520                         dyn_CONFIGFILE, strerror(errno));
521                 exit(1);
522         }
523         
524         load_interfaces();
525
526         /* Parse command line options */
527
528         if (argc == 1) {
529                 usage();
530                 return 1;
531         }
532
533         /* Parse options */
534
535         pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
536
537         while((opt = poptGetNextOpt(pc)) != -1) {
538                 if (got_command) {
539                         fprintf(stderr, "No more than one command may be specified "
540                                 "at once.\n");
541                         exit(1);
542                 }
543                 got_command = True;
544         }
545
546         pc = poptGetContext(NULL, argc, (const char **)argv, long_options, 
547                             POPT_CONTEXT_KEEP_FIRST);
548
549         while((opt = poptGetNextOpt(pc)) != -1) {
550                 switch (opt) {
551                 case 'u':
552                         if (!print_domain_users()) {
553                                 printf("Error looking up domain users\n");
554                                 return 1;
555                         }
556                         break;
557                 case 'g':
558                         if (!print_domain_groups()) {
559                                 printf("Error looking up domain groups\n");
560                                 return 1;
561                         }
562                         break;
563                 case 's':
564                         if (!wbinfo_lookupsid(string_arg)) {
565                                 printf("Could not lookup sid %s\n", string_arg);
566                                 return 1;
567                         }
568                         break;
569                 case 'n':
570                         if (!wbinfo_lookupname(string_arg)) {
571                                 printf("Could not lookup name %s\n", string_arg);
572                                 return 1;
573                         }
574                         break;
575                 case 'U':
576                         if (!wbinfo_uid_to_sid(int_arg)) {
577                                 printf("Could not convert uid %d to sid\n", int_arg);
578                                 return 1;
579                         }
580                         break;
581                 case 'G':
582                         if (!wbinfo_gid_to_sid(int_arg)) {
583                                 printf("Could not convert gid %d to sid\n",
584                                        int_arg);
585                                 return 1;
586                         }
587                         break;
588                 case 'S':
589                         if (!wbinfo_sid_to_uid(string_arg)) {
590                                 printf("Could not convert sid %s to uid\n",
591                                        string_arg);
592                                 return 1;
593                         }
594                         break;
595                 case 'Y':
596                         if (!wbinfo_sid_to_gid(string_arg)) {
597                                 printf("Could not convert sid %s to gid\n",
598                                        string_arg);
599                                 return 1;
600                         }
601                         break;
602                 case 't':
603                         if (!wbinfo_check_secret()) {
604                                 printf("Could not check secret\n");
605                                 return 1;
606                         }
607                         break;
608                 case 'm':
609                         if (!wbinfo_list_domains()) {
610                                 printf("Could not list trusted domains\n");
611                                 return 1;
612                         }
613                         break;
614                 case 'r':
615                         if (!wbinfo_get_usergroups(string_arg)) {
616                                 printf("Could not get groups for user %s\n", 
617                                        string_arg);
618                                 return 1;
619                         }
620                         break;
621                 case 'a': {
622                         BOOL got_error = False;
623
624                         if (!wbinfo_auth(string_arg)) {
625                                 printf("Could not authenticate user %s with "
626                                        "plaintext password\n", string_arg);
627                                 got_error = True;
628                         }
629
630                         if (!wbinfo_auth_crap(string_arg)) {
631                                 printf("Could not authenticate user %s with "
632                                        "challenge/response\n", string_arg);
633                                 got_error = True;
634                         }
635                         
636                         if (got_error)
637                                 return 1;
638                         break;
639                 }
640                 case OPT_SET_AUTH_USER:
641                         if (!(wbinfo_set_auth_user(string_arg))) {
642                                 return 1;
643                         }
644                         break;
645                 default:
646                         fprintf(stderr, "Invalid option\n");
647                         return 1;
648                 }
649         }
650
651         /* Clean exit */
652
653         return 0;
654 }