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());
70 fstrcpy(user, domuser);
76 fstrcpy(domain, domuser);
77 domain[PTR_DIFF(p, domuser)] = 0;
82 /* List groups a user is a member of */
84 static BOOL wbinfo_get_usergroups(char *user)
86 struct winbindd_request request;
87 struct winbindd_response response;
91 ZERO_STRUCT(response);
95 fstrcpy(request.data.username, user);
97 result = winbindd_request(WINBINDD_GETGROUPS, &request, &response);
99 if (result != NSS_STATUS_SUCCESS)
102 for (i = 0; i < response.data.num_entries; i++)
103 printf("%d\n", (int)((gid_t *)response.extra_data)[i]);
105 SAFE_FREE(response.extra_data);
110 /* List trusted domains */
112 static BOOL wbinfo_list_domains(void)
114 struct winbindd_response response;
117 ZERO_STRUCT(response);
121 if (winbindd_request(WINBINDD_LIST_TRUSTDOM, NULL, &response) !=
122 NSS_STATUS_SUCCESS) {
126 /* Display response */
128 if (response.extra_data) {
129 char *extra_data = (char *)response.extra_data;
131 while(next_token(&extra_data, name, ",", sizeof(fstring)))
132 printf("%s\n", name);
134 SAFE_FREE(response.extra_data);
140 /* Check trust account password */
142 static BOOL wbinfo_check_secret(void)
144 struct winbindd_response response;
147 ZERO_STRUCT(response);
149 result = winbindd_request(WINBINDD_CHECK_MACHACC, NULL, &response) ==
154 if (response.data.num_entries == 0) {
155 printf("Secret is good\n");
157 printf("Secret is bad\n0x%08x\n",
158 response.data.num_entries);
167 /* Convert uid to sid */
169 static BOOL wbinfo_uid_to_sid(uid_t uid)
171 struct winbindd_request request;
172 struct winbindd_response response;
174 ZERO_STRUCT(request);
175 ZERO_STRUCT(response);
179 request.data.uid = uid;
180 if (winbindd_request(WINBINDD_UID_TO_SID, &request, &response) !=
181 NSS_STATUS_SUCCESS) {
185 /* Display response */
187 printf("%s\n", response.data.sid.sid);
192 /* Convert gid to sid */
194 static BOOL wbinfo_gid_to_sid(gid_t gid)
196 struct winbindd_request request;
197 struct winbindd_response response;
199 ZERO_STRUCT(request);
200 ZERO_STRUCT(response);
204 request.data.gid = gid;
205 if (winbindd_request(WINBINDD_GID_TO_SID, &request, &response) !=
206 NSS_STATUS_SUCCESS) {
210 /* Display response */
212 printf("%s\n", response.data.sid.sid);
217 /* Convert sid to uid */
219 static BOOL wbinfo_sid_to_uid(char *sid)
221 struct winbindd_request request;
222 struct winbindd_response response;
224 ZERO_STRUCT(request);
225 ZERO_STRUCT(response);
229 fstrcpy(request.data.sid, sid);
230 if (winbindd_request(WINBINDD_SID_TO_UID, &request, &response) !=
231 NSS_STATUS_SUCCESS) {
235 /* Display response */
237 printf("%d\n", (int)response.data.uid);
242 static BOOL wbinfo_sid_to_gid(char *sid)
244 struct winbindd_request request;
245 struct winbindd_response response;
247 ZERO_STRUCT(request);
248 ZERO_STRUCT(response);
252 fstrcpy(request.data.sid, sid);
253 if (winbindd_request(WINBINDD_SID_TO_GID, &request, &response) !=
254 NSS_STATUS_SUCCESS) {
258 /* Display response */
260 printf("%d\n", (int)response.data.gid);
265 /* Convert sid to string */
267 static BOOL wbinfo_lookupsid(char *sid)
269 struct winbindd_request request;
270 struct winbindd_response response;
272 ZERO_STRUCT(request);
273 ZERO_STRUCT(response);
275 /* Send off request */
277 fstrcpy(request.data.sid, sid);
278 if (winbindd_request(WINBINDD_LOOKUPSID, &request, &response) !=
279 NSS_STATUS_SUCCESS) {
283 /* Display response */
285 printf("%s %d\n", response.data.name.name, response.data.name.type);
290 /* Convert string to sid */
292 static BOOL wbinfo_lookupname(char *name)
294 struct winbindd_request request;
295 struct winbindd_response response;
297 /* Send off request */
299 ZERO_STRUCT(request);
300 ZERO_STRUCT(response);
302 fstrcpy(request.data.name, name);
303 if (winbindd_request(WINBINDD_LOOKUPNAME, &request, &response) !=
304 NSS_STATUS_SUCCESS) {
308 /* Display response */
310 printf("%s %d\n", response.data.sid.sid, response.data.sid.type);
315 /* Authenticate a user with a plaintext password */
317 static BOOL wbinfo_auth(char *username)
319 struct winbindd_request request;
320 struct winbindd_response response;
324 /* Send off request */
326 ZERO_STRUCT(request);
327 ZERO_STRUCT(response);
329 p = strchr(username, '%');
333 fstrcpy(request.data.auth.user, username);
334 fstrcpy(request.data.auth.pass, p + 1);
337 fstrcpy(request.data.auth.user, username);
339 result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
341 /* Display response */
343 printf("plaintext password authentication %s\n",
344 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
346 return result == NSS_STATUS_SUCCESS;
349 /* Authenticate a user with a challenge/response */
351 static BOOL wbinfo_auth_crap(char *username)
353 struct winbindd_request request;
354 struct winbindd_response response;
361 /* Send off request */
363 ZERO_STRUCT(request);
364 ZERO_STRUCT(response);
366 p = strchr(username, '%');
370 fstrcpy(pass, p + 1);
373 parse_wbinfo_domain_user(username, name_domain, name_user);
375 fstrcpy(request.data.auth_crap.user, name_user);
377 fstrcpy(request.data.auth_crap.domain, name_domain);
379 generate_random_buffer(request.data.auth_crap.chal, 8, False);
381 SMBencrypt((uchar *)pass, request.data.auth_crap.chal,
382 (uchar *)request.data.auth_crap.lm_resp);
383 SMBNTencrypt((uchar *)pass, request.data.auth_crap.chal,
384 (uchar *)request.data.auth_crap.nt_resp);
386 request.data.auth_crap.lm_resp_len = 24;
387 request.data.auth_crap.nt_resp_len = 24;
389 result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
391 /* Display response */
393 printf("challenge/response password authentication %s\n",
394 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
396 return result == NSS_STATUS_SUCCESS;
399 /* Print domain users */
401 static BOOL print_domain_users(void)
403 struct winbindd_response response;
407 /* Send request to winbind daemon */
409 ZERO_STRUCT(response);
411 if (winbindd_request(WINBINDD_LIST_USERS, NULL, &response) !=
412 NSS_STATUS_SUCCESS) {
416 /* Look through extra data */
418 if (!response.extra_data)
421 extra_data = (char *)response.extra_data;
423 while(next_token(&extra_data, name, ",", sizeof(fstring)))
424 printf("%s\n", name);
426 SAFE_FREE(response.extra_data);
431 /* Print domain groups */
433 static BOOL print_domain_groups(void)
435 struct winbindd_response response;
439 ZERO_STRUCT(response);
441 if (winbindd_request(WINBINDD_LIST_GROUPS, NULL, &response) !=
442 NSS_STATUS_SUCCESS) {
446 /* Look through extra data */
448 if (!response.extra_data)
451 extra_data = (char *)response.extra_data;
453 while(next_token(&extra_data, name, ",", sizeof(fstring)))
454 printf("%s\n", name);
456 SAFE_FREE(response.extra_data);
461 /* Set the authorised user for winbindd access in secrets.tdb */
463 static BOOL wbinfo_set_auth_user(char *username)
467 /* Separate into user and password */
469 password = strchr(username, '%');
477 /* Store in secrets.tdb */
479 if (!secrets_store(SECRETS_AUTH_USER, username, strlen(username) + 1) ||
480 !secrets_store(SECRETS_AUTH_PASSWORD, password, strlen(password) + 1)) {
481 fprintf(stderr, "error storing authenticated user info\n");
488 static BOOL wbinfo_ping(void)
492 result = winbindd_request(WINBINDD_PING, NULL, NULL);
494 /* Display response */
496 printf("'ping' to winbindd %s\n",
497 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
499 return result == NSS_STATUS_SUCCESS;
502 /* Print program usage */
504 static void usage(void)
506 printf("Usage: wbinfo -ug | -n name | -sSY sid | -UG uid/gid | -tm "
507 "| -a user%%password\n");
508 printf("\t-u\t\t\tlists all domain users\n");
509 printf("\t-g\t\t\tlists all domain groups\n");
510 printf("\t-n name\t\t\tconverts name to sid\n");
511 printf("\t-s sid\t\t\tconverts sid to name\n");
512 printf("\t-U uid\t\t\tconverts uid to sid\n");
513 printf("\t-G gid\t\t\tconverts gid to sid\n");
514 printf("\t-S sid\t\t\tconverts sid to uid\n");
515 printf("\t-Y sid\t\t\tconverts sid to gid\n");
516 printf("\t-t\t\t\tcheck shared secret\n");
517 printf("\t-m\t\t\tlist trusted domains\n");
518 printf("\t-r user\t\t\tget user groups\n");
519 printf("\t-a user%%password\tauthenticate user\n");
520 printf("\t-p 'ping' winbindd to see if it is alive\n");
526 OPT_SET_AUTH_USER = 1000
529 int main(int argc, char **argv)
531 extern pstring global_myname;
535 static char *string_arg;
537 BOOL got_command = False;
539 struct poptOption long_options[] = {
541 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
542 { "help", 'h', POPT_ARG_NONE, 0, 'h' },
543 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u' },
544 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g' },
545 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n' },
546 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's' },
547 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U' },
548 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G' },
549 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S' },
550 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y' },
551 { "check-secret", 't', POPT_ARG_NONE, 0, 't' },
552 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm' },
553 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r' },
554 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a' },
555 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER },
556 { "ping", 'p', POPT_ARG_NONE, 0, 'p' },
560 /* Samba client initialisation */
562 if (!*global_myname) {
565 fstrcpy(global_myname, myhostname());
566 p = strchr(global_myname, '.');
572 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
573 fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
574 dyn_CONFIGFILE, strerror(errno));
580 /* Parse command line options */
589 pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
591 while((opt = poptGetNextOpt(pc)) != -1) {
593 fprintf(stderr, "No more than one command may be specified "
600 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
601 POPT_CONTEXT_KEEP_FIRST);
603 while((opt = poptGetNextOpt(pc)) != -1) {
609 if (!print_domain_users()) {
610 printf("Error looking up domain users\n");
615 if (!print_domain_groups()) {
616 printf("Error looking up domain groups\n");
621 if (!wbinfo_lookupsid(string_arg)) {
622 printf("Could not lookup sid %s\n", string_arg);
627 if (!wbinfo_lookupname(string_arg)) {
628 printf("Could not lookup name %s\n", string_arg);
633 if (!wbinfo_uid_to_sid(int_arg)) {
634 printf("Could not convert uid %d to sid\n", int_arg);
639 if (!wbinfo_gid_to_sid(int_arg)) {
640 printf("Could not convert gid %d to sid\n",
646 if (!wbinfo_sid_to_uid(string_arg)) {
647 printf("Could not convert sid %s to uid\n",
653 if (!wbinfo_sid_to_gid(string_arg)) {
654 printf("Could not convert sid %s to gid\n",
660 if (!wbinfo_check_secret()) {
661 printf("Could not check secret\n");
666 if (!wbinfo_list_domains()) {
667 printf("Could not list trusted domains\n");
672 if (!wbinfo_get_usergroups(string_arg)) {
673 printf("Could not get groups for user %s\n",
679 BOOL got_error = False;
681 if (!wbinfo_auth(string_arg)) {
682 printf("Could not authenticate user %s with "
683 "plaintext password\n", string_arg);
687 if (!wbinfo_auth_crap(string_arg)) {
688 printf("Could not authenticate user %s with "
689 "challenge/response\n", string_arg);
699 if (!wbinfo_ping()) {
700 printf("could not ping winbindd!\n");
705 case OPT_SET_AUTH_USER:
706 if (!(wbinfo_set_auth_user(string_arg))) {
711 fprintf(stderr, "Invalid option\n");