2 Unix SMB/Netbios implementation.
5 Winbind status program.
7 Copyright (C) Tim Potter 2000
8 Copyright (C) Andrew Bartlett 2002
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 /* Prototypes from common.h */
31 NSS_STATUS winbindd_request(int req_type,
32 struct winbindd_request *request,
33 struct winbindd_response *response);
35 static char get_winbind_separator(void)
37 struct winbindd_response response;
38 char winbind_separator;
40 ZERO_STRUCT(response);
42 /* Send off request */
44 if (winbindd_request(WINBINDD_INFO, NULL, &response) !=
46 printf("could not obtain winbind seperator!\n");
50 winbind_separator = response.data.info.winbind_separator;
52 if (!winbind_separator) {
53 printf("winbind separator was NULL!\n");
57 return winbind_separator;
61 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
62 form DOMAIN/user into a domain and a user */
64 static BOOL parse_wbinfo_domain_user(const char *domuser, fstring domain, fstring user)
67 char *p = strchr(domuser,get_winbind_separator());
73 fstrcpy(domain, domuser);
74 domain[PTR_DIFF(p, domuser)] = 0;
79 /* List groups a user is a member of */
81 static BOOL wbinfo_get_usergroups(char *user)
83 struct winbindd_request request;
84 struct winbindd_response response;
88 ZERO_STRUCT(response);
92 fstrcpy(request.data.username, user);
94 result = winbindd_request(WINBINDD_GETGROUPS, &request, &response);
96 if (result != NSS_STATUS_SUCCESS)
99 for (i = 0; i < response.data.num_entries; i++)
100 printf("%d\n", (int)((gid_t *)response.extra_data)[i]);
102 SAFE_FREE(response.extra_data);
107 /* List trusted domains */
109 static BOOL wbinfo_list_domains(void)
111 struct winbindd_response response;
114 ZERO_STRUCT(response);
118 if (winbindd_request(WINBINDD_LIST_TRUSTDOM, NULL, &response) !=
119 NSS_STATUS_SUCCESS) {
123 /* Display response */
125 if (response.extra_data) {
126 char *extra_data = (char *)response.extra_data;
128 while(next_token(&extra_data, name, ",", sizeof(fstring)))
129 printf("%s\n", name);
131 SAFE_FREE(response.extra_data);
137 /* Check trust account password */
139 static BOOL wbinfo_check_secret(void)
141 struct winbindd_response response;
144 ZERO_STRUCT(response);
146 result = winbindd_request(WINBINDD_CHECK_MACHACC, NULL, &response) ==
151 if (response.data.num_entries == 0) {
152 printf("Secret is good\n");
154 printf("Secret is bad\n0x%08x\n",
155 response.data.num_entries);
164 /* Convert uid to sid */
166 static BOOL wbinfo_uid_to_sid(uid_t uid)
168 struct winbindd_request request;
169 struct winbindd_response response;
171 ZERO_STRUCT(request);
172 ZERO_STRUCT(response);
176 request.data.uid = uid;
177 if (winbindd_request(WINBINDD_UID_TO_SID, &request, &response) !=
178 NSS_STATUS_SUCCESS) {
182 /* Display response */
184 printf("%s\n", response.data.sid.sid);
189 /* Convert gid to sid */
191 static BOOL wbinfo_gid_to_sid(gid_t gid)
193 struct winbindd_request request;
194 struct winbindd_response response;
196 ZERO_STRUCT(request);
197 ZERO_STRUCT(response);
201 request.data.gid = gid;
202 if (winbindd_request(WINBINDD_GID_TO_SID, &request, &response) !=
203 NSS_STATUS_SUCCESS) {
207 /* Display response */
209 printf("%s\n", response.data.sid.sid);
214 /* Convert sid to uid */
216 static BOOL wbinfo_sid_to_uid(char *sid)
218 struct winbindd_request request;
219 struct winbindd_response response;
221 ZERO_STRUCT(request);
222 ZERO_STRUCT(response);
226 fstrcpy(request.data.sid, sid);
227 if (winbindd_request(WINBINDD_SID_TO_UID, &request, &response) !=
228 NSS_STATUS_SUCCESS) {
232 /* Display response */
234 printf("%d\n", (int)response.data.uid);
239 static BOOL wbinfo_sid_to_gid(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_GID, &request, &response) !=
251 NSS_STATUS_SUCCESS) {
255 /* Display response */
257 printf("%d\n", (int)response.data.gid);
262 /* Convert sid to string */
264 static BOOL wbinfo_lookupsid(char *sid)
266 struct winbindd_request request;
267 struct winbindd_response response;
269 ZERO_STRUCT(request);
270 ZERO_STRUCT(response);
272 /* Send off request */
274 fstrcpy(request.data.sid, sid);
275 if (winbindd_request(WINBINDD_LOOKUPSID, &request, &response) !=
276 NSS_STATUS_SUCCESS) {
280 /* Display response */
282 printf("%s %d\n", response.data.name.name, response.data.name.type);
287 /* Convert string to sid */
289 static BOOL wbinfo_lookupname(char *name)
291 struct winbindd_request request;
292 struct winbindd_response response;
295 * Don't do the lookup if the name has no separator.
298 if (!strchr(name, get_winbind_separator()))
301 /* Send off request */
303 ZERO_STRUCT(request);
304 ZERO_STRUCT(response);
306 fstrcpy(request.data.name, name);
307 if (winbindd_request(WINBINDD_LOOKUPNAME, &request, &response) !=
308 NSS_STATUS_SUCCESS) {
312 /* Display response */
314 printf("%s %d\n", response.data.sid.sid, response.data.sid.type);
319 /* Authenticate a user with a plaintext password */
321 static BOOL wbinfo_auth(char *username)
323 struct winbindd_request request;
324 struct winbindd_response response;
328 /* Send off request */
330 ZERO_STRUCT(request);
331 ZERO_STRUCT(response);
333 p = strchr(username, '%');
337 fstrcpy(request.data.auth.user, username);
338 fstrcpy(request.data.auth.pass, p + 1);
341 fstrcpy(request.data.auth.user, username);
343 result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
345 /* Display response */
347 printf("plaintext password authentication %s\n",
348 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
350 return result == NSS_STATUS_SUCCESS;
353 /* Authenticate a user with a challenge/response */
355 static BOOL wbinfo_auth_crap(char *username)
357 struct winbindd_request request;
358 struct winbindd_response response;
364 char sep = get_winbind_separator();
367 * Don't do the lookup if the name has no separator.
370 if (!strchr(username, sep)) {
371 printf("no domain seperator (%c) in username - failing\n", sep);
375 /* Send off request */
377 ZERO_STRUCT(request);
378 ZERO_STRUCT(response);
380 p = strchr(username, '%');
384 fstrcpy(pass, p + 1);
387 parse_wbinfo_domain_user(username, name_domain, name_user);
389 fstrcpy(request.data.auth_crap.user, name_user);
391 fstrcpy(request.data.auth_crap.domain, name_domain);
393 generate_random_buffer(request.data.auth_crap.chal, 8, False);
395 SMBencrypt((uchar *)pass, request.data.auth_crap.chal,
396 (uchar *)request.data.auth_crap.lm_resp);
397 SMBNTencrypt((uchar *)pass, request.data.auth_crap.chal,
398 (uchar *)request.data.auth_crap.nt_resp);
400 request.data.auth_crap.lm_resp_len = 24;
401 request.data.auth_crap.nt_resp_len = 24;
403 result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
405 /* Display response */
407 printf("challenge/response password authentication %s\n",
408 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
410 return result == NSS_STATUS_SUCCESS;
413 /* Print domain users */
415 static BOOL print_domain_users(void)
417 struct winbindd_response response;
421 /* Send request to winbind daemon */
423 ZERO_STRUCT(response);
425 if (winbindd_request(WINBINDD_LIST_USERS, NULL, &response) !=
426 NSS_STATUS_SUCCESS) {
430 /* Look through extra data */
432 if (!response.extra_data)
435 extra_data = (char *)response.extra_data;
437 while(next_token(&extra_data, name, ",", sizeof(fstring)))
438 printf("%s\n", name);
440 SAFE_FREE(response.extra_data);
445 /* Print domain groups */
447 static BOOL print_domain_groups(void)
449 struct winbindd_response response;
453 ZERO_STRUCT(response);
455 if (winbindd_request(WINBINDD_LIST_GROUPS, NULL, &response) !=
456 NSS_STATUS_SUCCESS) {
460 /* Look through extra data */
462 if (!response.extra_data)
465 extra_data = (char *)response.extra_data;
467 while(next_token(&extra_data, name, ",", sizeof(fstring)))
468 printf("%s\n", name);
470 SAFE_FREE(response.extra_data);
475 /* Set the authorised user for winbindd access in secrets.tdb */
477 static BOOL wbinfo_set_auth_user(char *username)
481 /* Separate into user and password */
483 password = strchr(username, '%');
491 /* Store in secrets.tdb */
493 if (!secrets_store(SECRETS_AUTH_USER, username, strlen(username) + 1) ||
494 !secrets_store(SECRETS_AUTH_PASSWORD, password, strlen(password) + 1)) {
495 fprintf(stderr, "error storing authenticated user info\n");
502 static BOOL wbinfo_ping(void)
506 result = winbindd_request(WINBINDD_PING, NULL, NULL);
508 /* Display response */
510 printf("'ping' to winbindd %s\n",
511 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
513 return result == NSS_STATUS_SUCCESS;
516 /* Print program usage */
518 static void usage(void)
520 printf("Usage: wbinfo -ug | -n name | -sSY sid | -UG uid/gid | -tm "
521 "| -a user%%password\n");
522 printf("\t-u\t\t\tlists all domain users\n");
523 printf("\t-g\t\t\tlists all domain groups\n");
524 printf("\t-n name\t\t\tconverts name to sid\n");
525 printf("\t-s sid\t\t\tconverts sid to name\n");
526 printf("\t-U uid\t\t\tconverts uid to sid\n");
527 printf("\t-G gid\t\t\tconverts gid to sid\n");
528 printf("\t-S sid\t\t\tconverts sid to uid\n");
529 printf("\t-Y sid\t\t\tconverts sid to gid\n");
530 printf("\t-t\t\t\tcheck shared secret\n");
531 printf("\t-m\t\t\tlist trusted domains\n");
532 printf("\t-r user\t\t\tget user groups\n");
533 printf("\t-a user%%password\tauthenticate user\n");
534 printf("\t-p 'ping' winbindd to see if it is alive\n");
540 OPT_SET_AUTH_USER = 1000
543 int main(int argc, char **argv)
545 extern pstring global_myname;
549 static char *string_arg;
551 BOOL got_command = False;
553 struct poptOption long_options[] = {
555 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
556 { "help", 'h', POPT_ARG_NONE, 0, 'h' },
557 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u' },
558 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g' },
559 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n' },
560 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's' },
561 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U' },
562 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G' },
563 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S' },
564 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y' },
565 { "check-secret", 't', POPT_ARG_NONE, 0, 't' },
566 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm' },
567 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r' },
568 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a' },
569 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER },
570 { "ping", 'p', POPT_ARG_NONE, 0, 'p' },
574 /* Samba client initialisation */
576 if (!*global_myname) {
579 fstrcpy(global_myname, myhostname());
580 p = strchr(global_myname, '.');
586 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
587 fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
588 dyn_CONFIGFILE, strerror(errno));
594 /* Parse command line options */
603 pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
605 while((opt = poptGetNextOpt(pc)) != -1) {
607 fprintf(stderr, "No more than one command may be specified "
614 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
615 POPT_CONTEXT_KEEP_FIRST);
617 while((opt = poptGetNextOpt(pc)) != -1) {
623 if (!print_domain_users()) {
624 printf("Error looking up domain users\n");
629 if (!print_domain_groups()) {
630 printf("Error looking up domain groups\n");
635 if (!wbinfo_lookupsid(string_arg)) {
636 printf("Could not lookup sid %s\n", string_arg);
641 if (!wbinfo_lookupname(string_arg)) {
642 printf("Could not lookup name %s\n", string_arg);
647 if (!wbinfo_uid_to_sid(int_arg)) {
648 printf("Could not convert uid %d to sid\n", int_arg);
653 if (!wbinfo_gid_to_sid(int_arg)) {
654 printf("Could not convert gid %d to sid\n",
660 if (!wbinfo_sid_to_uid(string_arg)) {
661 printf("Could not convert sid %s to uid\n",
667 if (!wbinfo_sid_to_gid(string_arg)) {
668 printf("Could not convert sid %s to gid\n",
674 if (!wbinfo_check_secret()) {
675 printf("Could not check secret\n");
680 if (!wbinfo_list_domains()) {
681 printf("Could not list trusted domains\n");
686 if (!wbinfo_get_usergroups(string_arg)) {
687 printf("Could not get groups for user %s\n",
693 BOOL got_error = False;
695 if (!wbinfo_auth(string_arg)) {
696 printf("Could not authenticate user %s with "
697 "plaintext password\n", string_arg);
701 if (!wbinfo_auth_crap(string_arg)) {
702 printf("Could not authenticate user %s with "
703 "challenge/response\n", string_arg);
713 if (!wbinfo_ping()) {
714 printf("could not ping winbindd!\n");
719 case OPT_SET_AUTH_USER:
720 if (!(wbinfo_set_auth_user(string_arg))) {
725 fprintf(stderr, "Invalid option\n");