r24958: This is the final text, and the final version. I'll send the release
[jelmer/samba4-debian.git] / source / nsswitch / wbinfo.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Winbind status program.
5
6    Copyright (C) Tim Potter      2000-2003
7    Copyright (C) Andrew Bartlett 2002-2007
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 3 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, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "pstring.h"
25 #include "winbind_nss_config.h"
26 #include "winbindd_nss.h"
27 #include "winbind_client.h"
28 #include "librpc/gen_ndr/ndr_netlogon.h"
29 #include "libcli/auth/libcli_auth.h"
30 #include "libcli/security/security.h"
31 #include "lib/cmdline/popt_common.h"
32 #include "dynconfig.h"
33
34 extern int winbindd_fd;
35
36 static char winbind_separator_int(BOOL strict)
37 {
38         struct winbindd_response response;
39         static BOOL got_sep;
40         static char sep;
41
42         if (got_sep)
43                 return sep;
44
45         ZERO_STRUCT(response);
46
47         /* Send off request */
48
49         if (winbindd_request(WINBINDD_INFO, NULL, &response) !=
50             NSS_STATUS_SUCCESS) {
51                 d_fprintf(stderr, "could not obtain winbind separator!\n");
52                 if (strict) {
53                         return 0;
54                 }
55                 /* HACK: (this module should not call lp_ funtions) */
56                 return *lp_winbind_separator();
57         }
58
59         sep = response.data.info.winbind_separator;
60         got_sep = True;
61
62         if (!sep) {
63                 d_fprintf(stderr, "winbind separator was NULL!\n");
64                 if (strict) {
65                         return 0;
66                 }
67                 /* HACK: (this module should not call lp_ funtions) */
68                 sep = *lp_winbind_separator();
69         }
70         
71         return sep;
72 }
73
74 static char winbind_separator(void)
75 {
76         return winbind_separator_int(False);
77 }
78
79 static const char *get_winbind_domain(void)
80 {
81         struct winbindd_response response;
82         static fstring winbind_domain;
83
84         ZERO_STRUCT(response);
85
86         /* Send off request */
87
88         if (winbindd_request(WINBINDD_DOMAIN_NAME, NULL, &response) !=
89             NSS_STATUS_SUCCESS) {
90                 d_fprintf(stderr, "could not obtain winbind domain name!\n");
91                 
92                 /* HACK: (this module should not call lp_ funtions) */
93                 return lp_workgroup();
94         }
95
96         fstrcpy(winbind_domain, response.data.domain_name);
97
98         return winbind_domain;
99
100 }
101
102 /* Copy of parse_domain_user from winbindd_util.c.  Parse a string of the
103    form DOMAIN/user into a domain and a user */
104
105 static BOOL parse_wbinfo_domain_user(const char *domuser, fstring domain, 
106                                      fstring user)
107 {
108
109         char *p = strchr(domuser,winbind_separator());
110
111         if (!p) {
112                 fstrcpy(user, domuser);
113                 fstrcpy(domain, get_winbind_domain());
114                 return True;
115         }
116         
117         fstrcpy(user, p+1);
118         fstrcpy(domain, domuser);
119         domain[PTR_DIFF(p, domuser)] = 0;
120         strupper_m(domain);
121
122         return True;
123 }
124
125 /* pull pwent info for a given user */
126
127 static BOOL wbinfo_get_userinfo(char *user)
128 {
129         struct winbindd_request request;
130         struct winbindd_response response;
131         NSS_STATUS result;
132         
133         ZERO_STRUCT(request);
134         ZERO_STRUCT(response);
135
136         /* Send request */
137         
138         fstrcpy(request.data.username, user);
139
140         result = winbindd_request(WINBINDD_GETPWNAM, &request, &response);
141         
142         if (result != NSS_STATUS_SUCCESS)
143                 return False;
144         
145         d_printf( "%s:%s:%d:%d:%s:%s:%s\n",
146                           response.data.pw.pw_name,
147                           response.data.pw.pw_passwd,
148                           response.data.pw.pw_uid,
149                           response.data.pw.pw_gid,
150                           response.data.pw.pw_gecos,
151                           response.data.pw.pw_dir,
152                           response.data.pw.pw_shell );
153         
154         return True;
155 }
156
157 /* pull pwent info for a given uid */
158 static BOOL wbinfo_get_uidinfo(int uid)
159 {
160         struct winbindd_request request;
161         struct winbindd_response response;
162         NSS_STATUS result;
163
164         ZERO_STRUCT(request);
165         ZERO_STRUCT(response);
166
167         request.data.uid = uid;
168
169         result = winbindd_request(WINBINDD_GETPWUID, &request, &response);
170
171         if (result != NSS_STATUS_SUCCESS)
172                 return False;
173
174         d_printf( "%s:%s:%d:%d:%s:%s:%s\n",
175                 response.data.pw.pw_name,
176                 response.data.pw.pw_passwd,
177                 response.data.pw.pw_uid,
178                 response.data.pw.pw_gid,
179                 response.data.pw.pw_gecos,
180                 response.data.pw.pw_dir,
181                 response.data.pw.pw_shell );
182
183         return True;
184 }
185
186 /* pull grent for a given group */
187 static BOOL wbinfo_get_groupinfo(char *group)
188 {
189         struct winbindd_request request;
190         struct winbindd_response response;
191         NSS_STATUS result;
192
193         ZERO_STRUCT(request);
194         ZERO_STRUCT(response);
195
196         /* Send request */
197
198         fstrcpy(request.data.groupname, group);
199
200         result = winbindd_request(WINBINDD_GETGRNAM, &request,
201                                   &response);
202
203         if ( result != NSS_STATUS_SUCCESS)
204                 return False;
205
206         d_printf( "%s:%s:%d\n",
207                   response.data.gr.gr_name,
208                   response.data.gr.gr_passwd,
209                   response.data.gr.gr_gid );
210         
211         return True;
212 }
213
214 /* List groups a user is a member of */
215
216 static BOOL wbinfo_get_usergroups(char *user)
217 {
218         struct winbindd_request request;
219         struct winbindd_response response;
220         NSS_STATUS result;
221         int i;
222         
223         ZERO_STRUCT(request);
224         ZERO_STRUCT(response);
225
226         /* Send request */
227
228         fstrcpy(request.data.username, user);
229
230         result = winbindd_request(WINBINDD_GETGROUPS, &request, &response);
231
232         if (result != NSS_STATUS_SUCCESS)
233                 return False;
234
235         for (i = 0; i < response.data.num_entries; i++)
236                 d_printf("%d\n", (int)((gid_t *)response.extra_data.data)[i]);
237
238         SAFE_FREE(response.extra_data.data);
239
240         return True;
241 }
242
243
244 /* List group SIDs a user SID is a member of */
245 static BOOL wbinfo_get_usersids(char *user_sid)
246 {
247         struct winbindd_request request;
248         struct winbindd_response response;
249         NSS_STATUS result;
250         int i;
251         const char *s;
252
253         ZERO_STRUCT(request);
254         ZERO_STRUCT(response);
255
256         /* Send request */
257         fstrcpy(request.data.sid, user_sid);
258
259         result = winbindd_request(WINBINDD_GETUSERSIDS, &request, &response);
260
261         if (result != NSS_STATUS_SUCCESS)
262                 return False;
263
264         s = (const char *)response.extra_data.data;
265         for (i = 0; i < response.data.num_entries; i++) {
266                 d_printf("%s\n", s);
267                 s += strlen(s) + 1;
268         }
269
270         SAFE_FREE(response.extra_data.data);
271
272         return True;
273 }
274
275 static BOOL wbinfo_get_userdomgroups(const char *user_sid)
276 {
277         struct winbindd_request request;
278         struct winbindd_response response;
279         NSS_STATUS result;
280
281         ZERO_STRUCT(request);
282         ZERO_STRUCT(response);
283
284         /* Send request */
285         fstrcpy(request.data.sid, user_sid);
286
287         result = winbindd_request(WINBINDD_GETUSERDOMGROUPS, &request,
288                                   &response);
289
290         if (result != NSS_STATUS_SUCCESS)
291                 return False;
292
293         if (response.data.num_entries != 0)
294                 printf("%s", (char *)response.extra_data.data);
295         
296         SAFE_FREE(response.extra_data.data);
297
298         return True;
299 }
300
301 /* Convert NetBIOS name to IP */
302
303 static BOOL wbinfo_wins_byname(char *name)
304 {
305         struct winbindd_request request;
306         struct winbindd_response response;
307
308         ZERO_STRUCT(request);
309         ZERO_STRUCT(response);
310
311         /* Send request */
312
313         fstrcpy(request.data.winsreq, name);
314
315         if (winbindd_request(WINBINDD_WINS_BYNAME, &request, &response) !=
316             NSS_STATUS_SUCCESS) {
317                 return False;
318         }
319
320         /* Display response */
321
322         d_printf("%s\n", response.data.winsresp);
323
324         return True;
325 }
326
327 /* Convert IP to NetBIOS name */
328
329 static BOOL wbinfo_wins_byip(char *ip)
330 {
331         struct winbindd_request request;
332         struct winbindd_response response;
333
334         ZERO_STRUCT(request);
335         ZERO_STRUCT(response);
336
337         /* Send request */
338
339         fstrcpy(request.data.winsreq, ip);
340
341         if (winbindd_request(WINBINDD_WINS_BYIP, &request, &response) !=
342             NSS_STATUS_SUCCESS) {
343                 return False;
344         }
345
346         /* Display response */
347
348         d_printf("%s\n", response.data.winsresp);
349
350         return True;
351 }
352
353 /* List trusted domains */
354
355 static BOOL wbinfo_list_domains(BOOL list_all_domains)
356 {
357         struct winbindd_request request;
358         struct winbindd_response response;
359
360         ZERO_STRUCT(request);
361         ZERO_STRUCT(response);
362
363         /* Send request */
364
365         request.data.list_all_domains = list_all_domains;
366
367         if (winbindd_request(WINBINDD_LIST_TRUSTDOM, &request, &response) !=
368             NSS_STATUS_SUCCESS)
369                 return False;
370
371         /* Display response */
372
373         if (response.extra_data.data) {
374                 const char *extra_data = (char *)response.extra_data.data;
375                 fstring name;
376                 char *p;
377
378                 while(next_token(&extra_data, name, "\n", sizeof(fstring))) {
379                         p = strchr(name, '\\');
380                         if (p == 0) {
381                                 d_fprintf(stderr, "Got invalid response: %s\n",
382                                          extra_data);
383                                 return False;
384                         }
385                         *p = 0;
386                         d_printf("%s\n", name);
387                 }
388
389                 SAFE_FREE(response.extra_data.data);
390         }
391
392         return True;
393 }
394
395 /* List own domain */
396
397 static BOOL wbinfo_list_own_domain(void)
398 {
399         d_printf("%s\n", get_winbind_domain());
400
401         return True;
402 }
403
404 /* show sequence numbers */
405 static BOOL wbinfo_show_sequence(const char *domain)
406 {
407         struct winbindd_request  request;
408         struct winbindd_response response;
409
410         ZERO_STRUCT(response);
411         ZERO_STRUCT(request);
412
413         if ( domain )
414                 fstrcpy( request.domain_name, domain );
415
416         /* Send request */
417
418         if (winbindd_request(WINBINDD_SHOW_SEQUENCE, &request, &response) !=
419             NSS_STATUS_SUCCESS)
420                 return False;
421
422         /* Display response */
423
424         if (response.extra_data.data) {
425                 char *extra_data = (char *)response.extra_data.data;
426                 d_printf("%s", extra_data);
427                 SAFE_FREE(response.extra_data.data);
428         }
429
430         return True;
431 }
432
433 /* Show domain info */
434
435 static BOOL wbinfo_domain_info(const char *domain_name)
436 {
437         struct winbindd_request request;
438         struct winbindd_response response;
439
440         ZERO_STRUCT(request);
441         ZERO_STRUCT(response);
442
443         if ((strequal(domain_name, ".")) || (domain_name[0] == '\0'))
444                 fstrcpy(request.domain_name, get_winbind_domain());
445         else
446                 fstrcpy(request.domain_name, domain_name);
447
448         /* Send request */
449
450         if (winbindd_request(WINBINDD_DOMAIN_INFO, &request, &response) !=
451             NSS_STATUS_SUCCESS)
452                 return False;
453
454         /* Display response */
455
456         d_printf("Name              : %s\n", response.data.domain_info.name);
457         d_printf("Alt_Name          : %s\n", response.data.domain_info.alt_name);
458
459         d_printf("SID               : %s\n", response.data.domain_info.sid);
460
461         d_printf("Active Directory  : %s\n",
462                  response.data.domain_info.active_directory ? "Yes" : "No");
463         d_printf("Native            : %s\n",
464                  response.data.domain_info.native_mode ? "Yes" : "No");
465
466         d_printf("Primary           : %s\n",
467                  response.data.domain_info.primary ? "Yes" : "No");
468
469         d_printf("Sequence          : %d\n", response.data.domain_info.sequence_number);
470
471         return True;
472 }
473
474 /* Get a foreign DC's name */
475 static BOOL wbinfo_getdcname(const char *domain_name)
476 {
477         struct winbindd_request request;
478         struct winbindd_response response;
479
480         ZERO_STRUCT(request);
481         ZERO_STRUCT(response);
482
483         fstrcpy(request.domain_name, domain_name);
484
485         /* Send request */
486
487         if (winbindd_request(WINBINDD_GETDCNAME, &request, &response) !=
488             NSS_STATUS_SUCCESS) {
489                 d_fprintf(stderr, "Could not get dc name for %s\n", domain_name);
490                 return False;
491         }
492
493         /* Display response */
494
495         d_printf("%s\n", response.data.dc_name);
496
497         return True;
498 }
499
500 /* Check trust account password */
501
502 static BOOL wbinfo_check_secret(void)
503 {
504         struct winbindd_response response;
505         NSS_STATUS result;
506
507         ZERO_STRUCT(response);
508
509         result = winbindd_request(WINBINDD_CHECK_MACHACC, NULL, &response);
510                 
511         d_printf("checking the trust secret via RPC calls %s\n", 
512                  (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
513
514         if (result != NSS_STATUS_SUCCESS)       
515                 d_fprintf(stderr, "error code was %s (0x%x)\n", 
516                          response.data.auth.nt_status_string, 
517                          response.data.auth.nt_status);
518         
519         return result == NSS_STATUS_SUCCESS;    
520 }
521
522 /* Convert uid to sid */
523
524 static BOOL wbinfo_uid_to_sid(uid_t uid)
525 {
526         struct winbindd_request request;
527         struct winbindd_response response;
528
529         ZERO_STRUCT(request);
530         ZERO_STRUCT(response);
531
532         /* Send request */
533
534         request.data.uid = uid;
535
536         if (winbindd_request(WINBINDD_UID_TO_SID, &request, &response) !=
537             NSS_STATUS_SUCCESS)
538                 return False;
539
540         /* Display response */
541
542         d_printf("%s\n", response.data.sid.sid);
543
544         return True;
545 }
546
547 /* Convert gid to sid */
548
549 static BOOL wbinfo_gid_to_sid(gid_t gid)
550 {
551         struct winbindd_request request;
552         struct winbindd_response response;
553
554         ZERO_STRUCT(request);
555         ZERO_STRUCT(response);
556
557         /* Send request */
558
559         request.data.gid = gid;
560
561         if (winbindd_request(WINBINDD_GID_TO_SID, &request, &response) !=
562             NSS_STATUS_SUCCESS)
563                 return False;
564
565         /* Display response */
566
567         d_printf("%s\n", response.data.sid.sid);
568
569         return True;
570 }
571
572 /* Convert sid to uid */
573
574 static BOOL wbinfo_sid_to_uid(char *sid)
575 {
576         struct winbindd_request request;
577         struct winbindd_response response;
578
579         ZERO_STRUCT(request);
580         ZERO_STRUCT(response);
581
582         /* Send request */
583
584         fstrcpy(request.data.sid, sid);
585
586         if (winbindd_request(WINBINDD_SID_TO_UID, &request, &response) !=
587             NSS_STATUS_SUCCESS)
588                 return False;
589
590         /* Display response */
591
592         d_printf("%d\n", (int)response.data.uid);
593
594         return True;
595 }
596
597 static BOOL wbinfo_sid_to_gid(char *sid)
598 {
599         struct winbindd_request request;
600         struct winbindd_response response;
601
602         ZERO_STRUCT(request);
603         ZERO_STRUCT(response);
604
605         /* Send request */
606
607         fstrcpy(request.data.sid, sid);
608
609         if (winbindd_request(WINBINDD_SID_TO_GID, &request, &response) !=
610             NSS_STATUS_SUCCESS)
611                 return False;
612
613         /* Display response */
614
615         d_printf("%d\n", (int)response.data.gid);
616
617         return True;
618 }
619
620 /* Convert sid to string */
621
622 static BOOL wbinfo_lookupsid(char *sid)
623 {
624         struct winbindd_request request;
625         struct winbindd_response response;
626
627         ZERO_STRUCT(request);
628         ZERO_STRUCT(response);
629
630         /* Send off request */
631
632         fstrcpy(request.data.sid, sid);
633
634         if (winbindd_request(WINBINDD_LOOKUPSID, &request, &response) !=
635             NSS_STATUS_SUCCESS)
636                 return False;
637
638         /* Display response */
639
640         d_printf("%s%c%s %d\n", response.data.name.dom_name, 
641                  winbind_separator(), response.data.name.name, 
642                  response.data.name.type);
643
644         return True;
645 }
646
647 static const char *sid_type_lookup(enum lsa_SidType r)
648 {
649         switch (r) {
650                 case SID_NAME_USE_NONE: return "SID_NAME_USE_NONE"; break;
651                 case SID_NAME_USER: return "SID_NAME_USER"; break;
652                 case SID_NAME_DOM_GRP: return "SID_NAME_DOM_GRP"; break;
653                 case SID_NAME_DOMAIN: return "SID_NAME_DOMAIN"; break;
654                 case SID_NAME_ALIAS: return "SID_NAME_ALIAS"; break;
655                 case SID_NAME_WKN_GRP: return "SID_NAME_WKN_GRP"; break;
656                 case SID_NAME_DELETED: return "SID_NAME_DELETED"; break;
657                 case SID_NAME_INVALID: return "SID_NAME_INVALID"; break;
658                 case SID_NAME_UNKNOWN: return "SID_NAME_UNKNOWN"; break;
659         }
660         return "Invalid sid type\n";
661 }
662
663 /* Convert string to sid */
664
665 static BOOL wbinfo_lookupname(char *name)
666 {
667         struct winbindd_request request;
668         struct winbindd_response response;
669
670         /* Send off request */
671
672         ZERO_STRUCT(request);
673         ZERO_STRUCT(response);
674
675         parse_wbinfo_domain_user(name, request.data.name.dom_name, 
676                                  request.data.name.name);
677
678         if (winbindd_request(WINBINDD_LOOKUPNAME, &request, &response) !=
679             NSS_STATUS_SUCCESS)
680                 return False;
681
682         /* Display response */
683
684         d_printf("%s %s (%d)\n", response.data.sid.sid, sid_type_lookup(response.data.sid.type), response.data.sid.type);
685
686         return True;
687 }
688
689 /* Authenticate a user with a plaintext password */
690
691 static BOOL wbinfo_auth_krb5(char *username, const char *cctype, uint32 flags)
692 {
693         struct winbindd_request request;
694         struct winbindd_response response;
695         NSS_STATUS result;
696         char *p;
697
698         /* Send off request */
699
700         ZERO_STRUCT(request);
701         ZERO_STRUCT(response);
702
703         p = strchr(username, '%');
704
705         if (p) {
706                 *p = 0;
707                 fstrcpy(request.data.auth.user, username);
708                 fstrcpy(request.data.auth.pass, p + 1);
709                 *p = '%';
710         } else
711                 fstrcpy(request.data.auth.user, username);
712
713         request.flags = flags;
714
715         fstrcpy(request.data.auth.krb5_cc_type, cctype);
716
717         request.data.auth.uid = geteuid();
718
719         result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
720
721         /* Display response */
722
723         d_printf("plaintext kerberos password authentication for [%s] %s (requesting cctype: %s)\n", 
724                 username, (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", cctype);
725
726         if (response.data.auth.nt_status)
727                 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n", 
728                          response.data.auth.nt_status_string, 
729                          response.data.auth.nt_status,
730                          response.data.auth.error_string);
731
732         if (result == NSS_STATUS_SUCCESS) {
733
734                 if (request.flags & WBFLAG_PAM_INFO3_TEXT) {
735                         if (response.data.auth.info3.user_flgs & NETLOGON_CACHED_ACCOUNT) {
736                                 d_printf("user_flgs: NETLOGON_CACHED_ACCOUNT\n");
737                         }
738                 }
739
740                 if (response.data.auth.krb5ccname[0] != '\0') {
741                         d_printf("credentials were put in: %s\n", response.data.auth.krb5ccname);
742                 } else {
743                         d_printf("no credentials cached\n");
744                 }
745         }
746
747         return result == NSS_STATUS_SUCCESS;
748 }
749
750 /* Authenticate a user with a plaintext password */
751
752 static BOOL wbinfo_auth(char *username)
753 {
754         struct winbindd_request request;
755         struct winbindd_response response;
756         NSS_STATUS result;
757         char *p;
758
759         /* Send off request */
760
761         ZERO_STRUCT(request);
762         ZERO_STRUCT(response);
763
764         p = strchr(username, '%');
765
766         if (p) {
767                 *p = 0;
768                 fstrcpy(request.data.auth.user, username);
769                 fstrcpy(request.data.auth.pass, p + 1);
770                 *p = '%';
771         } else
772                 fstrcpy(request.data.auth.user, username);
773
774         result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
775
776         /* Display response */
777
778         d_printf("plaintext password authentication %s\n", 
779                (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
780
781         if (response.data.auth.nt_status)
782                 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n", 
783                          response.data.auth.nt_status_string, 
784                          response.data.auth.nt_status,
785                          response.data.auth.error_string);
786
787         return result == NSS_STATUS_SUCCESS;
788 }
789
790 /* Authenticate a user with a challenge/response */
791
792 static BOOL wbinfo_auth_crap(char *username)
793 {
794         struct winbindd_request request;
795         struct winbindd_response response;
796         NSS_STATUS result;
797         fstring name_user;
798         fstring name_domain;
799         fstring pass;
800         char *p;
801
802         /* Send off request */
803
804         ZERO_STRUCT(request);
805         ZERO_STRUCT(response);
806
807         p = strchr(username, '%');
808
809         if (p) {
810                 *p = 0;
811                 fstrcpy(pass, p + 1);
812         }
813                 
814         parse_wbinfo_domain_user(username, name_domain, name_user);
815
816         request.data.auth_crap.logon_parameters = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT | MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
817
818         fstrcpy(request.data.auth_crap.user, name_user);
819
820         fstrcpy(request.data.auth_crap.domain, 
821                               name_domain);
822
823         generate_random_buffer(request.data.auth_crap.chal, 8);
824         
825         if (lp_client_ntlmv2_auth()) {
826                 DATA_BLOB server_chal;
827                 DATA_BLOB names_blob;   
828
829                 DATA_BLOB lm_response;
830                 DATA_BLOB nt_response;
831
832                 TALLOC_CTX *mem_ctx;
833                 mem_ctx = talloc_new(NULL);
834                 if (mem_ctx == NULL) {
835                         d_printf("talloc_new failed\n");
836                         return False;
837                 }
838
839                 server_chal = data_blob(request.data.auth_crap.chal, 8); 
840                 
841                 /* Pretend this is a login to 'us', for blob purposes */
842                 names_blob = NTLMv2_generate_names_blob(mem_ctx, lp_netbios_name(), lp_workgroup());
843                 
844                 if (!SMBNTLMv2encrypt(mem_ctx, name_user, name_domain, pass, &server_chal, 
845                                       &names_blob,
846                                       &lm_response, &nt_response, NULL, NULL)) {
847                         data_blob_free(&names_blob);
848                         data_blob_free(&server_chal);
849                         return False;
850                 }
851                 data_blob_free(&names_blob);
852                 data_blob_free(&server_chal);
853
854                 memcpy(request.data.auth_crap.nt_resp, nt_response.data, 
855                        MIN(nt_response.length, 
856                            sizeof(request.data.auth_crap.nt_resp)));
857                 request.data.auth_crap.nt_resp_len = nt_response.length;
858
859                 memcpy(request.data.auth_crap.lm_resp, lm_response.data, 
860                        MIN(lm_response.length, 
861                            sizeof(request.data.auth_crap.lm_resp)));
862                 request.data.auth_crap.lm_resp_len = lm_response.length;
863                        
864                 data_blob_free(&nt_response);
865                 data_blob_free(&lm_response);
866
867         } else {
868                 if (lp_client_lanman_auth() 
869                     && SMBencrypt(pass, request.data.auth_crap.chal, 
870                                (unsigned char *)request.data.auth_crap.lm_resp)) {
871                         request.data.auth_crap.lm_resp_len = 24;
872                 } else {
873                         request.data.auth_crap.lm_resp_len = 0;
874                 }
875                 SMBNTencrypt(pass, request.data.auth_crap.chal,
876                              (unsigned char *)request.data.auth_crap.nt_resp);
877
878                 request.data.auth_crap.nt_resp_len = 24;
879         }
880
881         result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
882
883         /* Display response */
884
885         d_printf("challenge/response password authentication %s\n", 
886                (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
887
888         if (response.data.auth.nt_status)
889                 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n", 
890                          response.data.auth.nt_status_string, 
891                          response.data.auth.nt_status,
892                          response.data.auth.error_string);
893
894         return result == NSS_STATUS_SUCCESS;
895 }
896
897 /* Print domain users */
898
899 static BOOL print_domain_users(const char *domain)
900 {
901         struct winbindd_request request;
902         struct winbindd_response response;
903         const char *extra_data;
904         fstring name;
905
906         /* Send request to winbind daemon */
907
908         ZERO_STRUCT(request);
909         ZERO_STRUCT(response);
910         
911         if (domain) {
912                 /* '.' is the special sign for our own domain */
913                 if ( strequal(domain, ".") )
914                         fstrcpy( request.domain_name, get_winbind_domain() );
915                 else
916                         fstrcpy( request.domain_name, domain );
917         }
918
919         if (winbindd_request(WINBINDD_LIST_USERS, &request, &response) !=
920             NSS_STATUS_SUCCESS)
921                 return False;
922
923         /* Look through extra data */
924
925         if (!response.extra_data.data)
926                 return False;
927
928         extra_data = (const char *)response.extra_data.data;
929
930         while(next_token(&extra_data, name, ",", sizeof(fstring)))
931                 d_printf("%s\n", name);
932         
933         SAFE_FREE(response.extra_data.data);
934
935         return True;
936 }
937
938 /* Print domain groups */
939
940 static BOOL print_domain_groups(const char *domain)
941 {
942         struct winbindd_request  request;
943         struct winbindd_response response;
944         const char *extra_data;
945         fstring name;
946
947         ZERO_STRUCT(request);
948         ZERO_STRUCT(response);
949
950         if (domain) {
951                 if ( strequal(domain, ".") )
952                         fstrcpy( request.domain_name, get_winbind_domain() );
953                 else
954                         fstrcpy( request.domain_name, domain );
955         }
956
957         if (winbindd_request(WINBINDD_LIST_GROUPS, &request, &response) !=
958             NSS_STATUS_SUCCESS)
959                 return False;
960
961         /* Look through extra data */
962
963         if (!response.extra_data.data)
964                 return False;
965
966         extra_data = (const char *)response.extra_data.data;
967
968         while(next_token(&extra_data, name, ",", sizeof(fstring)))
969                 d_printf("%s\n", name);
970
971         SAFE_FREE(response.extra_data.data);
972         
973         return True;
974 }
975
976 static BOOL wbinfo_ping(void)
977 {
978         NSS_STATUS result;
979
980         result = winbindd_request(WINBINDD_PING, NULL, NULL);
981
982         /* Display response */
983
984         d_printf("Ping to winbindd %s on fd %d\n", 
985                (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", winbindd_fd);
986
987         return result == NSS_STATUS_SUCCESS;
988 }
989
990 /* Main program */
991
992 enum {
993         OPT_SET_AUTH_USER = 1000,
994         OPT_GET_AUTH_USER,
995         OPT_DOMAIN_NAME,
996         OPT_SEQUENCE,
997         OPT_GETDCNAME,
998         OPT_USERDOMGROUPS,
999         OPT_USERSIDS,
1000         OPT_ALLOCATE_UID,
1001         OPT_ALLOCATE_GID,
1002         OPT_SEPARATOR,
1003         OPT_LIST_ALL_DOMAINS,
1004         OPT_LIST_OWN_DOMAIN,
1005         OPT_UID_INFO,
1006         OPT_GROUP_INFO,
1007 };
1008
1009 int main(int argc, char **argv, char **envp)
1010 {
1011         int opt;
1012
1013         poptContext pc;
1014         static char *string_arg;
1015         static char *opt_domain_name;
1016         static int int_arg;
1017         int result = 1;
1018
1019         struct poptOption long_options[] = {
1020                 POPT_AUTOHELP
1021
1022                 /* longName, shortName, argInfo, argPtr, value, descrip, 
1023                    argDesc */
1024
1025                 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
1026                 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
1027                 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1028                 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
1029                 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
1030                 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
1031                 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
1032                 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
1033                 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
1034                 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
1035                 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
1036                 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
1037                 { "all-domains", 0, POPT_ARG_NONE, 0, OPT_LIST_ALL_DOMAINS, "List all domains (trusted and own domain)" },
1038                 { "own-domain", 0, POPT_ARG_NONE, 0, OPT_LIST_OWN_DOMAIN, "List own domain" },
1039                 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
1040                 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
1041                 { "user-info", 'i', POPT_ARG_STRING, &string_arg, 'i', "Get user info", "USER" },
1042                 { "uid-info", 0, POPT_ARG_INT, &int_arg, OPT_UID_INFO, "Get user info from uid", "UID" },
1043                 { "group-info", 0, POPT_ARG_STRING, &string_arg, OPT_GROUP_INFO, "Get group info", "GROUP" },
1044                 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
1045                 { "user-domgroups", 0, POPT_ARG_STRING, &string_arg,
1046                   OPT_USERDOMGROUPS, "Get user domain groups", "SID" },
1047                 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
1048                 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
1049                 { "getdcname", 0, POPT_ARG_STRING, &string_arg, OPT_GETDCNAME,
1050                   "Get a DC name for a foreign domain", "domainname" },
1051                 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
1052                 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
1053 #ifdef HAVE_KRB5
1054                 { "krb5auth", 'K', POPT_ARG_STRING, &string_arg, 'K', "authenticate user using Kerberos", "user%password" },
1055                         /* destroys wbinfo --help output */
1056                         /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
1057 #endif
1058                 { "separator", 0, POPT_ARG_NONE, 0, OPT_SEPARATOR, "Get the active winbind separator", NULL },
1059                 POPT_COMMON_VERSION
1060                 POPT_COMMON_SAMBA
1061                 POPT_TABLEEND
1062         };
1063
1064         /* Parse options */
1065
1066         pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
1067
1068         /* Parse command line options */
1069
1070         if (argc == 1) {
1071                 poptPrintHelp(pc, stderr, 0);
1072                 return 1;
1073         }
1074
1075         while((opt = poptGetNextOpt(pc)) != -1) {
1076                 /* get the generic configuration parameters like --domain */
1077         }
1078
1079         poptFreeContext(pc);
1080
1081         pc = poptGetContext(NULL, argc, (const char **)argv, long_options, 
1082                             POPT_CONTEXT_KEEP_FIRST);
1083
1084         while((opt = poptGetNextOpt(pc)) != -1) {
1085                 switch (opt) {
1086                 case 'u':
1087                         if (!print_domain_users(opt_domain_name)) {
1088                                 d_fprintf(stderr, "Error looking up domain users\n");
1089                                 goto done;
1090                         }
1091                         break;
1092                 case 'g':
1093                         if (!print_domain_groups(opt_domain_name)) {
1094                                 d_fprintf(stderr, "Error looking up domain groups\n");
1095                                 goto done;
1096                         }
1097                         break;
1098                 case 's':
1099                         if (!wbinfo_lookupsid(string_arg)) {
1100                                 d_fprintf(stderr, "Could not lookup sid %s\n", string_arg);
1101                                 goto done;
1102                         }
1103                         break;
1104                 case 'n':
1105                         if (!wbinfo_lookupname(string_arg)) {
1106                                 d_fprintf(stderr, "Could not lookup name %s\n", string_arg);
1107                                 goto done;
1108                         }
1109                         break;
1110                 case 'N':
1111                         if (!wbinfo_wins_byname(string_arg)) {
1112                                 d_fprintf(stderr, "Could not lookup WINS by name %s\n", string_arg);
1113                                 goto done;
1114                         }
1115                         break;
1116                 case 'I':
1117                         if (!wbinfo_wins_byip(string_arg)) {
1118                                 d_fprintf(stderr, "Could not lookup WINS by IP %s\n", string_arg);
1119                                 goto done;
1120                         }
1121                         break;
1122                 case 'U':
1123                         if (!wbinfo_uid_to_sid(int_arg)) {
1124                                 d_fprintf(stderr, "Could not convert uid %d to sid\n", int_arg);
1125                                 goto done;
1126                         }
1127                         break;
1128                 case 'G':
1129                         if (!wbinfo_gid_to_sid(int_arg)) {
1130                                 d_fprintf(stderr, "Could not convert gid %d to sid\n",
1131                                        int_arg);
1132                                 goto done;
1133                         }
1134                         break;
1135                 case 'S':
1136                         if (!wbinfo_sid_to_uid(string_arg)) {
1137                                 d_fprintf(stderr, "Could not convert sid %s to uid\n",
1138                                        string_arg);
1139                                 goto done;
1140                         }
1141                         break;
1142                 case 'Y':
1143                         if (!wbinfo_sid_to_gid(string_arg)) {
1144                                 d_fprintf(stderr, "Could not convert sid %s to gid\n",
1145                                        string_arg);
1146                                 goto done;
1147                         }
1148                         break;
1149                 case 't':
1150                         if (!wbinfo_check_secret()) {
1151                                 d_fprintf(stderr, "Could not check secret\n");
1152                                 goto done;
1153                         }
1154                         break;
1155                 case 'm':
1156                         if (!wbinfo_list_domains(False)) {
1157                                 d_fprintf(stderr, "Could not list trusted domains\n");
1158                                 goto done;
1159                         }
1160                         break;
1161                 case OPT_SEQUENCE:
1162                         if (!wbinfo_show_sequence(opt_domain_name)) {
1163                                 d_fprintf(stderr, "Could not show sequence numbers\n");
1164                                 goto done;
1165                         }
1166                         break;
1167                 case 'D':
1168                         if (!wbinfo_domain_info(string_arg)) {
1169                                 d_fprintf(stderr, "Could not get domain info\n");
1170                                 goto done;
1171                         }
1172                         break;
1173                 case 'i':
1174                         if (!wbinfo_get_userinfo(string_arg)) {
1175                                 d_fprintf(stderr, "Could not get info for user %s\n",
1176                                                   string_arg);
1177                                 goto done;
1178                         }
1179                         break;
1180                 case OPT_UID_INFO:
1181                         if ( !wbinfo_get_uidinfo(int_arg)) {
1182                                 d_fprintf(stderr, "Could not get info for uid "
1183                                                 "%d\n", int_arg);
1184                                 goto done;
1185                         }
1186                         break;
1187                 case OPT_GROUP_INFO:
1188                         if ( !wbinfo_get_groupinfo(string_arg)) {
1189                                 d_fprintf(stderr, "Could not get info for "
1190                                           "group %s\n", string_arg);
1191                                 goto done;
1192                         }
1193                         break;
1194                 case 'r':
1195                         if (!wbinfo_get_usergroups(string_arg)) {
1196                                 d_fprintf(stderr, "Could not get groups for user %s\n", 
1197                                        string_arg);
1198                                 goto done;
1199                         }
1200                         break;
1201                 case OPT_USERSIDS:
1202                         if (!wbinfo_get_usersids(string_arg)) {
1203                                 d_fprintf(stderr, "Could not get group SIDs for user SID %s\n", 
1204                                        string_arg);
1205                                 goto done;
1206                         }
1207                         break;
1208                 case OPT_USERDOMGROUPS:
1209                         if (!wbinfo_get_userdomgroups(string_arg)) {
1210                                 d_fprintf(stderr, "Could not get user's domain groups "
1211                                          "for user SID %s\n", string_arg);
1212                                 goto done;
1213                         }
1214                         break;
1215                 case 'a': {
1216                                 BOOL got_error = False;
1217
1218                                 if (!wbinfo_auth(string_arg)) {
1219                                         d_fprintf(stderr, "Could not authenticate user %s with "
1220                                                 "plaintext password\n", string_arg);
1221                                         got_error = True;
1222                                 }
1223
1224                                 if (!wbinfo_auth_crap(string_arg)) {
1225                                         d_fprintf(stderr, "Could not authenticate user %s with "
1226                                                 "challenge/response\n", string_arg);
1227                                         got_error = True;
1228                                 }
1229
1230                                 if (got_error)
1231                                         goto done;
1232                                 break;
1233                         }
1234                 case 'K': {
1235                                 uint32 flags =  WBFLAG_PAM_KRB5 |
1236                                                 WBFLAG_PAM_CACHED_LOGIN |
1237                                                 WBFLAG_PAM_FALLBACK_AFTER_KRB5 |
1238                                                 WBFLAG_PAM_INFO3_TEXT;
1239
1240                                 if (!wbinfo_auth_krb5(string_arg, "FILE", flags)) {
1241                                         d_fprintf(stderr, "Could not authenticate user [%s] with "
1242                                                 "Kerberos (ccache: %s)\n", string_arg, "FILE");
1243                                         goto done;
1244                                 }
1245                                 break;
1246                         }
1247                 case 'p':
1248                         if (!wbinfo_ping()) {
1249                                 d_fprintf(stderr, "could not ping winbindd!\n");
1250                                 goto done;
1251                         }
1252                         break;
1253                 case OPT_GETDCNAME:
1254                         if (!wbinfo_getdcname(string_arg)) {
1255                                 goto done;
1256                         }
1257                         break;
1258                 case OPT_SEPARATOR: {
1259                         const char sep = winbind_separator_int(True);
1260                         if ( !sep ) {
1261                                 goto done;
1262                         }
1263                         d_printf("%c\n", sep);
1264                         break;
1265                 }
1266                 case OPT_LIST_ALL_DOMAINS:
1267                         if (!wbinfo_list_domains(True)) {
1268                                 goto done;
1269                         }
1270                         break;
1271                 case OPT_LIST_OWN_DOMAIN:
1272                         if (!wbinfo_list_own_domain()) {
1273                                 goto done;
1274                         }
1275                         break;
1276                 /* generic configuration options */
1277                 case OPT_DOMAIN_NAME:
1278                         break;
1279                 default:
1280                         d_fprintf(stderr, "Invalid option\n");
1281                         poptPrintHelp(pc, stderr, 0);
1282                         goto done;
1283                 }
1284         }
1285
1286         result = 0;
1287
1288         /* Exit code */
1289
1290  done:
1291         poptFreeContext(pc);
1292         return result;
1293 }