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