s3: Implement wbcGetpwsid
[sfrench/samba-autobuild/.git] / nsswitch / wbinfo.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Winbind status program.
5
6    Copyright (C) Tim Potter      2000-2003
7    Copyright (C) Andrew Bartlett 2002
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 "libwbclient/wbclient.h"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_WINBIND
29
30 static struct wbcInterfaceDetails *init_interface_details(void)
31 {
32         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
33         static struct wbcInterfaceDetails *details;
34
35         if (details) {
36                 return details;
37         }
38
39         wbc_status = wbcInterfaceDetails(&details);
40         if (!WBC_ERROR_IS_OK(wbc_status)) {
41                 d_fprintf(stderr, "could not obtain winbind interface details!\n");
42         }
43
44         return details;
45 }
46
47 static char winbind_separator_int(bool strict)
48 {
49         struct wbcInterfaceDetails *details;
50         static bool got_sep;
51         static char sep;
52
53         if (got_sep)
54                 return sep;
55
56         details = init_interface_details();
57
58         if (!details) {
59                 d_fprintf(stderr, "could not obtain winbind separator!\n");
60                 if (strict) {
61                         return 0;
62                 }
63                 /* HACK: (this module should not call lp_ funtions) */
64                 return *lp_winbind_separator();
65         }
66
67         sep = details->winbind_separator;
68         got_sep = true;
69
70         if (!sep) {
71                 d_fprintf(stderr, "winbind separator was NULL!\n");
72                 if (strict) {
73                         return 0;
74                 }
75                 /* HACK: (this module should not call lp_ funtions) */
76                 sep = *lp_winbind_separator();
77         }
78
79         return sep;
80 }
81
82 static char winbind_separator(void)
83 {
84         return winbind_separator_int(false);
85 }
86
87 static const char *get_winbind_domain(void)
88 {
89         static struct wbcInterfaceDetails *details;
90
91         details = init_interface_details();
92
93         if (!details) {
94                 d_fprintf(stderr, "could not obtain winbind domain name!\n");
95
96                 /* HACK: (this module should not call lp_ functions) */
97                 return lp_workgroup();
98         }
99
100         return details->netbios_domain;
101 }
102
103 /* Copy of parse_domain_user from winbindd_util.c.  Parse a string of the
104    form DOMAIN/user into a domain and a user */
105
106 static bool parse_wbinfo_domain_user(const char *domuser, fstring domain,
107                                      fstring user)
108 {
109
110         char *p = strchr(domuser,winbind_separator());
111
112         if (!p) {
113                 /* Maybe it was a UPN? */
114                 if ((p = strchr(domuser, '@')) != NULL) {
115                         fstrcpy(domain, "");
116                         fstrcpy(user, domuser);
117                         return true;
118                 }
119
120                 fstrcpy(user, domuser);
121                 fstrcpy(domain, get_winbind_domain());
122                 return true;
123         }
124
125         fstrcpy(user, p+1);
126         fstrcpy(domain, domuser);
127         domain[PTR_DIFF(p, domuser)] = 0;
128         strupper_m(domain);
129
130         return true;
131 }
132
133 /* Parse string of "uid,sid" or "gid,sid" into separate int and string values.
134  * Return true if input was valid, false otherwise. */
135 static bool parse_mapping_arg(char *arg, int *id, char **sid)
136 {
137         char *tmp, *endptr;
138
139         if (!arg || !*arg)
140                 return false;
141
142         tmp = strtok(arg, ",");
143         *sid = strtok(NULL, ",");
144
145         if (!tmp || !*tmp || !*sid || !**sid)
146                 return false;
147
148         /* Because atoi() can return 0 on invalid input, which would be a valid
149          * UID/GID we must use strtoul() and do error checking */
150         *id = strtoul(tmp, &endptr, 10);
151
152         if (endptr[0] != '\0')
153                 return false;
154
155         return true;
156 }
157
158 /* pull pwent info for a given user */
159
160 static bool wbinfo_get_userinfo(char *user)
161 {
162         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
163         struct passwd *pwd = NULL;
164
165         wbc_status = wbcGetpwnam(user, &pwd);
166         if (!WBC_ERROR_IS_OK(wbc_status)) {
167                 return false;
168         }
169
170         d_printf("%s:%s:%d:%d:%s:%s:%s\n",
171                  pwd->pw_name,
172                  pwd->pw_passwd,
173                  pwd->pw_uid,
174                  pwd->pw_gid,
175                  pwd->pw_gecos,
176                  pwd->pw_dir,
177                  pwd->pw_shell);
178
179         return true;
180 }
181
182 /* pull pwent info for a given uid */
183 static bool wbinfo_get_uidinfo(int uid)
184 {
185         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
186         struct passwd *pwd = NULL;
187
188         wbc_status = wbcGetpwuid(uid, &pwd);
189         if (!WBC_ERROR_IS_OK(wbc_status)) {
190                 return false;
191         }
192
193         d_printf("%s:%s:%d:%d:%s:%s:%s\n",
194                  pwd->pw_name,
195                  pwd->pw_passwd,
196                  pwd->pw_uid,
197                  pwd->pw_gid,
198                  pwd->pw_gecos,
199                  pwd->pw_dir,
200                  pwd->pw_shell);
201
202         return true;
203 }
204
205 static bool wbinfo_get_user_sidinfo(const char *sid_str)
206 {
207         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
208         struct passwd *pwd = NULL;
209         struct wbcDomainSid sid;
210
211         wbc_status = wbcStringToSid(sid_str, &sid);
212         wbc_status = wbcGetpwsid(&sid, &pwd);
213         if (!WBC_ERROR_IS_OK(wbc_status)) {
214                 return false;
215         }
216
217         d_printf("%s:%s:%d:%d:%s:%s:%s\n",
218                  pwd->pw_name,
219                  pwd->pw_passwd,
220                  pwd->pw_uid,
221                  pwd->pw_gid,
222                  pwd->pw_gecos,
223                  pwd->pw_dir,
224                  pwd->pw_shell);
225
226         return true;
227 }
228
229
230 /* pull grent for a given group */
231 static bool wbinfo_get_groupinfo(const char *group)
232 {
233         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
234         struct group *grp;
235
236         wbc_status = wbcGetgrnam(group, &grp);
237         if (!WBC_ERROR_IS_OK(wbc_status)) {
238                 return false;
239         }
240
241         d_printf("%s:%s:%d\n",
242                  grp->gr_name,
243                  grp->gr_passwd,
244                  grp->gr_gid);
245
246         wbcFreeMemory(grp);
247
248         return true;
249 }
250
251 /* pull grent for a given gid */
252 static bool wbinfo_get_gidinfo(int gid)
253 {
254         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
255         struct group *grp;
256
257         wbc_status = wbcGetgrgid(gid, &grp);
258         if (!WBC_ERROR_IS_OK(wbc_status)) {
259                 return false;
260         }
261
262         d_printf("%s:%s:%d\n",
263                  grp->gr_name,
264                  grp->gr_passwd,
265                  grp->gr_gid);
266
267         wbcFreeMemory(grp);
268
269         return true;
270 }
271
272 /* List groups a user is a member of */
273
274 static bool wbinfo_get_usergroups(const char *user)
275 {
276         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
277         uint32_t num_groups;
278         uint32_t i;
279         gid_t *groups = NULL;
280
281         /* Send request */
282
283         wbc_status = wbcGetGroups(user, &num_groups, &groups);
284         if (!WBC_ERROR_IS_OK(wbc_status)) {
285                 return false;
286         }
287
288         for (i = 0; i < num_groups; i++) {
289                 d_printf("%d\n", (int)groups[i]);
290         }
291
292         wbcFreeMemory(groups);
293
294         return true;
295 }
296
297
298 /* List group SIDs a user SID is a member of */
299 static bool wbinfo_get_usersids(const char *user_sid_str)
300 {
301         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
302         uint32_t num_sids;
303         uint32_t i;
304         struct wbcDomainSid user_sid, *sids = NULL;
305
306         /* Send request */
307
308         wbc_status = wbcStringToSid(user_sid_str, &user_sid);
309         if (!WBC_ERROR_IS_OK(wbc_status)) {
310                 return false;
311         }
312
313         wbc_status = wbcLookupUserSids(&user_sid, false, &num_sids, &sids);
314         if (!WBC_ERROR_IS_OK(wbc_status)) {
315                 return false;
316         }
317
318         for (i = 0; i < num_sids; i++) {
319                 char *str = NULL;
320                 wbc_status = wbcSidToString(&sids[i], &str);
321                 if (!WBC_ERROR_IS_OK(wbc_status)) {
322                         wbcFreeMemory(sids);
323                         return false;
324                 }
325                 d_printf("%s\n", str);
326                 wbcFreeMemory(str);
327         }
328
329         wbcFreeMemory(sids);
330
331         return true;
332 }
333
334 static bool wbinfo_get_userdomgroups(const char *user_sid_str)
335 {
336         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
337         uint32_t num_sids;
338         uint32_t i;
339         struct wbcDomainSid user_sid, *sids = NULL;
340
341         /* Send request */
342
343         wbc_status = wbcStringToSid(user_sid_str, &user_sid);
344         if (!WBC_ERROR_IS_OK(wbc_status)) {
345                 return false;
346         }
347
348         wbc_status = wbcLookupUserSids(&user_sid, true, &num_sids, &sids);
349         if (!WBC_ERROR_IS_OK(wbc_status)) {
350                 return false;
351         }
352
353         for (i = 0; i < num_sids; i++) {
354                 char *str = NULL;
355                 wbc_status = wbcSidToString(&sids[i], &str);
356                 if (!WBC_ERROR_IS_OK(wbc_status)) {
357                         wbcFreeMemory(sids);
358                         return false;
359                 }
360                 d_printf("%s\n", str);
361                 wbcFreeMemory(str);
362         }
363
364         wbcFreeMemory(sids);
365
366         return true;
367 }
368
369 /* Convert NetBIOS name to IP */
370
371 static bool wbinfo_wins_byname(const char *name)
372 {
373         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
374         char *ip = NULL;
375
376         wbc_status = wbcResolveWinsByName(name, &ip);
377         if (!WBC_ERROR_IS_OK(wbc_status)) {
378                 return false;
379         }
380
381         /* Display response */
382
383         d_printf("%s\n", ip);
384
385         wbcFreeMemory(ip);
386
387         return true;
388 }
389
390 /* Convert IP to NetBIOS name */
391
392 static bool wbinfo_wins_byip(const char *ip)
393 {
394         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
395         char *name = NULL;
396
397         wbc_status = wbcResolveWinsByIP(ip, &name);
398         if (!WBC_ERROR_IS_OK(wbc_status)) {
399                 return false;
400         }
401
402         /* Display response */
403
404         d_printf("%s\n", name);
405
406         wbcFreeMemory(name);
407
408         return true;
409 }
410
411 /* List all/trusted domains */
412
413 static bool wbinfo_list_domains(bool list_all_domains, bool verbose)
414 {
415         struct wbcDomainInfo *domain_list = NULL;
416         size_t num_domains;
417         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
418         bool print_all = !list_all_domains && verbose;
419         int i;
420
421         wbc_status = wbcListTrusts(&domain_list, &num_domains);
422         if (!WBC_ERROR_IS_OK(wbc_status)) {
423                 return false;
424         }
425
426         if (print_all) {
427                 d_printf("%-16s%-24s%-12s%-12s%-5s%-5s\n",
428                          "Domain Name", "DNS Domain", "Trust Type",
429                          "Transitive", "In", "Out");
430         }
431
432         for (i=0; i<num_domains; i++) {
433                 if (print_all) {
434                         d_printf("%-16s", domain_list[i].short_name);
435                 } else {
436                         d_printf("%s", domain_list[i].short_name);
437                         d_printf("\n");
438                         continue;
439                 }
440
441                 d_printf("%-24s", domain_list[i].dns_name);
442
443                 switch(domain_list[i].trust_type) {
444                 case WBC_DOMINFO_TRUSTTYPE_NONE:
445                         d_printf("None        ");
446                         break;
447                 case WBC_DOMINFO_TRUSTTYPE_FOREST:
448                         d_printf("Forest      ");
449                         break;
450                 case WBC_DOMINFO_TRUSTTYPE_EXTERNAL:
451                         d_printf("External    ");
452                         break;
453                 case WBC_DOMINFO_TRUSTTYPE_IN_FOREST:
454                         d_printf("In-Forest   ");
455                         break;
456                 }
457
458                 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_TRANSITIVE) {
459                         d_printf("Yes         ");
460                 } else {
461                         d_printf("No          ");
462                 }
463
464                 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_INCOMING) {
465                         d_printf("Yes  ");
466                 } else {
467                         d_printf("No   ");
468                 }
469
470                 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_OUTGOING) {
471                         d_printf("Yes  ");
472                 } else {
473                         d_printf("No   ");
474                 }
475
476                 d_printf("\n");
477         }
478
479         return true;
480 }
481
482 /* List own domain */
483
484 static bool wbinfo_list_own_domain(void)
485 {
486         d_printf("%s\n", get_winbind_domain());
487
488         return true;
489 }
490
491 /* show sequence numbers */
492 static bool wbinfo_show_sequence(const char *domain)
493 {
494         d_printf("This command has been deprecated.  Please use the --online-status option instead.\n");
495         return false;
496 }
497
498 /* show sequence numbers */
499 static bool wbinfo_show_onlinestatus(const char *domain)
500 {
501         struct wbcDomainInfo *domain_list = NULL;
502         size_t num_domains;
503         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
504         int i;
505
506         wbc_status = wbcListTrusts(&domain_list, &num_domains);
507         if (!WBC_ERROR_IS_OK(wbc_status)) {
508                 return false;
509         }
510
511         for (i=0; i<num_domains; i++) {
512                 bool is_offline;
513
514                 if (domain) {
515                         if (!strequal(domain_list[i].short_name, domain)) {
516                                 continue;
517                         }
518                 }
519
520                 is_offline = (domain_list[i].domain_flags & WBC_DOMINFO_DOMAIN_OFFLINE);
521
522                 d_printf("%s : %s\n",
523                          domain_list[i].short_name,
524                          is_offline ? "offline" : "online" );
525         }
526
527         return true;
528 }
529
530
531 /* Show domain info */
532
533 static bool wbinfo_domain_info(const char *domain)
534 {
535         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
536         struct wbcDomainInfo *dinfo = NULL;
537         char *sid_str = NULL;
538
539         if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0')) {
540                 domain = get_winbind_domain();
541         }
542
543         /* Send request */
544
545         wbc_status = wbcDomainInfo(domain, &dinfo);
546         if (!WBC_ERROR_IS_OK(wbc_status)) {
547                 return false;
548         }
549
550         wbc_status = wbcSidToString(&dinfo->sid, &sid_str);
551         if (!WBC_ERROR_IS_OK(wbc_status)) {
552                 wbcFreeMemory(dinfo);
553                 return false;
554         }
555
556         /* Display response */
557
558         d_printf("Name              : %s\n", dinfo->short_name);
559         d_printf("Alt_Name          : %s\n", dinfo->dns_name);
560
561         d_printf("SID               : %s\n", sid_str);
562
563         d_printf("Active Directory  : %s\n",
564                  (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_AD) ? "Yes" : "No");
565         d_printf("Native            : %s\n",
566                  (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_NATIVE) ? "Yes" : "No");
567
568         d_printf("Primary           : %s\n",
569                  (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_PRIMARY) ? "Yes" : "No");
570
571         wbcFreeMemory(sid_str);
572         wbcFreeMemory(dinfo);
573
574         return true;
575 }
576
577 /* Get a foreign DC's name */
578 static bool wbinfo_getdcname(const char *domain_name)
579 {
580         struct winbindd_request request;
581         struct winbindd_response response;
582
583         ZERO_STRUCT(request);
584         ZERO_STRUCT(response);
585
586         fstrcpy(request.domain_name, domain_name);
587
588         /* Send request */
589
590         if (winbindd_request_response(WINBINDD_GETDCNAME, &request, &response) !=
591             NSS_STATUS_SUCCESS) {
592                 d_fprintf(stderr, "Could not get dc name for %s\n", domain_name);
593                 return false;
594         }
595
596         /* Display response */
597
598         d_printf("%s\n", response.data.dc_name);
599
600         return true;
601 }
602
603 /* Find a DC */
604 static bool wbinfo_dsgetdcname(const char *domain_name, uint32_t flags)
605 {
606         struct winbindd_request request;
607         struct winbindd_response response;
608
609         ZERO_STRUCT(request);
610         ZERO_STRUCT(response);
611
612         fstrcpy(request.data.dsgetdcname.domain_name, domain_name);
613         request.data.dsgetdcname.flags = flags;
614
615         request.flags |= DS_DIRECTORY_SERVICE_REQUIRED;
616
617         /* Send request */
618
619         if (winbindd_request_response(WINBINDD_DSGETDCNAME, &request, &response) !=
620             NSS_STATUS_SUCCESS) {
621                 d_fprintf(stderr, "Could not find dc for %s\n", domain_name);
622                 return false;
623         }
624
625         /* Display response */
626
627         d_printf("%s\n", response.data.dsgetdcname.dc_unc);
628         d_printf("%s\n", response.data.dsgetdcname.dc_address);
629         d_printf("%d\n", response.data.dsgetdcname.dc_address_type);
630         d_printf("%s\n", response.data.dsgetdcname.domain_guid);
631         d_printf("%s\n", response.data.dsgetdcname.domain_name);
632         d_printf("%s\n", response.data.dsgetdcname.forest_name);
633         d_printf("0x%08x\n", response.data.dsgetdcname.dc_flags);
634         d_printf("%s\n", response.data.dsgetdcname.dc_site_name);
635         d_printf("%s\n", response.data.dsgetdcname.client_site_name);
636
637         return true;
638 }
639
640 /* Check trust account password */
641
642 static bool wbinfo_check_secret(void)
643 {
644         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
645         struct wbcAuthErrorInfo *error = NULL;
646
647         wbc_status = wbcCheckTrustCredentials(NULL, &error);
648
649         d_printf("checking the trust secret via RPC calls %s\n",
650                  WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
651
652         if (wbc_status == WBC_ERR_AUTH_ERROR) {
653                 d_fprintf(stderr, "error code was %s (0x%x)\n",
654                           error->nt_string, error->nt_status);
655                 wbcFreeMemory(error);
656         }
657         if (!WBC_ERROR_IS_OK(wbc_status)) {
658                 return false;
659         }
660
661         return true;
662 }
663
664 /* Convert uid to sid */
665
666 static bool wbinfo_uid_to_sid(uid_t uid)
667 {
668         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
669         struct wbcDomainSid sid;
670         char *sid_str = NULL;
671
672         /* Send request */
673
674         wbc_status = wbcUidToSid(uid, &sid);
675         if (!WBC_ERROR_IS_OK(wbc_status)) {
676                 return false;
677         }
678
679         wbc_status = wbcSidToString(&sid, &sid_str);
680         if (!WBC_ERROR_IS_OK(wbc_status)) {
681                 return false;
682         }
683
684         /* Display response */
685
686         d_printf("%s\n", sid_str);
687
688         wbcFreeMemory(sid_str);
689
690         return true;
691 }
692
693 /* Convert gid to sid */
694
695 static bool wbinfo_gid_to_sid(gid_t gid)
696 {
697         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
698         struct wbcDomainSid sid;
699         char *sid_str = NULL;
700
701         /* Send request */
702
703         wbc_status = wbcGidToSid(gid, &sid);
704         if (!WBC_ERROR_IS_OK(wbc_status)) {
705                 return false;
706         }
707
708         wbc_status = wbcSidToString(&sid, &sid_str);
709         if (!WBC_ERROR_IS_OK(wbc_status)) {
710                 return false;
711         }
712
713         /* Display response */
714
715         d_printf("%s\n", sid_str);
716
717         wbcFreeMemory(sid_str);
718
719         return true;
720 }
721
722 /* Convert sid to uid */
723
724 static bool wbinfo_sid_to_uid(const char *sid_str)
725 {
726         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
727         struct wbcDomainSid sid;
728         uid_t uid;
729
730         /* Send request */
731
732         wbc_status = wbcStringToSid(sid_str, &sid);
733         if (!WBC_ERROR_IS_OK(wbc_status)) {
734                 return false;
735         }
736
737         wbc_status = wbcSidToUid(&sid, &uid);
738         if (!WBC_ERROR_IS_OK(wbc_status)) {
739                 return false;
740         }
741
742         /* Display response */
743
744         d_printf("%d\n", (int)uid);
745
746         return true;
747 }
748
749 static bool wbinfo_sid_to_gid(const char *sid_str)
750 {
751         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
752         struct wbcDomainSid sid;
753         gid_t gid;
754
755         /* Send request */
756
757         wbc_status = wbcStringToSid(sid_str, &sid);
758         if (!WBC_ERROR_IS_OK(wbc_status)) {
759                 return false;
760         }
761
762         wbc_status = wbcSidToGid(&sid, &gid);
763         if (!WBC_ERROR_IS_OK(wbc_status)) {
764                 return false;
765         }
766
767         /* Display response */
768
769         d_printf("%d\n", (int)gid);
770
771         return true;
772 }
773
774 static bool wbinfo_allocate_uid(void)
775 {
776         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
777         uid_t uid;
778
779         /* Send request */
780
781         wbc_status = wbcAllocateUid(&uid);
782         if (!WBC_ERROR_IS_OK(wbc_status)) {
783                 return false;
784         }
785
786         /* Display response */
787
788         d_printf("New uid: %d\n", uid);
789
790         return true;
791 }
792
793 static bool wbinfo_allocate_gid(void)
794 {
795         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
796         gid_t gid;
797
798         /* Send request */
799
800         wbc_status = wbcAllocateGid(&gid);
801         if (!WBC_ERROR_IS_OK(wbc_status)) {
802                 return false;
803         }
804
805         /* Display response */
806
807         d_printf("New gid: %d\n", gid);
808
809         return true;
810 }
811
812 static bool wbinfo_set_uid_mapping(uid_t uid, const char *sid_str)
813 {
814         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
815         struct wbcDomainSid sid;
816
817         /* Send request */
818
819         wbc_status = wbcStringToSid(sid_str, &sid);
820         if (!WBC_ERROR_IS_OK(wbc_status)) {
821                 return false;
822         }
823
824         wbc_status = wbcSetUidMapping(uid, &sid);
825         if (!WBC_ERROR_IS_OK(wbc_status)) {
826                 return false;
827         }
828
829         /* Display response */
830
831         d_printf("uid %d now mapped to sid %s\n", uid, sid_str);
832
833         return true;
834 }
835
836 static bool wbinfo_set_gid_mapping(gid_t gid, const char *sid_str)
837 {
838         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
839         struct wbcDomainSid sid;
840
841         /* Send request */
842
843         wbc_status = wbcStringToSid(sid_str, &sid);
844         if (!WBC_ERROR_IS_OK(wbc_status)) {
845                 return false;
846         }
847
848         wbc_status = wbcSetGidMapping(gid, &sid);
849         if (!WBC_ERROR_IS_OK(wbc_status)) {
850                 return false;
851         }
852
853         /* Display response */
854
855         d_printf("gid %d now mapped to sid %s\n", gid, sid_str);
856
857         return true;
858 }
859
860 static bool wbinfo_remove_uid_mapping(uid_t uid, const char *sid_str)
861 {
862         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
863         struct wbcDomainSid sid;
864
865         /* Send request */
866
867         wbc_status = wbcStringToSid(sid_str, &sid);
868         if (!WBC_ERROR_IS_OK(wbc_status)) {
869                 return false;
870         }
871
872         wbc_status = wbcRemoveUidMapping(uid, &sid);
873         if (!WBC_ERROR_IS_OK(wbc_status)) {
874                 return false;
875         }
876
877         /* Display response */
878
879         d_printf("Removed uid %d to sid %s mapping\n", uid, sid_str);
880
881         return true;
882 }
883
884 static bool wbinfo_remove_gid_mapping(gid_t gid, const char *sid_str)
885 {
886         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
887         struct wbcDomainSid sid;
888
889         /* Send request */
890
891         wbc_status = wbcStringToSid(sid_str, &sid);
892         if (!WBC_ERROR_IS_OK(wbc_status)) {
893                 return false;
894         }
895
896         wbc_status = wbcRemoveGidMapping(gid, &sid);
897         if (!WBC_ERROR_IS_OK(wbc_status)) {
898                 return false;
899         }
900
901         /* Display response */
902
903         d_printf("Removed gid %d to sid %s mapping\n", gid, sid_str);
904
905         return true;
906 }
907
908 /* Convert sid to string */
909
910 static bool wbinfo_lookupsid(const char *sid_str)
911 {
912         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
913         struct wbcDomainSid sid;
914         char *domain;
915         char *name;
916         enum wbcSidType type;
917
918         /* Send off request */
919
920         wbc_status = wbcStringToSid(sid_str, &sid);
921         if (!WBC_ERROR_IS_OK(wbc_status)) {
922                 return false;
923         }
924
925         wbc_status = wbcLookupSid(&sid, &domain, &name, &type);
926         if (!WBC_ERROR_IS_OK(wbc_status)) {
927                 return false;
928         }
929
930         /* Display response */
931
932         d_printf("%s%c%s %d\n",
933                  domain, winbind_separator(), name, type);
934
935         return true;
936 }
937
938 /* Convert sid to fullname */
939
940 static bool wbinfo_lookupsid_fullname(const char *sid_str)
941 {
942         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
943         struct wbcDomainSid sid;
944         char *domain;
945         char *name;
946         enum wbcSidType type;
947
948         /* Send off request */
949
950         wbc_status = wbcStringToSid(sid_str, &sid);
951         if (!WBC_ERROR_IS_OK(wbc_status)) {
952                 return false;
953         }
954
955         wbc_status = wbcGetDisplayName(&sid, &domain, &name, &type);
956         if (!WBC_ERROR_IS_OK(wbc_status)) {
957                 return false;
958         }
959
960         /* Display response */
961
962         d_printf("%s%c%s %d\n",
963                  domain, winbind_separator(), name, type);
964
965         return true;
966 }
967
968 /* Lookup a list of RIDs */
969
970 static bool wbinfo_lookuprids(const char *domain, const char *arg)
971 {
972         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
973         struct wbcDomainInfo *dinfo = NULL;
974         char *domain_name = NULL;
975         const char **names = NULL;
976         enum wbcSidType *types = NULL;
977         size_t i;
978         int num_rids;
979         uint32 *rids = NULL;
980         const char *p;
981         char *ridstr;
982         TALLOC_CTX *mem_ctx = NULL;
983         bool ret = false;
984
985         if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0')) {
986                 domain = get_winbind_domain();
987         }
988
989         /* Send request */
990
991         wbc_status = wbcDomainInfo(domain, &dinfo);
992         if (!WBC_ERROR_IS_OK(wbc_status)) {
993                 d_printf("wbcDomainInfo(%s) failed: %s\n", domain,
994                          wbcErrorString(wbc_status));
995                 goto done;
996         }
997
998         mem_ctx = talloc_new(NULL);
999         if (mem_ctx == NULL) {
1000                 d_printf("talloc_new failed\n");
1001                 goto done;
1002         }
1003
1004         num_rids = 0;
1005         rids = NULL;
1006         p = arg;
1007
1008         while (next_token_talloc(mem_ctx, &p, &ridstr, " ,\n")) {
1009                 uint32 rid = strtoul(ridstr, NULL, 10);
1010                 ADD_TO_ARRAY(mem_ctx, uint32, rid, &rids, &num_rids);
1011         }
1012
1013         if (rids == NULL) {
1014                 d_printf("no rids\n");
1015                 goto done;
1016         }
1017
1018         wbc_status = wbcLookupRids(&dinfo->sid, num_rids, rids,
1019                                    (const char **)&domain_name, &names, &types);
1020         if (!WBC_ERROR_IS_OK(wbc_status)) {
1021                 d_printf("winbind_lookup_rids failed: %s\n",
1022                          wbcErrorString(wbc_status));
1023                 goto done;
1024         }
1025
1026         d_printf("Domain: %s\n", domain_name);
1027
1028         for (i=0; i<num_rids; i++) {
1029                 d_printf("%8d: %s (%s)\n", rids[i], names[i],
1030                          sid_type_lookup(types[i]));
1031         }
1032
1033         ret = true;
1034 done:
1035         if (dinfo) {
1036                 wbcFreeMemory(dinfo);
1037         }
1038         if (domain_name) {
1039                 wbcFreeMemory(domain_name);
1040         }
1041         if (names) {
1042                 wbcFreeMemory(names);
1043         }
1044         if (types) {
1045                 wbcFreeMemory(types);
1046         }
1047         TALLOC_FREE(mem_ctx);
1048         return ret;
1049 }
1050
1051 /* Convert string to sid */
1052
1053 static bool wbinfo_lookupname(const char *full_name)
1054 {
1055         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1056         struct wbcDomainSid sid;
1057         char *sid_str;
1058         enum wbcSidType type;
1059         fstring domain_name;
1060         fstring account_name;
1061
1062         /* Send off request */
1063
1064         parse_wbinfo_domain_user(full_name, domain_name,
1065                                  account_name);
1066
1067         wbc_status = wbcLookupName(domain_name, account_name,
1068                                    &sid, &type);
1069         if (!WBC_ERROR_IS_OK(wbc_status)) {
1070                 return false;
1071         }
1072
1073         wbc_status = wbcSidToString(&sid, &sid_str);
1074         if (!WBC_ERROR_IS_OK(wbc_status)) {
1075                 return false;
1076         }
1077
1078         /* Display response */
1079
1080         d_printf("%s %s (%d)\n", sid_str, sid_type_lookup(type), type);
1081
1082         wbcFreeMemory(sid_str);
1083
1084         return true;
1085 }
1086
1087 static char *wbinfo_prompt_pass(const char *prefix,
1088                                 const char *username)
1089 {
1090         char *prompt;
1091         const char *ret = NULL;
1092
1093         prompt = talloc_asprintf(talloc_tos(), "Enter %s's ", username);
1094         if (!prompt) {
1095                 return NULL;
1096         }
1097         if (prefix) {
1098                 prompt = talloc_asprintf_append(prompt, "%s ", prefix);
1099                 if (!prompt) {
1100                         return NULL;
1101                 }
1102         }
1103         prompt = talloc_asprintf_append(prompt, "password: ");
1104         if (!prompt) {
1105                 return NULL;
1106         }
1107
1108         ret = getpass(prompt);
1109         TALLOC_FREE(prompt);
1110
1111         return SMB_STRDUP(ret);
1112 }
1113
1114 /* Authenticate a user with a plaintext password */
1115
1116 static bool wbinfo_auth_krb5(char *username, const char *cctype, uint32 flags)
1117 {
1118         struct winbindd_request request;
1119         struct winbindd_response response;
1120         NSS_STATUS result;
1121         char *p;
1122         char *password;
1123
1124         /* Send off request */
1125
1126         ZERO_STRUCT(request);
1127         ZERO_STRUCT(response);
1128
1129         p = strchr(username, '%');
1130
1131         if (p) {
1132                 *p = 0;
1133                 fstrcpy(request.data.auth.user, username);
1134                 fstrcpy(request.data.auth.pass, p + 1);
1135                 *p = '%';
1136         } else {
1137                 fstrcpy(request.data.auth.user, username);
1138                 password = wbinfo_prompt_pass(NULL, username);
1139                 fstrcpy(request.data.auth.pass, password);
1140                 SAFE_FREE(password);
1141         }
1142
1143         request.flags = flags;
1144
1145         fstrcpy(request.data.auth.krb5_cc_type, cctype);
1146
1147         request.data.auth.uid = geteuid();
1148
1149         result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
1150
1151         /* Display response */
1152
1153         d_printf("plaintext kerberos password authentication for [%s] %s (requesting cctype: %s)\n",
1154                 username, (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", cctype);
1155
1156         if (response.data.auth.nt_status)
1157                 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
1158                          response.data.auth.nt_status_string,
1159                          response.data.auth.nt_status,
1160                          response.data.auth.error_string);
1161
1162         if (result == NSS_STATUS_SUCCESS) {
1163
1164                 if (request.flags & WBFLAG_PAM_INFO3_TEXT) {
1165                         if (response.data.auth.info3.user_flgs & NETLOGON_CACHED_ACCOUNT) {
1166                                 d_printf("user_flgs: NETLOGON_CACHED_ACCOUNT\n");
1167                         }
1168                 }
1169
1170                 if (response.data.auth.krb5ccname[0] != '\0') {
1171                         d_printf("credentials were put in: %s\n", response.data.auth.krb5ccname);
1172                 } else {
1173                         d_printf("no credentials cached\n");
1174                 }
1175         }
1176
1177         return result == NSS_STATUS_SUCCESS;
1178 }
1179
1180 /* Authenticate a user with a plaintext password */
1181
1182 static bool wbinfo_auth(char *username)
1183 {
1184         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1185         char *s = NULL;
1186         char *p = NULL;
1187         char *password = NULL;
1188         char *name = NULL;
1189
1190         if ((s = SMB_STRDUP(username)) == NULL) {
1191                 return false;
1192         }
1193
1194         if ((p = strchr(s, '%')) != NULL) {
1195                 *p = 0;
1196                 p++;
1197                 password = SMB_STRDUP(p);
1198         } else {
1199                 password = wbinfo_prompt_pass(NULL, username);
1200         }
1201
1202         name = s;
1203
1204         wbc_status = wbcAuthenticateUser(name, password);
1205
1206         d_printf("plaintext password authentication %s\n",
1207                  WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1208
1209 #if 0
1210         if (response.data.auth.nt_status)
1211                 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
1212                          response.data.auth.nt_status_string,
1213                          response.data.auth.nt_status,
1214                          response.data.auth.error_string);
1215 #endif
1216
1217         SAFE_FREE(s);
1218         SAFE_FREE(password);
1219
1220         return WBC_ERROR_IS_OK(wbc_status);
1221 }
1222
1223 /* Authenticate a user with a challenge/response */
1224
1225 static bool wbinfo_auth_crap(char *username)
1226 {
1227         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1228         struct wbcAuthUserParams params;
1229         struct wbcAuthUserInfo *info = NULL;
1230         struct wbcAuthErrorInfo *err = NULL;
1231         DATA_BLOB lm = data_blob_null;
1232         DATA_BLOB nt = data_blob_null;
1233         fstring name_user;
1234         fstring name_domain;
1235         char *pass;
1236         char *p;
1237
1238         p = strchr(username, '%');
1239
1240         if (p) {
1241                 *p = 0;
1242                 pass = SMB_STRDUP(p + 1);
1243         } else {
1244                 pass = wbinfo_prompt_pass(NULL, username);
1245         }
1246
1247         parse_wbinfo_domain_user(username, name_domain, name_user);
1248
1249         params.account_name     = name_user;
1250         params.domain_name      = name_domain;
1251         params.workstation_name = NULL;
1252
1253         params.flags            = 0;
1254         params.parameter_control= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT |
1255                                   WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
1256
1257         params.level            = WBC_AUTH_USER_LEVEL_RESPONSE;
1258
1259         generate_random_buffer(params.password.response.challenge, 8);
1260
1261         if (lp_client_ntlmv2_auth()) {
1262                 DATA_BLOB server_chal;
1263                 DATA_BLOB names_blob;
1264
1265                 server_chal = data_blob(params.password.response.challenge, 8);
1266
1267                 /* Pretend this is a login to 'us', for blob purposes */
1268                 names_blob = NTLMv2_generate_names_blob(global_myname(), lp_workgroup());
1269
1270                 if (!SMBNTLMv2encrypt(name_user, name_domain, pass, &server_chal,
1271                                       &names_blob,
1272                                       &lm, &nt, NULL)) {
1273                         data_blob_free(&names_blob);
1274                         data_blob_free(&server_chal);
1275                         SAFE_FREE(pass);
1276                         return false;
1277                 }
1278                 data_blob_free(&names_blob);
1279                 data_blob_free(&server_chal);
1280
1281         } else {
1282                 if (lp_client_lanman_auth()) {
1283                         bool ok;
1284                         lm = data_blob(NULL, 24);
1285                         ok = SMBencrypt(pass, params.password.response.challenge,
1286                                         lm.data);
1287                         if (!ok) {
1288                                 data_blob_free(&lm);
1289                         }
1290                 }
1291                 nt = data_blob(NULL, 24);
1292                 SMBNTencrypt(pass, params.password.response.challenge,
1293                              nt.data);
1294         }
1295
1296         params.password.response.nt_length      = nt.length;
1297         params.password.response.nt_data        = nt.data;
1298         params.password.response.lm_length      = lm.length;
1299         params.password.response.lm_data        = lm.data;
1300
1301         wbc_status = wbcAuthenticateUserEx(&params, &info, &err);
1302
1303         /* Display response */
1304
1305         d_printf("challenge/response password authentication %s\n",
1306                  WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1307
1308         if (wbc_status == WBC_ERR_AUTH_ERROR) {
1309                 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
1310                          err->nt_string,
1311                          err->nt_status,
1312                          err->display_string);
1313                 wbcFreeMemory(err);
1314         } else if (WBC_ERROR_IS_OK(wbc_status)) {
1315                 wbcFreeMemory(info);
1316         }
1317
1318         data_blob_free(&nt);
1319         data_blob_free(&lm);
1320         SAFE_FREE(pass);
1321
1322         return WBC_ERROR_IS_OK(wbc_status);
1323 }
1324
1325 /* Authenticate a user with a plaintext password and set a token */
1326
1327 static bool wbinfo_klog(char *username)
1328 {
1329         struct winbindd_request request;
1330         struct winbindd_response response;
1331         NSS_STATUS result;
1332         char *p;
1333
1334         /* Send off request */
1335
1336         ZERO_STRUCT(request);
1337         ZERO_STRUCT(response);
1338
1339         p = strchr(username, '%');
1340
1341         if (p) {
1342                 *p = 0;
1343                 fstrcpy(request.data.auth.user, username);
1344                 fstrcpy(request.data.auth.pass, p + 1);
1345                 *p = '%';
1346         } else {
1347                 fstrcpy(request.data.auth.user, username);
1348                 fstrcpy(request.data.auth.pass, getpass("Password: "));
1349         }
1350
1351         request.flags |= WBFLAG_PAM_AFS_TOKEN;
1352
1353         result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
1354
1355         /* Display response */
1356
1357         d_printf("plaintext password authentication %s\n",
1358                  (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
1359
1360         if (response.data.auth.nt_status)
1361                 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
1362                          response.data.auth.nt_status_string,
1363                          response.data.auth.nt_status,
1364                          response.data.auth.error_string);
1365
1366         if (result != NSS_STATUS_SUCCESS)
1367                 return false;
1368
1369         if (response.extra_data.data == NULL) {
1370                 d_fprintf(stderr, "Did not get token data\n");
1371                 return false;
1372         }
1373
1374         if (!afs_settoken_str((char *)response.extra_data.data)) {
1375                 d_fprintf(stderr, "Could not set token\n");
1376                 return false;
1377         }
1378
1379         d_printf("Successfully created AFS token\n");
1380         return true;
1381 }
1382
1383 /* Print domain users */
1384
1385 static bool print_domain_users(const char *domain)
1386 {
1387         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1388         uint32_t i;
1389         uint32_t num_users = 0;
1390         const char **users = NULL;
1391
1392         /* Send request to winbind daemon */
1393
1394         /* '.' is the special sign for our own domain */
1395         if (domain && strcmp(domain, ".") == 0) {
1396                 domain = get_winbind_domain();
1397         }
1398
1399         wbc_status = wbcListUsers(domain, &num_users, &users);
1400         if (!WBC_ERROR_IS_OK(wbc_status)) {
1401                 return false;
1402         }
1403
1404         for (i=0; i < num_users; i++) {
1405                 d_printf("%s\n", users[i]);
1406         }
1407
1408         wbcFreeMemory(users);
1409
1410         return true;
1411 }
1412
1413 /* Print domain groups */
1414
1415 static bool print_domain_groups(const char *domain)
1416 {
1417         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1418         uint32_t i;
1419         uint32_t num_groups = 0;
1420         const char **groups = NULL;
1421
1422         /* Send request to winbind daemon */
1423
1424         /* '.' is the special sign for our own domain */
1425         if (domain && strcmp(domain, ".") == 0) {
1426                 domain = get_winbind_domain();
1427         }
1428
1429         wbc_status = wbcListGroups(domain, &num_groups, &groups);
1430         if (!WBC_ERROR_IS_OK(wbc_status)) {
1431                 return false;
1432         }
1433
1434         for (i=0; i < num_groups; i++) {
1435                 d_printf("%s\n", groups[i]);
1436         }
1437
1438         wbcFreeMemory(groups);
1439
1440         return true;
1441 }
1442
1443 /* Set the authorised user for winbindd access in secrets.tdb */
1444
1445 static bool wbinfo_set_auth_user(char *username)
1446 {
1447         const char *password;
1448         char *p;
1449         fstring user, domain;
1450
1451         /* Separate into user and password */
1452
1453         parse_wbinfo_domain_user(username, domain, user);
1454
1455         p = strchr(user, '%');
1456
1457         if (p != NULL) {
1458                 *p = 0;
1459                 password = p+1;
1460         } else {
1461                 char *thepass = getpass("Password: ");
1462                 if (thepass) {
1463                         password = thepass;
1464                 } else
1465                         password = "";
1466         }
1467
1468         /* Store or remove DOMAIN\username%password in secrets.tdb */
1469
1470         secrets_init();
1471
1472         if (user[0]) {
1473
1474                 if (!secrets_store(SECRETS_AUTH_USER, user,
1475                                    strlen(user) + 1)) {
1476                         d_fprintf(stderr, "error storing username\n");
1477                         return false;
1478                 }
1479
1480                 /* We always have a domain name added by the
1481                    parse_wbinfo_domain_user() function. */
1482
1483                 if (!secrets_store(SECRETS_AUTH_DOMAIN, domain,
1484                                    strlen(domain) + 1)) {
1485                         d_fprintf(stderr, "error storing domain name\n");
1486                         return false;
1487                 }
1488
1489         } else {
1490                 secrets_delete(SECRETS_AUTH_USER);
1491                 secrets_delete(SECRETS_AUTH_DOMAIN);
1492         }
1493
1494         if (password[0]) {
1495
1496                 if (!secrets_store(SECRETS_AUTH_PASSWORD, password,
1497                                    strlen(password) + 1)) {
1498                         d_fprintf(stderr, "error storing password\n");
1499                         return false;
1500                 }
1501
1502         } else
1503                 secrets_delete(SECRETS_AUTH_PASSWORD);
1504
1505         return true;
1506 }
1507
1508 static void wbinfo_get_auth_user(void)
1509 {
1510         char *user, *domain, *password;
1511
1512         /* Lift data from secrets file */
1513
1514         secrets_fetch_ipc_userpass(&user, &domain, &password);
1515
1516         if ((!user || !*user) && (!domain || !*domain ) && (!password || !*password)){
1517
1518                 SAFE_FREE(user);
1519                 SAFE_FREE(domain);
1520                 SAFE_FREE(password);
1521                 d_printf("No authorised user configured\n");
1522                 return;
1523         }
1524
1525         /* Pretty print authorised user info */
1526
1527         d_printf("%s%s%s%s%s\n", domain ? domain : "", domain ? lp_winbind_separator(): "",
1528                  user, password ? "%" : "", password ? password : "");
1529
1530         SAFE_FREE(user);
1531         SAFE_FREE(domain);
1532         SAFE_FREE(password);
1533 }
1534
1535 static bool wbinfo_ping(void)
1536 {
1537         wbcErr wbc_status;
1538
1539         wbc_status = wbcPing();
1540
1541         /* Display response */
1542
1543         d_printf("Ping to winbindd %s\n",
1544                  WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1545
1546         return WBC_ERROR_IS_OK(wbc_status);
1547 }
1548
1549 static bool wbinfo_change_user_password(const char *username)
1550 {
1551         wbcErr wbc_status;
1552         char *old_password = NULL;
1553         char *new_password = NULL;
1554
1555         old_password = wbinfo_prompt_pass("old", username);
1556         new_password = wbinfo_prompt_pass("new", username);
1557
1558         wbc_status = wbcChangeUserPassword(username, old_password, new_password);
1559
1560         /* Display response */
1561
1562         d_printf("Password change for user %s %s\n", username,
1563                 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1564
1565         SAFE_FREE(old_password);
1566         SAFE_FREE(new_password);
1567
1568         return WBC_ERROR_IS_OK(wbc_status);
1569 }
1570
1571 /* Main program */
1572
1573 enum {
1574         OPT_SET_AUTH_USER = 1000,
1575         OPT_GET_AUTH_USER,
1576         OPT_DOMAIN_NAME,
1577         OPT_SEQUENCE,
1578         OPT_GETDCNAME,
1579         OPT_DSGETDCNAME,
1580         OPT_USERDOMGROUPS,
1581         OPT_USERSIDS,
1582         OPT_ALLOCATE_UID,
1583         OPT_ALLOCATE_GID,
1584         OPT_SET_UID_MAPPING,
1585         OPT_SET_GID_MAPPING,
1586         OPT_REMOVE_UID_MAPPING,
1587         OPT_REMOVE_GID_MAPPING,
1588         OPT_SEPARATOR,
1589         OPT_LIST_ALL_DOMAINS,
1590         OPT_LIST_OWN_DOMAIN,
1591         OPT_UID_INFO,
1592         OPT_USER_SIDINFO,
1593         OPT_GROUP_INFO,
1594         OPT_GID_INFO,
1595         OPT_VERBOSE,
1596         OPT_ONLINESTATUS,
1597         OPT_CHANGE_USER_PASSWORD,
1598         OPT_SID_TO_FULLNAME
1599 };
1600
1601 int main(int argc, char **argv, char **envp)
1602 {
1603         int opt;
1604         TALLOC_CTX *frame = talloc_stackframe();
1605         poptContext pc;
1606         static char *string_arg;
1607         char *string_subarg = NULL;
1608         static char *opt_domain_name;
1609         static int int_arg;
1610         int int_subarg = -1;
1611         int result = 1;
1612         bool verbose = false;
1613
1614         struct poptOption long_options[] = {
1615                 POPT_AUTOHELP
1616
1617                 /* longName, shortName, argInfo, argPtr, value, descrip,
1618                    argDesc */
1619
1620                 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
1621                 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
1622                 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1623                 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
1624                 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
1625                 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
1626                 { "sid-to-fullname", 0, POPT_ARG_STRING, &string_arg,
1627                   OPT_SID_TO_FULLNAME, "Converts sid to fullname", "SID" },
1628                 { "lookup-rids", 'R', POPT_ARG_STRING, &string_arg, 'R', "Converts RIDs to names", "RIDs" },
1629                 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
1630                 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
1631                 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
1632                 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
1633                 { "allocate-uid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_UID,
1634                   "Get a new UID out of idmap" },
1635                 { "allocate-gid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_GID,
1636                   "Get a new GID out of idmap" },
1637                 { "set-uid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_SET_UID_MAPPING, "Create or modify uid to sid mapping in idmap", "UID,SID" },
1638                 { "set-gid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_SET_GID_MAPPING, "Create or modify gid to sid mapping in idmap", "GID,SID" },
1639                 { "remove-uid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_REMOVE_UID_MAPPING, "Remove uid to sid mapping in idmap", "UID,SID" },
1640                 { "remove-gid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_REMOVE_GID_MAPPING, "Remove gid to sid mapping in idmap", "GID,SID" },
1641                 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
1642                 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
1643                 { "all-domains", 0, POPT_ARG_NONE, 0, OPT_LIST_ALL_DOMAINS, "List all domains (trusted and own domain)" },
1644                 { "own-domain", 0, POPT_ARG_NONE, 0, OPT_LIST_OWN_DOMAIN, "List own domain" },
1645                 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
1646                 { "online-status", 0, POPT_ARG_NONE, 0, OPT_ONLINESTATUS, "Show whether domains are marked as online or offline"},
1647                 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
1648                 { "user-info", 'i', POPT_ARG_STRING, &string_arg, 'i', "Get user info", "USER" },
1649                 { "uid-info", 0, POPT_ARG_INT, &int_arg, OPT_UID_INFO, "Get user info from uid", "UID" },
1650                 { "group-info", 0, POPT_ARG_STRING, &string_arg, OPT_GROUP_INFO, "Get group info", "GROUP" },
1651                 { "user-sidinfo", 0, POPT_ARG_STRING, &string_arg, OPT_USER_SIDINFO, "Get user info from sid", "SID" },
1652                 { "gid-info", 0, POPT_ARG_INT, &int_arg, OPT_GID_INFO, "Get group info from gid", "GID" },
1653                 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
1654                 { "user-domgroups", 0, POPT_ARG_STRING, &string_arg,
1655                   OPT_USERDOMGROUPS, "Get user domain groups", "SID" },
1656                 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
1657                 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
1658                 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
1659                 { "getdcname", 0, POPT_ARG_STRING, &string_arg, OPT_GETDCNAME,
1660                   "Get a DC name for a foreign domain", "domainname" },
1661                 { "dsgetdcname", 0, POPT_ARG_STRING, &string_arg, OPT_DSGETDCNAME, "Find a DC for a domain", "domainname" },
1662                 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
1663                 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
1664                 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
1665 #ifdef WITH_FAKE_KASERVER
1666                 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
1667 #endif
1668 #ifdef HAVE_KRB5
1669                 { "krb5auth", 'K', POPT_ARG_STRING, &string_arg, 'K', "authenticate user using Kerberos", "user%password" },
1670                         /* destroys wbinfo --help output */
1671                         /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
1672 #endif
1673                 { "separator", 0, POPT_ARG_NONE, 0, OPT_SEPARATOR, "Get the active winbind separator", NULL },
1674                 { "verbose", 0, POPT_ARG_NONE, 0, OPT_VERBOSE, "Print additional information per command", NULL },
1675                 { "change-user-password", 0, POPT_ARG_STRING, &string_arg, OPT_CHANGE_USER_PASSWORD, "Change the password for a user", NULL },
1676                 POPT_COMMON_CONFIGFILE
1677                 POPT_COMMON_VERSION
1678                 POPT_TABLEEND
1679         };
1680
1681         /* Samba client initialisation */
1682         load_case_tables();
1683
1684
1685         /* Parse options */
1686
1687         pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
1688
1689         /* Parse command line options */
1690
1691         if (argc == 1) {
1692                 poptPrintHelp(pc, stderr, 0);
1693                 return 1;
1694         }
1695
1696         while((opt = poptGetNextOpt(pc)) != -1) {
1697                 /* get the generic configuration parameters like --domain */
1698                 switch (opt) {
1699                 case OPT_VERBOSE:
1700                         verbose = True;
1701                         break;
1702                 }
1703         }
1704
1705         poptFreeContext(pc);
1706
1707         if (!lp_load(get_dyn_CONFIGFILE(), true, false, false, true)) {
1708                 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
1709                         get_dyn_CONFIGFILE(), strerror(errno));
1710                 exit(1);
1711         }
1712
1713         if (!init_names())
1714                 return 1;
1715
1716         load_interfaces();
1717
1718         pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1719                             POPT_CONTEXT_KEEP_FIRST);
1720
1721         while((opt = poptGetNextOpt(pc)) != -1) {
1722                 switch (opt) {
1723                 case 'u':
1724                         if (!print_domain_users(opt_domain_name)) {
1725                                 d_fprintf(stderr, "Error looking up domain users\n");
1726                                 goto done;
1727                         }
1728                         break;
1729                 case 'g':
1730                         if (!print_domain_groups(opt_domain_name)) {
1731                                 d_fprintf(stderr, "Error looking up domain groups\n");
1732                                 goto done;
1733                         }
1734                         break;
1735                 case 's':
1736                         if (!wbinfo_lookupsid(string_arg)) {
1737                                 d_fprintf(stderr, "Could not lookup sid %s\n", string_arg);
1738                                 goto done;
1739                         }
1740                         break;
1741                 case OPT_SID_TO_FULLNAME:
1742                         if (!wbinfo_lookupsid_fullname(string_arg)) {
1743                                 d_fprintf(stderr, "Could not lookup sid %s\n",
1744                                           string_arg);
1745                                 goto done;
1746                         }
1747                         break;
1748                 case 'R':
1749                         if (!wbinfo_lookuprids(opt_domain_name, string_arg)) {
1750                                 d_fprintf(stderr, "Could not lookup RIDs %s\n", string_arg);
1751                                 goto done;
1752                         }
1753                         break;
1754                 case 'n':
1755                         if (!wbinfo_lookupname(string_arg)) {
1756                                 d_fprintf(stderr, "Could not lookup name %s\n", string_arg);
1757                                 goto done;
1758                         }
1759                         break;
1760                 case 'N':
1761                         if (!wbinfo_wins_byname(string_arg)) {
1762                                 d_fprintf(stderr, "Could not lookup WINS by name %s\n", string_arg);
1763                                 goto done;
1764                         }
1765                         break;
1766                 case 'I':
1767                         if (!wbinfo_wins_byip(string_arg)) {
1768                                 d_fprintf(stderr, "Could not lookup WINS by IP %s\n", string_arg);
1769                                 goto done;
1770                         }
1771                         break;
1772                 case 'U':
1773                         if (!wbinfo_uid_to_sid(int_arg)) {
1774                                 d_fprintf(stderr, "Could not convert uid %d to sid\n", int_arg);
1775                                 goto done;
1776                         }
1777                         break;
1778                 case 'G':
1779                         if (!wbinfo_gid_to_sid(int_arg)) {
1780                                 d_fprintf(stderr, "Could not convert gid %d to sid\n",
1781                                        int_arg);
1782                                 goto done;
1783                         }
1784                         break;
1785                 case 'S':
1786                         if (!wbinfo_sid_to_uid(string_arg)) {
1787                                 d_fprintf(stderr, "Could not convert sid %s to uid\n",
1788                                        string_arg);
1789                                 goto done;
1790                         }
1791                         break;
1792                 case 'Y':
1793                         if (!wbinfo_sid_to_gid(string_arg)) {
1794                                 d_fprintf(stderr, "Could not convert sid %s to gid\n",
1795                                        string_arg);
1796                                 goto done;
1797                         }
1798                         break;
1799                 case OPT_ALLOCATE_UID:
1800                         if (!wbinfo_allocate_uid()) {
1801                                 d_fprintf(stderr, "Could not allocate a uid\n");
1802                                 goto done;
1803                         }
1804                         break;
1805                 case OPT_ALLOCATE_GID:
1806                         if (!wbinfo_allocate_gid()) {
1807                                 d_fprintf(stderr, "Could not allocate a gid\n");
1808                                 goto done;
1809                         }
1810                         break;
1811                 case OPT_SET_UID_MAPPING:
1812                         if (!parse_mapping_arg(string_arg, &int_subarg,
1813                                 &string_subarg) ||
1814                             !wbinfo_set_uid_mapping(int_subarg, string_subarg))
1815                         {
1816                                 d_fprintf(stderr, "Could not create or modify "
1817                                           "uid to sid mapping\n");
1818                                 goto done;
1819                         }
1820                         break;
1821                 case OPT_SET_GID_MAPPING:
1822                         if (!parse_mapping_arg(string_arg, &int_subarg,
1823                                 &string_subarg) ||
1824                             !wbinfo_set_gid_mapping(int_subarg, string_subarg))
1825                         {
1826                                 d_fprintf(stderr, "Could not create or modify "
1827                                           "gid to sid mapping\n");
1828                                 goto done;
1829                         }
1830                         break;
1831                 case OPT_REMOVE_UID_MAPPING:
1832                         if (!parse_mapping_arg(string_arg, &int_subarg,
1833                                 &string_subarg) ||
1834                             !wbinfo_remove_uid_mapping(int_subarg,
1835                                 string_subarg))
1836                         {
1837                                 d_fprintf(stderr, "Could not remove uid to sid "
1838                                     "mapping\n");
1839                                 goto done;
1840                         }
1841                         break;
1842                 case OPT_REMOVE_GID_MAPPING:
1843                         if (!parse_mapping_arg(string_arg, &int_subarg,
1844                                 &string_subarg) ||
1845                             !wbinfo_remove_gid_mapping(int_subarg,
1846                                 string_subarg))
1847                         {
1848                                 d_fprintf(stderr, "Could not remove gid to sid "
1849                                     "mapping\n");
1850                                 goto done;
1851                         }
1852                         break;
1853                 case 't':
1854                         if (!wbinfo_check_secret()) {
1855                                 d_fprintf(stderr, "Could not check secret\n");
1856                                 goto done;
1857                         }
1858                         break;
1859                 case 'm':
1860                         if (!wbinfo_list_domains(false, verbose)) {
1861                                 d_fprintf(stderr, "Could not list trusted domains\n");
1862                                 goto done;
1863                         }
1864                         break;
1865                 case OPT_SEQUENCE:
1866                         if (!wbinfo_show_sequence(opt_domain_name)) {
1867                                 d_fprintf(stderr, "Could not show sequence numbers\n");
1868                                 goto done;
1869                         }
1870                         break;
1871                 case OPT_ONLINESTATUS:
1872                         if (!wbinfo_show_onlinestatus(opt_domain_name)) {
1873                                 d_fprintf(stderr, "Could not show online-status\n");
1874                                 goto done;
1875                         }
1876                         break;
1877                 case 'D':
1878                         if (!wbinfo_domain_info(string_arg)) {
1879                                 d_fprintf(stderr, "Could not get domain info\n");
1880                                 goto done;
1881                         }
1882                         break;
1883                 case 'i':
1884                         if (!wbinfo_get_userinfo(string_arg)) {
1885                                 d_fprintf(stderr, "Could not get info for user %s\n",
1886                                                   string_arg);
1887                                 goto done;
1888                         }
1889                         break;
1890                 case OPT_USER_SIDINFO:
1891                         if ( !wbinfo_get_user_sidinfo(string_arg)) {
1892                                 d_fprintf(stderr, "Could not get info for user sid %s\n",
1893                                     string_arg);
1894                                 goto done;
1895                         }
1896                         break;
1897                 case OPT_UID_INFO:
1898                         if ( !wbinfo_get_uidinfo(int_arg)) {
1899                                 d_fprintf(stderr, "Could not get info for uid "
1900                                                 "%d\n", int_arg);
1901                                 goto done;
1902                         }
1903                         break;
1904                 case OPT_GROUP_INFO:
1905                         if ( !wbinfo_get_groupinfo(string_arg)) {
1906                                 d_fprintf(stderr, "Could not get info for "
1907                                           "group %s\n", string_arg);
1908                                 goto done;
1909                         }
1910                         break;
1911                 case OPT_GID_INFO:
1912                         if ( !wbinfo_get_gidinfo(int_arg)) {
1913                                 d_fprintf(stderr, "Could not get info for gid "
1914                                                 "%d\n", int_arg);
1915                                 goto done;
1916                         }
1917                         break;
1918                 case 'r':
1919                         if (!wbinfo_get_usergroups(string_arg)) {
1920                                 d_fprintf(stderr, "Could not get groups for user %s\n",
1921                                        string_arg);
1922                                 goto done;
1923                         }
1924                         break;
1925                 case OPT_USERSIDS:
1926                         if (!wbinfo_get_usersids(string_arg)) {
1927                                 d_fprintf(stderr, "Could not get group SIDs for user SID %s\n",
1928                                        string_arg);
1929                                 goto done;
1930                         }
1931                         break;
1932                 case OPT_USERDOMGROUPS:
1933                         if (!wbinfo_get_userdomgroups(string_arg)) {
1934                                 d_fprintf(stderr, "Could not get user's domain groups "
1935                                          "for user SID %s\n", string_arg);
1936                                 goto done;
1937                         }
1938                         break;
1939                 case 'a': {
1940                                 bool got_error = false;
1941
1942                                 if (!wbinfo_auth(string_arg)) {
1943                                         d_fprintf(stderr, "Could not authenticate user %s with "
1944                                                 "plaintext password\n", string_arg);
1945                                         got_error = true;
1946                                 }
1947
1948                                 if (!wbinfo_auth_crap(string_arg)) {
1949                                         d_fprintf(stderr, "Could not authenticate user %s with "
1950                                                 "challenge/response\n", string_arg);
1951                                         got_error = true;
1952                                 }
1953
1954                                 if (got_error)
1955                                         goto done;
1956                                 break;
1957                         }
1958                 case 'K': {
1959                                 uint32 flags =  WBFLAG_PAM_KRB5 |
1960                                                 WBFLAG_PAM_CACHED_LOGIN |
1961                                                 WBFLAG_PAM_FALLBACK_AFTER_KRB5 |
1962                                                 WBFLAG_PAM_INFO3_TEXT;
1963
1964                                 if (!wbinfo_auth_krb5(string_arg, "FILE", flags)) {
1965                                         d_fprintf(stderr, "Could not authenticate user [%s] with "
1966                                                 "Kerberos (ccache: %s)\n", string_arg, "FILE");
1967                                         goto done;
1968                                 }
1969                                 break;
1970                         }
1971                 case 'k':
1972                         if (!wbinfo_klog(string_arg)) {
1973                                 d_fprintf(stderr, "Could not klog user\n");
1974                                 goto done;
1975                         }
1976                         break;
1977                 case 'p':
1978                         if (!wbinfo_ping()) {
1979                                 d_fprintf(stderr, "could not ping winbindd!\n");
1980                                 goto done;
1981                         }
1982                         break;
1983                 case OPT_SET_AUTH_USER:
1984                         if (!wbinfo_set_auth_user(string_arg)) {
1985                                 goto done;
1986                         }
1987                         break;
1988                 case OPT_GET_AUTH_USER:
1989                         wbinfo_get_auth_user();
1990                         break;
1991                 case OPT_GETDCNAME:
1992                         if (!wbinfo_getdcname(string_arg)) {
1993                                 goto done;
1994                         }
1995                         break;
1996                 case OPT_DSGETDCNAME:
1997                         if (!wbinfo_dsgetdcname(string_arg, 0)) {
1998                                 goto done;
1999                         }
2000                         break;
2001                 case OPT_SEPARATOR: {
2002                         const char sep = winbind_separator_int(true);
2003                         if ( !sep ) {
2004                                 goto done;
2005                         }
2006                         d_printf("%c\n", sep);
2007                         break;
2008                 }
2009                 case OPT_LIST_ALL_DOMAINS:
2010                         if (!wbinfo_list_domains(true, verbose)) {
2011                                 goto done;
2012                         }
2013                         break;
2014                 case OPT_LIST_OWN_DOMAIN:
2015                         if (!wbinfo_list_own_domain()) {
2016                                 goto done;
2017                         }
2018                         break;
2019                 case OPT_CHANGE_USER_PASSWORD:
2020                         if (!wbinfo_change_user_password(string_arg)) {
2021                                 d_fprintf(stderr, "Could not change user password "
2022                                          "for user %s\n", string_arg);
2023                                 goto done;
2024                         }
2025                         break;
2026
2027                 /* generic configuration options */
2028                 case OPT_DOMAIN_NAME:
2029                         break;
2030                 case OPT_VERBOSE:
2031                         break;
2032                 default:
2033                         d_fprintf(stderr, "Invalid option\n");
2034                         poptPrintHelp(pc, stderr, 0);
2035                         goto done;
2036                 }
2037         }
2038
2039         result = 0;
2040
2041         /* Exit code */
2042
2043  done:
2044         talloc_destroy(frame);
2045
2046         poptFreeContext(pc);
2047         return result;
2048 }