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