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 static char *get_winbind_domain(void)
63 struct winbindd_response response;
64 static fstring winbind_domain;
66 ZERO_STRUCT(response);
68 /* Send off request */
70 if (winbindd_request(WINBINDD_DOMAIN_NAME, NULL, &response) !=
72 printf("could not obtain winbind domain name!\n");
76 fstrcpy(winbind_domain, response.data.domain_name);
78 return winbind_domain;
82 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
83 form DOMAIN/user into a domain and a user */
85 static BOOL parse_wbinfo_domain_user(const char *domuser, fstring domain, fstring user)
88 char *p = strchr(domuser,get_winbind_separator());
91 fstrcpy(user, domuser);
92 fstrcpy(domain, get_winbind_domain());
97 fstrcpy(domain, domuser);
98 domain[PTR_DIFF(p, domuser)] = 0;
103 /* List groups a user is a member of */
105 static BOOL wbinfo_get_usergroups(char *user)
107 struct winbindd_request request;
108 struct winbindd_response response;
112 ZERO_STRUCT(response);
116 fstrcpy(request.data.username, user);
118 result = winbindd_request(WINBINDD_GETGROUPS, &request, &response);
120 if (result != NSS_STATUS_SUCCESS)
123 for (i = 0; i < response.data.num_entries; i++)
124 printf("%d\n", (int)((gid_t *)response.extra_data)[i]);
126 SAFE_FREE(response.extra_data);
131 /* List trusted domains */
133 static BOOL wbinfo_list_domains(void)
135 struct winbindd_response response;
138 ZERO_STRUCT(response);
142 if (winbindd_request(WINBINDD_LIST_TRUSTDOM, NULL, &response) !=
143 NSS_STATUS_SUCCESS) {
147 /* Display response */
149 if (response.extra_data) {
150 char *extra_data = (char *)response.extra_data;
152 while(next_token(&extra_data, name, ",", sizeof(fstring)))
153 printf("%s\n", name);
155 SAFE_FREE(response.extra_data);
161 /* Check trust account password */
163 static BOOL wbinfo_check_secret(void)
165 struct winbindd_response response;
168 ZERO_STRUCT(response);
170 result = winbindd_request(WINBINDD_CHECK_MACHACC, NULL, &response) ==
175 if (response.data.num_entries == 0) {
176 printf("Secret is good\n");
178 printf("Secret is bad\n0x%08x\n",
179 response.data.num_entries);
188 /* Convert uid to sid */
190 static BOOL wbinfo_uid_to_sid(uid_t uid)
192 struct winbindd_request request;
193 struct winbindd_response response;
195 ZERO_STRUCT(request);
196 ZERO_STRUCT(response);
200 request.data.uid = uid;
201 if (winbindd_request(WINBINDD_UID_TO_SID, &request, &response) !=
202 NSS_STATUS_SUCCESS) {
206 /* Display response */
208 printf("%s\n", response.data.sid.sid);
213 /* Convert gid to sid */
215 static BOOL wbinfo_gid_to_sid(gid_t gid)
217 struct winbindd_request request;
218 struct winbindd_response response;
220 ZERO_STRUCT(request);
221 ZERO_STRUCT(response);
225 request.data.gid = gid;
226 if (winbindd_request(WINBINDD_GID_TO_SID, &request, &response) !=
227 NSS_STATUS_SUCCESS) {
231 /* Display response */
233 printf("%s\n", response.data.sid.sid);
238 /* Convert sid to uid */
240 static BOOL wbinfo_sid_to_uid(char *sid)
242 struct winbindd_request request;
243 struct winbindd_response response;
245 ZERO_STRUCT(request);
246 ZERO_STRUCT(response);
250 fstrcpy(request.data.sid, sid);
251 if (winbindd_request(WINBINDD_SID_TO_UID, &request, &response) !=
252 NSS_STATUS_SUCCESS) {
256 /* Display response */
258 printf("%d\n", (int)response.data.uid);
263 static BOOL wbinfo_sid_to_gid(char *sid)
265 struct winbindd_request request;
266 struct winbindd_response response;
268 ZERO_STRUCT(request);
269 ZERO_STRUCT(response);
273 fstrcpy(request.data.sid, sid);
274 if (winbindd_request(WINBINDD_SID_TO_GID, &request, &response) !=
275 NSS_STATUS_SUCCESS) {
279 /* Display response */
281 printf("%d\n", (int)response.data.gid);
286 /* Convert sid to string */
288 static BOOL wbinfo_lookupsid(char *sid)
290 struct winbindd_request request;
291 struct winbindd_response response;
293 ZERO_STRUCT(request);
294 ZERO_STRUCT(response);
296 /* Send off request */
298 fstrcpy(request.data.sid, sid);
299 if (winbindd_request(WINBINDD_LOOKUPSID, &request, &response) !=
300 NSS_STATUS_SUCCESS) {
304 /* Display response */
306 printf("[%s]\\[%s] %d\n", response.data.name.dom_name, response.data.name.name, response.data.name.type);
311 /* Convert string to sid */
313 static BOOL wbinfo_lookupname(char *name)
315 struct winbindd_request request;
316 struct winbindd_response response;
318 /* Send off request */
320 ZERO_STRUCT(request);
321 ZERO_STRUCT(response);
323 parse_wbinfo_domain_user(name, request.data.name.dom_name, request.data.name.name);
325 if (winbindd_request(WINBINDD_LOOKUPNAME, &request, &response) !=
326 NSS_STATUS_SUCCESS) {
330 /* Display response */
332 printf("%s %d\n", response.data.sid.sid, response.data.sid.type);
337 /* Authenticate a user with a plaintext password */
339 static BOOL wbinfo_auth(char *username)
341 struct winbindd_request request;
342 struct winbindd_response response;
346 /* Send off request */
348 ZERO_STRUCT(request);
349 ZERO_STRUCT(response);
351 p = strchr(username, '%');
355 fstrcpy(request.data.auth.user, username);
356 fstrcpy(request.data.auth.pass, p + 1);
359 fstrcpy(request.data.auth.user, username);
361 result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
363 /* Display response */
365 printf("plaintext password authentication %s\n",
366 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
368 return result == NSS_STATUS_SUCCESS;
371 /* Authenticate a user with a challenge/response */
373 static BOOL wbinfo_auth_crap(char *username)
375 struct winbindd_request request;
376 struct winbindd_response response;
383 /* Send off request */
385 ZERO_STRUCT(request);
386 ZERO_STRUCT(response);
388 p = strchr(username, '%');
392 fstrcpy(pass, p + 1);
395 parse_wbinfo_domain_user(username, name_domain, name_user);
397 fstrcpy(request.data.auth_crap.user, name_user);
399 fstrcpy(request.data.auth_crap.domain, name_domain);
401 generate_random_buffer(request.data.auth_crap.chal, 8, False);
403 SMBencrypt((uchar *)pass, request.data.auth_crap.chal,
404 (uchar *)request.data.auth_crap.lm_resp);
405 SMBNTencrypt((uchar *)pass, request.data.auth_crap.chal,
406 (uchar *)request.data.auth_crap.nt_resp);
408 request.data.auth_crap.lm_resp_len = 24;
409 request.data.auth_crap.nt_resp_len = 24;
411 result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
413 /* Display response */
415 printf("challenge/response password authentication %s\n",
416 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
418 return result == NSS_STATUS_SUCCESS;
421 /* Print domain users */
423 static BOOL print_domain_users(void)
425 struct winbindd_response response;
429 /* Send request to winbind daemon */
431 ZERO_STRUCT(response);
433 if (winbindd_request(WINBINDD_LIST_USERS, NULL, &response) !=
434 NSS_STATUS_SUCCESS) {
438 /* Look through extra data */
440 if (!response.extra_data)
443 extra_data = (char *)response.extra_data;
445 while(next_token(&extra_data, name, ",", sizeof(fstring)))
446 printf("%s\n", name);
448 SAFE_FREE(response.extra_data);
453 /* Print domain groups */
455 static BOOL print_domain_groups(void)
457 struct winbindd_response response;
461 ZERO_STRUCT(response);
463 if (winbindd_request(WINBINDD_LIST_GROUPS, NULL, &response) !=
464 NSS_STATUS_SUCCESS) {
468 /* Look through extra data */
470 if (!response.extra_data)
473 extra_data = (char *)response.extra_data;
475 while(next_token(&extra_data, name, ",", sizeof(fstring)))
476 printf("%s\n", name);
478 SAFE_FREE(response.extra_data);
483 /* Set the authorised user for winbindd access in secrets.tdb */
485 static BOOL wbinfo_set_auth_user(char *username)
489 /* Separate into user and password */
491 password = strchr(username, '%');
499 /* Store in secrets.tdb */
501 if (!secrets_store(SECRETS_AUTH_USER, username, strlen(username) + 1) ||
502 !secrets_store(SECRETS_AUTH_PASSWORD, password, strlen(password) + 1)) {
503 fprintf(stderr, "error storing authenticated user info\n");
510 static BOOL wbinfo_ping(void)
514 result = winbindd_request(WINBINDD_PING, NULL, NULL);
516 /* Display response */
518 printf("'ping' to winbindd %s\n",
519 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
521 return result == NSS_STATUS_SUCCESS;
524 /* Print program usage */
526 static void usage(void)
528 printf("Usage: wbinfo -ug | -n name | -sSY sid | -UG uid/gid | -tm "
529 "| -a user%%password\n");
530 printf("\t-u\t\t\tlists all domain users\n");
531 printf("\t-g\t\t\tlists all domain groups\n");
532 printf("\t-n name\t\t\tconverts name to sid\n");
533 printf("\t-s sid\t\t\tconverts sid to name\n");
534 printf("\t-U uid\t\t\tconverts uid to sid\n");
535 printf("\t-G gid\t\t\tconverts gid to sid\n");
536 printf("\t-S sid\t\t\tconverts sid to uid\n");
537 printf("\t-Y sid\t\t\tconverts sid to gid\n");
538 printf("\t-t\t\t\tcheck shared secret\n");
539 printf("\t-m\t\t\tlist trusted domains\n");
540 printf("\t-r user\t\t\tget user groups\n");
541 printf("\t-a user%%password\tauthenticate user\n");
542 printf("\t-p 'ping' winbindd to see if it is alive\n");
548 OPT_SET_AUTH_USER = 1000
551 int main(int argc, char **argv)
553 extern pstring global_myname;
557 static char *string_arg;
559 BOOL got_command = False;
561 struct poptOption long_options[] = {
563 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
564 { "help", 'h', POPT_ARG_NONE, 0, 'h' },
565 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u' },
566 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g' },
567 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n' },
568 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's' },
569 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U' },
570 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G' },
571 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S' },
572 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y' },
573 { "check-secret", 't', POPT_ARG_NONE, 0, 't' },
574 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm' },
575 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r' },
576 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a' },
577 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER },
578 { "ping", 'p', POPT_ARG_NONE, 0, 'p' },
582 /* Samba client initialisation */
584 if (!*global_myname) {
587 fstrcpy(global_myname, myhostname());
588 p = strchr(global_myname, '.');
594 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
595 fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
596 dyn_CONFIGFILE, strerror(errno));
602 /* Parse command line options */
611 pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
613 while((opt = poptGetNextOpt(pc)) != -1) {
615 fprintf(stderr, "No more than one command may be specified "
622 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
623 POPT_CONTEXT_KEEP_FIRST);
625 while((opt = poptGetNextOpt(pc)) != -1) {
631 if (!print_domain_users()) {
632 printf("Error looking up domain users\n");
637 if (!print_domain_groups()) {
638 printf("Error looking up domain groups\n");
643 if (!wbinfo_lookupsid(string_arg)) {
644 printf("Could not lookup sid %s\n", string_arg);
649 if (!wbinfo_lookupname(string_arg)) {
650 printf("Could not lookup name %s\n", string_arg);
655 if (!wbinfo_uid_to_sid(int_arg)) {
656 printf("Could not convert uid %d to sid\n", int_arg);
661 if (!wbinfo_gid_to_sid(int_arg)) {
662 printf("Could not convert gid %d to sid\n",
668 if (!wbinfo_sid_to_uid(string_arg)) {
669 printf("Could not convert sid %s to uid\n",
675 if (!wbinfo_sid_to_gid(string_arg)) {
676 printf("Could not convert sid %s to gid\n",
682 if (!wbinfo_check_secret()) {
683 printf("Could not check secret\n");
688 if (!wbinfo_list_domains()) {
689 printf("Could not list trusted domains\n");
694 if (!wbinfo_get_usergroups(string_arg)) {
695 printf("Could not get groups for user %s\n",
701 BOOL got_error = False;
703 if (!wbinfo_auth(string_arg)) {
704 printf("Could not authenticate user %s with "
705 "plaintext password\n", string_arg);
709 if (!wbinfo_auth_crap(string_arg)) {
710 printf("Could not authenticate user %s with "
711 "challenge/response\n", string_arg);
721 if (!wbinfo_ping()) {
722 printf("could not ping winbindd!\n");
727 case OPT_SET_AUTH_USER:
728 if (!(wbinfo_set_auth_user(string_arg))) {
733 fprintf(stderr, "Invalid option\n");