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;
298 * Don't do the lookup if the name has no separator.
301 if (!strchr(name, get_winbind_separator()))
304 /* Send off request */
306 ZERO_STRUCT(request);
307 ZERO_STRUCT(response);
309 fstrcpy(request.data.name, name);
310 if (winbindd_request(WINBINDD_LOOKUPNAME, &request, &response) !=
311 NSS_STATUS_SUCCESS) {
315 /* Display response */
317 printf("%s %d\n", response.data.sid.sid, response.data.sid.type);
322 /* Authenticate a user with a plaintext password */
324 static BOOL wbinfo_auth(char *username)
326 struct winbindd_request request;
327 struct winbindd_response response;
331 /* Send off request */
333 ZERO_STRUCT(request);
334 ZERO_STRUCT(response);
336 p = strchr(username, '%');
340 fstrcpy(request.data.auth.user, username);
341 fstrcpy(request.data.auth.pass, p + 1);
344 fstrcpy(request.data.auth.user, username);
346 result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
348 /* Display response */
350 printf("plaintext password authentication %s\n",
351 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
353 return result == NSS_STATUS_SUCCESS;
356 /* Authenticate a user with a challenge/response */
358 static BOOL wbinfo_auth_crap(char *username)
360 struct winbindd_request request;
361 struct winbindd_response response;
369 * Don't do the lookup if the name has no separator.
372 /* Send off request */
374 ZERO_STRUCT(request);
375 ZERO_STRUCT(response);
377 p = strchr(username, '%');
381 fstrcpy(pass, p + 1);
384 parse_wbinfo_domain_user(username, name_domain, name_user);
386 fstrcpy(request.data.auth_crap.user, name_user);
388 fstrcpy(request.data.auth_crap.domain, name_domain);
390 generate_random_buffer(request.data.auth_crap.chal, 8, False);
392 SMBencrypt((uchar *)pass, request.data.auth_crap.chal,
393 (uchar *)request.data.auth_crap.lm_resp);
394 SMBNTencrypt((uchar *)pass, request.data.auth_crap.chal,
395 (uchar *)request.data.auth_crap.nt_resp);
397 request.data.auth_crap.lm_resp_len = 24;
398 request.data.auth_crap.nt_resp_len = 24;
400 result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
402 /* Display response */
404 printf("challenge/response password authentication %s\n",
405 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
407 return result == NSS_STATUS_SUCCESS;
410 /* Print domain users */
412 static BOOL print_domain_users(void)
414 struct winbindd_response response;
418 /* Send request to winbind daemon */
420 ZERO_STRUCT(response);
422 if (winbindd_request(WINBINDD_LIST_USERS, NULL, &response) !=
423 NSS_STATUS_SUCCESS) {
427 /* Look through extra data */
429 if (!response.extra_data)
432 extra_data = (char *)response.extra_data;
434 while(next_token(&extra_data, name, ",", sizeof(fstring)))
435 printf("%s\n", name);
437 SAFE_FREE(response.extra_data);
442 /* Print domain groups */
444 static BOOL print_domain_groups(void)
446 struct winbindd_response response;
450 ZERO_STRUCT(response);
452 if (winbindd_request(WINBINDD_LIST_GROUPS, NULL, &response) !=
453 NSS_STATUS_SUCCESS) {
457 /* Look through extra data */
459 if (!response.extra_data)
462 extra_data = (char *)response.extra_data;
464 while(next_token(&extra_data, name, ",", sizeof(fstring)))
465 printf("%s\n", name);
467 SAFE_FREE(response.extra_data);
472 /* Set the authorised user for winbindd access in secrets.tdb */
474 static BOOL wbinfo_set_auth_user(char *username)
478 /* Separate into user and password */
480 password = strchr(username, '%');
488 /* Store in secrets.tdb */
490 if (!secrets_store(SECRETS_AUTH_USER, username, strlen(username) + 1) ||
491 !secrets_store(SECRETS_AUTH_PASSWORD, password, strlen(password) + 1)) {
492 fprintf(stderr, "error storing authenticated user info\n");
499 static BOOL wbinfo_ping(void)
503 result = winbindd_request(WINBINDD_PING, NULL, NULL);
505 /* Display response */
507 printf("'ping' to winbindd %s\n",
508 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
510 return result == NSS_STATUS_SUCCESS;
513 /* Print program usage */
515 static void usage(void)
517 printf("Usage: wbinfo -ug | -n name | -sSY sid | -UG uid/gid | -tm "
518 "| -a user%%password\n");
519 printf("\t-u\t\t\tlists all domain users\n");
520 printf("\t-g\t\t\tlists all domain groups\n");
521 printf("\t-n name\t\t\tconverts name to sid\n");
522 printf("\t-s sid\t\t\tconverts sid to name\n");
523 printf("\t-U uid\t\t\tconverts uid to sid\n");
524 printf("\t-G gid\t\t\tconverts gid to sid\n");
525 printf("\t-S sid\t\t\tconverts sid to uid\n");
526 printf("\t-Y sid\t\t\tconverts sid to gid\n");
527 printf("\t-t\t\t\tcheck shared secret\n");
528 printf("\t-m\t\t\tlist trusted domains\n");
529 printf("\t-r user\t\t\tget user groups\n");
530 printf("\t-a user%%password\tauthenticate user\n");
531 printf("\t-p 'ping' winbindd to see if it is alive\n");
537 OPT_SET_AUTH_USER = 1000
540 int main(int argc, char **argv)
542 extern pstring global_myname;
546 static char *string_arg;
548 BOOL got_command = False;
550 struct poptOption long_options[] = {
552 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
553 { "help", 'h', POPT_ARG_NONE, 0, 'h' },
554 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u' },
555 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g' },
556 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n' },
557 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's' },
558 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U' },
559 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G' },
560 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S' },
561 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y' },
562 { "check-secret", 't', POPT_ARG_NONE, 0, 't' },
563 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm' },
564 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r' },
565 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a' },
566 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER },
567 { "ping", 'p', POPT_ARG_NONE, 0, 'p' },
571 /* Samba client initialisation */
573 if (!*global_myname) {
576 fstrcpy(global_myname, myhostname());
577 p = strchr(global_myname, '.');
583 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
584 fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
585 dyn_CONFIGFILE, strerror(errno));
591 /* Parse command line options */
600 pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
602 while((opt = poptGetNextOpt(pc)) != -1) {
604 fprintf(stderr, "No more than one command may be specified "
611 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
612 POPT_CONTEXT_KEEP_FIRST);
614 while((opt = poptGetNextOpt(pc)) != -1) {
620 if (!print_domain_users()) {
621 printf("Error looking up domain users\n");
626 if (!print_domain_groups()) {
627 printf("Error looking up domain groups\n");
632 if (!wbinfo_lookupsid(string_arg)) {
633 printf("Could not lookup sid %s\n", string_arg);
638 if (!wbinfo_lookupname(string_arg)) {
639 printf("Could not lookup name %s\n", string_arg);
644 if (!wbinfo_uid_to_sid(int_arg)) {
645 printf("Could not convert uid %d to sid\n", int_arg);
650 if (!wbinfo_gid_to_sid(int_arg)) {
651 printf("Could not convert gid %d to sid\n",
657 if (!wbinfo_sid_to_uid(string_arg)) {
658 printf("Could not convert sid %s to uid\n",
664 if (!wbinfo_sid_to_gid(string_arg)) {
665 printf("Could not convert sid %s to gid\n",
671 if (!wbinfo_check_secret()) {
672 printf("Could not check secret\n");
677 if (!wbinfo_list_domains()) {
678 printf("Could not list trusted domains\n");
683 if (!wbinfo_get_usergroups(string_arg)) {
684 printf("Could not get groups for user %s\n",
690 BOOL got_error = False;
692 if (!wbinfo_auth(string_arg)) {
693 printf("Could not authenticate user %s with "
694 "plaintext password\n", string_arg);
698 if (!wbinfo_auth_crap(string_arg)) {
699 printf("Could not authenticate user %s with "
700 "challenge/response\n", string_arg);
710 if (!wbinfo_ping()) {
711 printf("could not ping winbindd!\n");
716 case OPT_SET_AUTH_USER:
717 if (!(wbinfo_set_auth_user(string_arg))) {
722 fprintf(stderr, "Invalid option\n");