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);
160 /* Check trust account password */
162 static BOOL wbinfo_check_secret(void)
164 struct winbindd_response response;
167 ZERO_STRUCT(response);
169 result = winbindd_request(WINBINDD_CHECK_MACHACC, NULL, &response) ==
174 if (response.data.num_entries == 0) {
175 printf("Secret is good\n");
177 printf("Secret is bad\n0x%08x\n",
178 response.data.num_entries);
187 /* Convert uid to sid */
189 static BOOL wbinfo_uid_to_sid(uid_t uid)
191 struct winbindd_request request;
192 struct winbindd_response response;
194 ZERO_STRUCT(request);
195 ZERO_STRUCT(response);
199 request.data.uid = uid;
200 if (winbindd_request(WINBINDD_UID_TO_SID, &request, &response) !=
201 NSS_STATUS_SUCCESS) {
205 /* Display response */
207 printf("%s\n", response.data.sid.sid);
212 /* Convert gid to sid */
214 static BOOL wbinfo_gid_to_sid(gid_t gid)
216 struct winbindd_request request;
217 struct winbindd_response response;
219 ZERO_STRUCT(request);
220 ZERO_STRUCT(response);
224 request.data.gid = gid;
225 if (winbindd_request(WINBINDD_GID_TO_SID, &request, &response) !=
226 NSS_STATUS_SUCCESS) {
230 /* Display response */
232 printf("%s\n", response.data.sid.sid);
237 /* Convert sid to uid */
239 static BOOL wbinfo_sid_to_uid(char *sid)
241 struct winbindd_request request;
242 struct winbindd_response response;
244 ZERO_STRUCT(request);
245 ZERO_STRUCT(response);
249 fstrcpy(request.data.sid, sid);
250 if (winbindd_request(WINBINDD_SID_TO_UID, &request, &response) !=
251 NSS_STATUS_SUCCESS) {
255 /* Display response */
257 printf("%d\n", (int)response.data.uid);
262 static BOOL wbinfo_sid_to_gid(char *sid)
264 struct winbindd_request request;
265 struct winbindd_response response;
267 ZERO_STRUCT(request);
268 ZERO_STRUCT(response);
272 fstrcpy(request.data.sid, sid);
273 if (winbindd_request(WINBINDD_SID_TO_GID, &request, &response) !=
274 NSS_STATUS_SUCCESS) {
278 /* Display response */
280 printf("%d\n", (int)response.data.gid);
285 /* Convert sid to string */
287 static BOOL wbinfo_lookupsid(char *sid)
289 struct winbindd_request request;
290 struct winbindd_response response;
292 ZERO_STRUCT(request);
293 ZERO_STRUCT(response);
295 /* Send off request */
297 fstrcpy(request.data.sid, sid);
298 if (winbindd_request(WINBINDD_LOOKUPSID, &request, &response) !=
299 NSS_STATUS_SUCCESS) {
303 /* Display response */
305 printf("[%s]\\[%s] %d\n", response.data.name.dom_name, response.data.name.name, response.data.name.type);
310 /* Convert string to sid */
312 static BOOL wbinfo_lookupname(char *name)
314 struct winbindd_request request;
315 struct winbindd_response response;
317 /* Send off request */
319 ZERO_STRUCT(request);
320 ZERO_STRUCT(response);
322 parse_wbinfo_domain_user(name, request.data.name.dom_name, request.data.name.name);
324 if (winbindd_request(WINBINDD_LOOKUPNAME, &request, &response) !=
325 NSS_STATUS_SUCCESS) {
329 /* Display response */
331 printf("%s %d\n", response.data.sid.sid, response.data.sid.type);
336 /* Authenticate a user with a plaintext password */
338 static BOOL wbinfo_auth(char *username)
340 struct winbindd_request request;
341 struct winbindd_response response;
345 /* Send off request */
347 ZERO_STRUCT(request);
348 ZERO_STRUCT(response);
350 p = strchr(username, '%');
354 fstrcpy(request.data.auth.user, username);
355 fstrcpy(request.data.auth.pass, p + 1);
358 fstrcpy(request.data.auth.user, username);
360 result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
362 /* Display response */
364 printf("plaintext password authentication %s\n",
365 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
367 return result == NSS_STATUS_SUCCESS;
370 /* Authenticate a user with a challenge/response */
372 static BOOL wbinfo_auth_crap(char *username)
374 struct winbindd_request request;
375 struct winbindd_response response;
382 /* Send off request */
384 ZERO_STRUCT(request);
385 ZERO_STRUCT(response);
387 p = strchr(username, '%');
391 fstrcpy(pass, p + 1);
394 parse_wbinfo_domain_user(username, name_domain, name_user);
396 fstrcpy(request.data.auth_crap.user, name_user);
398 fstrcpy(request.data.auth_crap.domain, name_domain);
400 generate_random_buffer(request.data.auth_crap.chal, 8, False);
402 SMBencrypt((uchar *)pass, request.data.auth_crap.chal,
403 (uchar *)request.data.auth_crap.lm_resp);
404 SMBNTencrypt((uchar *)pass, request.data.auth_crap.chal,
405 (uchar *)request.data.auth_crap.nt_resp);
407 request.data.auth_crap.lm_resp_len = 24;
408 request.data.auth_crap.nt_resp_len = 24;
410 result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
412 /* Display response */
414 printf("challenge/response password authentication %s\n",
415 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
417 return result == NSS_STATUS_SUCCESS;
420 /* Print domain users */
422 static BOOL print_domain_users(void)
424 struct winbindd_response response;
428 /* Send request to winbind daemon */
430 ZERO_STRUCT(response);
432 if (winbindd_request(WINBINDD_LIST_USERS, NULL, &response) !=
433 NSS_STATUS_SUCCESS) {
437 /* Look through extra data */
439 if (!response.extra_data)
442 extra_data = (char *)response.extra_data;
444 while(next_token(&extra_data, name, ",", sizeof(fstring)))
445 printf("%s\n", name);
447 SAFE_FREE(response.extra_data);
452 /* Print domain groups */
454 static BOOL print_domain_groups(void)
456 struct winbindd_response response;
460 ZERO_STRUCT(response);
462 if (winbindd_request(WINBINDD_LIST_GROUPS, NULL, &response) !=
463 NSS_STATUS_SUCCESS) {
467 /* Look through extra data */
469 if (!response.extra_data)
472 extra_data = (char *)response.extra_data;
474 while(next_token(&extra_data, name, ",", sizeof(fstring)))
475 printf("%s\n", name);
477 SAFE_FREE(response.extra_data);
482 /* Set the authorised user for winbindd access in secrets.tdb */
484 static BOOL wbinfo_set_auth_user(char *username)
488 /* Separate into user and password */
490 password = strchr(username, '%');
498 /* Store in secrets.tdb */
500 if (!secrets_store(SECRETS_AUTH_USER, username, strlen(username) + 1) ||
501 !secrets_store(SECRETS_AUTH_PASSWORD, password, strlen(password) + 1)) {
502 fprintf(stderr, "error storing authenticated user info\n");
509 static BOOL wbinfo_ping(void)
513 result = winbindd_request(WINBINDD_PING, NULL, NULL);
515 /* Display response */
517 printf("'ping' to winbindd %s\n",
518 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
520 return result == NSS_STATUS_SUCCESS;
523 /* Print program usage */
525 static void usage(void)
527 printf("Usage: wbinfo -ug | -n name | -sSY sid | -UG uid/gid | -tm "
528 "| -a user%%password\n");
529 printf("\t-u\t\t\tlists all domain users\n");
530 printf("\t-g\t\t\tlists all domain groups\n");
531 printf("\t-n name\t\t\tconverts name to sid\n");
532 printf("\t-s sid\t\t\tconverts sid to name\n");
533 printf("\t-U uid\t\t\tconverts uid to sid\n");
534 printf("\t-G gid\t\t\tconverts gid to sid\n");
535 printf("\t-S sid\t\t\tconverts sid to uid\n");
536 printf("\t-Y sid\t\t\tconverts sid to gid\n");
537 printf("\t-t\t\t\tcheck shared secret\n");
538 printf("\t-m\t\t\tlist trusted domains\n");
539 printf("\t-r user\t\t\tget user groups\n");
540 printf("\t-a user%%password\tauthenticate user\n");
541 printf("\t-p 'ping' winbindd to see if it is alive\n");
547 OPT_SET_AUTH_USER = 1000
550 int main(int argc, char **argv)
552 extern pstring global_myname;
556 static char *string_arg;
558 BOOL got_command = False;
560 struct poptOption long_options[] = {
562 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
563 { "help", 'h', POPT_ARG_NONE, 0, 'h' },
564 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u' },
565 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g' },
566 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n' },
567 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's' },
568 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U' },
569 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G' },
570 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S' },
571 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y' },
572 { "check-secret", 't', POPT_ARG_NONE, 0, 't' },
573 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm' },
574 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r' },
575 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a' },
576 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER },
577 { "ping", 'p', POPT_ARG_NONE, 0, 'p' },
581 /* Samba client initialisation */
583 if (!*global_myname) {
586 fstrcpy(global_myname, myhostname());
587 p = strchr(global_myname, '.');
593 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
594 fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
595 dyn_CONFIGFILE, strerror(errno));
601 /* Parse command line options */
610 pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
612 while((opt = poptGetNextOpt(pc)) != -1) {
614 fprintf(stderr, "No more than one command may be specified "
621 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
622 POPT_CONTEXT_KEEP_FIRST);
624 while((opt = poptGetNextOpt(pc)) != -1) {
630 if (!print_domain_users()) {
631 printf("Error looking up domain users\n");
636 if (!print_domain_groups()) {
637 printf("Error looking up domain groups\n");
642 if (!wbinfo_lookupsid(string_arg)) {
643 printf("Could not lookup sid %s\n", string_arg);
648 if (!wbinfo_lookupname(string_arg)) {
649 printf("Could not lookup name %s\n", string_arg);
654 if (!wbinfo_uid_to_sid(int_arg)) {
655 printf("Could not convert uid %d to sid\n", int_arg);
660 if (!wbinfo_gid_to_sid(int_arg)) {
661 printf("Could not convert gid %d to sid\n",
667 if (!wbinfo_sid_to_uid(string_arg)) {
668 printf("Could not convert sid %s to uid\n",
674 if (!wbinfo_sid_to_gid(string_arg)) {
675 printf("Could not convert sid %s to gid\n",
681 if (!wbinfo_check_secret()) {
682 printf("Could not check secret\n");
687 if (!wbinfo_list_domains()) {
688 printf("Could not list trusted domains\n");
693 if (!wbinfo_get_usergroups(string_arg)) {
694 printf("Could not get groups for user %s\n",
700 BOOL got_error = False;
702 if (!wbinfo_auth(string_arg)) {
703 printf("Could not authenticate user %s with "
704 "plaintext password\n", string_arg);
708 if (!wbinfo_auth_crap(string_arg)) {
709 printf("Could not authenticate user %s with "
710 "challenge/response\n", string_arg);
720 if (!wbinfo_ping()) {
721 printf("could not ping winbindd!\n");
726 case OPT_SET_AUTH_USER:
727 if (!(wbinfo_set_auth_user(string_arg))) {
732 fprintf(stderr, "Invalid option\n");