2 Unix SMB/CIFS implementation.
4 Winbind status program.
6 Copyright (C) Tim Potter 2000
7 Copyright (C) Andrew Bartlett 2002
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.
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.
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.
28 /* Prototypes from common.h */
30 NSS_STATUS winbindd_request(int req_type,
31 struct winbindd_request *request,
32 struct winbindd_response *response);
34 static char get_winbind_separator(void)
36 struct winbindd_response response;
37 char winbind_separator;
39 ZERO_STRUCT(response);
41 /* Send off request */
43 if (winbindd_request(WINBINDD_INFO, NULL, &response) !=
45 printf("could not obtain winbind seperator!\n");
49 winbind_separator = response.data.info.winbind_separator;
51 if (!winbind_separator) {
52 printf("winbind separator was NULL!\n");
56 return winbind_separator;
60 static char *get_winbind_domain(void)
62 struct winbindd_response response;
63 static fstring winbind_domain;
65 ZERO_STRUCT(response);
67 /* Send off request */
69 if (winbindd_request(WINBINDD_DOMAIN_NAME, NULL, &response) !=
71 printf("could not obtain winbind domain name!\n");
75 fstrcpy(winbind_domain, response.data.domain_name);
77 return winbind_domain;
81 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
82 form DOMAIN/user into a domain and a user */
84 static BOOL parse_wbinfo_domain_user(const char *domuser, fstring domain, fstring user)
87 char *p = strchr(domuser,get_winbind_separator());
90 fstrcpy(user, domuser);
91 fstrcpy(domain, get_winbind_domain());
96 fstrcpy(domain, domuser);
97 domain[PTR_DIFF(p, domuser)] = 0;
102 /* List groups a user is a member of */
104 static BOOL wbinfo_get_usergroups(char *user)
106 struct winbindd_request request;
107 struct winbindd_response response;
111 ZERO_STRUCT(response);
115 fstrcpy(request.data.username, user);
117 result = winbindd_request(WINBINDD_GETGROUPS, &request, &response);
119 if (result != NSS_STATUS_SUCCESS)
122 for (i = 0; i < response.data.num_entries; i++)
123 printf("%d\n", (int)((gid_t *)response.extra_data)[i]);
125 SAFE_FREE(response.extra_data);
130 /* List trusted domains */
132 static BOOL wbinfo_list_domains(void)
134 struct winbindd_response response;
137 ZERO_STRUCT(response);
141 if (winbindd_request(WINBINDD_LIST_TRUSTDOM, NULL, &response) !=
142 NSS_STATUS_SUCCESS) {
146 /* Display response */
148 if (response.extra_data) {
149 char *extra_data = (char *)response.extra_data;
151 while(next_token(&extra_data, name, ",", sizeof(fstring)))
152 printf("%s\n", name);
154 SAFE_FREE(response.extra_data);
161 /* show sequence numbers */
162 static BOOL wbinfo_show_sequence(void)
164 struct winbindd_response response;
167 ZERO_STRUCT(response);
170 if (winbindd_request(WINBINDD_SHOW_SEQUENCE, NULL, &response) !=
171 NSS_STATUS_SUCCESS) {
175 /* Display response */
176 if (response.extra_data) {
177 char *extra_data = (char *)response.extra_data;
178 printf("%s", extra_data);
179 SAFE_FREE(response.extra_data);
185 /* Check trust account password */
187 static BOOL wbinfo_check_secret(void)
189 struct winbindd_response response;
192 ZERO_STRUCT(response);
194 result = winbindd_request(WINBINDD_CHECK_MACHACC, NULL, &response) ==
199 if (response.data.num_entries == 0) {
200 printf("Secret is good\n");
202 printf("Secret is bad\n0x%08x\n",
203 response.data.num_entries);
212 /* Convert uid to sid */
214 static BOOL wbinfo_uid_to_sid(uid_t uid)
216 struct winbindd_request request;
217 struct winbindd_response response;
219 ZERO_STRUCT(request);
220 ZERO_STRUCT(response);
224 request.data.uid = uid;
225 if (winbindd_request(WINBINDD_UID_TO_SID, &request, &response) !=
226 NSS_STATUS_SUCCESS) {
230 /* Display response */
232 printf("%s\n", response.data.sid.sid);
237 /* Convert gid to sid */
239 static BOOL wbinfo_gid_to_sid(gid_t gid)
241 struct winbindd_request request;
242 struct winbindd_response response;
244 ZERO_STRUCT(request);
245 ZERO_STRUCT(response);
249 request.data.gid = gid;
250 if (winbindd_request(WINBINDD_GID_TO_SID, &request, &response) !=
251 NSS_STATUS_SUCCESS) {
255 /* Display response */
257 printf("%s\n", response.data.sid.sid);
262 /* Convert sid to uid */
264 static BOOL wbinfo_sid_to_uid(char *sid)
266 struct winbindd_request request;
267 struct winbindd_response response;
269 ZERO_STRUCT(request);
270 ZERO_STRUCT(response);
274 fstrcpy(request.data.sid, sid);
275 if (winbindd_request(WINBINDD_SID_TO_UID, &request, &response) !=
276 NSS_STATUS_SUCCESS) {
280 /* Display response */
282 printf("%d\n", (int)response.data.uid);
287 static BOOL wbinfo_sid_to_gid(char *sid)
289 struct winbindd_request request;
290 struct winbindd_response response;
292 ZERO_STRUCT(request);
293 ZERO_STRUCT(response);
297 fstrcpy(request.data.sid, sid);
298 if (winbindd_request(WINBINDD_SID_TO_GID, &request, &response) !=
299 NSS_STATUS_SUCCESS) {
303 /* Display response */
305 printf("%d\n", (int)response.data.gid);
310 /* Convert sid to string */
312 static BOOL wbinfo_lookupsid(char *sid)
314 struct winbindd_request request;
315 struct winbindd_response response;
317 ZERO_STRUCT(request);
318 ZERO_STRUCT(response);
320 /* Send off request */
322 fstrcpy(request.data.sid, sid);
323 if (winbindd_request(WINBINDD_LOOKUPSID, &request, &response) !=
324 NSS_STATUS_SUCCESS) {
328 /* Display response */
330 printf("[%s]\\[%s] %d\n", response.data.name.dom_name, response.data.name.name, response.data.name.type);
335 /* Convert string to sid */
337 static BOOL wbinfo_lookupname(char *name)
339 struct winbindd_request request;
340 struct winbindd_response response;
342 /* Send off request */
344 ZERO_STRUCT(request);
345 ZERO_STRUCT(response);
347 parse_wbinfo_domain_user(name, request.data.name.dom_name, request.data.name.name);
349 if (winbindd_request(WINBINDD_LOOKUPNAME, &request, &response) !=
350 NSS_STATUS_SUCCESS) {
354 /* Display response */
356 printf("%s %d\n", response.data.sid.sid, response.data.sid.type);
361 /* Authenticate a user with a plaintext password */
363 static BOOL wbinfo_auth(char *username)
365 struct winbindd_request request;
366 struct winbindd_response response;
370 /* Send off request */
372 ZERO_STRUCT(request);
373 ZERO_STRUCT(response);
375 p = strchr(username, '%');
379 fstrcpy(request.data.auth.user, username);
380 fstrcpy(request.data.auth.pass, p + 1);
383 fstrcpy(request.data.auth.user, username);
385 result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
387 /* Display response */
389 printf("plaintext password authentication %s\n",
390 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
392 return result == NSS_STATUS_SUCCESS;
395 /* Authenticate a user with a challenge/response */
397 static BOOL wbinfo_auth_crap(char *username)
399 struct winbindd_request request;
400 struct winbindd_response response;
407 /* Send off request */
409 ZERO_STRUCT(request);
410 ZERO_STRUCT(response);
412 p = strchr(username, '%');
416 fstrcpy(pass, p + 1);
419 parse_wbinfo_domain_user(username, name_domain, name_user);
421 fstrcpy(request.data.auth_crap.user, name_user);
423 fstrcpy(request.data.auth_crap.domain, name_domain);
425 generate_random_buffer(request.data.auth_crap.chal, 8, False);
427 SMBencrypt((uchar *)pass, request.data.auth_crap.chal,
428 (uchar *)request.data.auth_crap.lm_resp);
429 SMBNTencrypt((uchar *)pass, request.data.auth_crap.chal,
430 (uchar *)request.data.auth_crap.nt_resp);
432 request.data.auth_crap.lm_resp_len = 24;
433 request.data.auth_crap.nt_resp_len = 24;
435 result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
437 /* Display response */
439 printf("challenge/response password authentication %s\n",
440 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
442 return result == NSS_STATUS_SUCCESS;
445 /* Print domain users */
447 static BOOL print_domain_users(void)
449 struct winbindd_response response;
453 /* Send request to winbind daemon */
455 ZERO_STRUCT(response);
457 if (winbindd_request(WINBINDD_LIST_USERS, NULL, &response) !=
458 NSS_STATUS_SUCCESS) {
462 /* Look through extra data */
464 if (!response.extra_data)
467 extra_data = (char *)response.extra_data;
469 while(next_token(&extra_data, name, ",", sizeof(fstring)))
470 printf("%s\n", name);
472 SAFE_FREE(response.extra_data);
477 /* Print domain groups */
479 static BOOL print_domain_groups(void)
481 struct winbindd_response response;
485 ZERO_STRUCT(response);
487 if (winbindd_request(WINBINDD_LIST_GROUPS, NULL, &response) !=
488 NSS_STATUS_SUCCESS) {
492 /* Look through extra data */
494 if (!response.extra_data)
497 extra_data = (char *)response.extra_data;
499 while(next_token(&extra_data, name, ",", sizeof(fstring)))
500 printf("%s\n", name);
502 SAFE_FREE(response.extra_data);
507 /* Set the authorised user for winbindd access in secrets.tdb */
509 static BOOL wbinfo_set_auth_user(char *username)
513 /* Separate into user and password */
515 password = strchr(username, '%');
523 /* Store in secrets.tdb */
525 if (!secrets_store(SECRETS_AUTH_USER, username, strlen(username) + 1) ||
526 !secrets_store(SECRETS_AUTH_PASSWORD, password, strlen(password) + 1)) {
527 fprintf(stderr, "error storing authenticated user info\n");
534 static BOOL wbinfo_ping(void)
538 result = winbindd_request(WINBINDD_PING, NULL, NULL);
540 /* Display response */
542 printf("'ping' to winbindd %s\n",
543 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
545 return result == NSS_STATUS_SUCCESS;
548 /* Print program usage */
550 static void usage(void)
552 printf("Usage: wbinfo -ug | -n name | -sSY sid | -UG uid/gid | -tm "
553 "| -a user%%password\n");
554 printf("\t-u\t\t\tlists all domain users\n");
555 printf("\t-g\t\t\tlists all domain groups\n");
556 printf("\t-n name\t\t\tconverts name to sid\n");
557 printf("\t-s sid\t\t\tconverts sid to name\n");
558 printf("\t-U uid\t\t\tconverts uid to sid\n");
559 printf("\t-G gid\t\t\tconverts gid to sid\n");
560 printf("\t-S sid\t\t\tconverts sid to uid\n");
561 printf("\t-Y sid\t\t\tconverts sid to gid\n");
562 printf("\t-t\t\t\tcheck shared secret\n");
563 printf("\t-m\t\t\tlist trusted domains\n");
564 printf("\t-r user\t\t\tget user groups\n");
565 printf("\t-a user%%password\tauthenticate user\n");
566 printf("\t-p 'ping' winbindd to see if it is alive\n");
567 printf("\t--sequence\t\tshow sequence numbers of all domains\n");
573 OPT_SET_AUTH_USER = 1000
576 int main(int argc, char **argv)
578 extern pstring global_myname;
582 static char *string_arg;
584 BOOL got_command = False;
585 enum {OPT_SEQUENCE=1};
587 struct poptOption long_options[] = {
589 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
590 { "help", 'h', POPT_ARG_NONE, 0, 'h' },
591 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u' },
592 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g' },
593 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n' },
594 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's' },
595 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U' },
596 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G' },
597 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S' },
598 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y' },
599 { "check-secret", 't', POPT_ARG_NONE, 0, 't' },
600 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm' },
601 { "sequence", OPT_SEQUENCE, POPT_ARG_NONE, 0, OPT_SEQUENCE },
602 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r' },
603 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a' },
604 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER },
605 { "ping", 'p', POPT_ARG_NONE, 0, 'p' },
609 /* Samba client initialisation */
611 if (!*global_myname) {
614 fstrcpy(global_myname, myhostname());
615 p = strchr(global_myname, '.');
621 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
622 fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
623 dyn_CONFIGFILE, strerror(errno));
629 /* Parse command line options */
638 pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
640 while((opt = poptGetNextOpt(pc)) != -1) {
642 fprintf(stderr, "No more than one command may be specified "
649 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
650 POPT_CONTEXT_KEEP_FIRST);
652 while((opt = poptGetNextOpt(pc)) != -1) {
658 if (!print_domain_users()) {
659 printf("Error looking up domain users\n");
664 if (!print_domain_groups()) {
665 printf("Error looking up domain groups\n");
670 if (!wbinfo_lookupsid(string_arg)) {
671 printf("Could not lookup sid %s\n", string_arg);
676 if (!wbinfo_lookupname(string_arg)) {
677 printf("Could not lookup name %s\n", string_arg);
682 if (!wbinfo_uid_to_sid(int_arg)) {
683 printf("Could not convert uid %d to sid\n", int_arg);
688 if (!wbinfo_gid_to_sid(int_arg)) {
689 printf("Could not convert gid %d to sid\n",
695 if (!wbinfo_sid_to_uid(string_arg)) {
696 printf("Could not convert sid %s to uid\n",
702 if (!wbinfo_sid_to_gid(string_arg)) {
703 printf("Could not convert sid %s to gid\n",
709 if (!wbinfo_check_secret()) {
710 printf("Could not check secret\n");
715 if (!wbinfo_list_domains()) {
716 printf("Could not list trusted domains\n");
721 if (!wbinfo_show_sequence()) {
722 printf("Could not show sequence numbers\n");
727 if (!wbinfo_get_usergroups(string_arg)) {
728 printf("Could not get groups for user %s\n",
734 BOOL got_error = False;
736 if (!wbinfo_auth(string_arg)) {
737 printf("Could not authenticate user %s with "
738 "plaintext password\n", string_arg);
742 if (!wbinfo_auth_crap(string_arg)) {
743 printf("Could not authenticate user %s with "
744 "challenge/response\n", string_arg);
754 if (!wbinfo_ping()) {
755 printf("could not ping winbindd!\n");
760 case OPT_SET_AUTH_USER:
761 if (!(wbinfo_set_auth_user(string_arg))) {
766 fprintf(stderr, "Invalid option\n");