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