link in OS2-Client-HOWTO to SWAT main page
[Samba.git] / source / nsswitch / wbinfo.c
blobcff2b8b69a0566cfd2f86f34a5d37c811fc9a2a6
1 /*
2 Unix SMB/Netbios implementation.
3 Version 2.0
5 Winbind status program.
7 Copyright (C) Tim Potter 2000
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.
24 #include "includes.h"
25 #include "winbind_nss_config.h"
26 #include "winbindd.h"
27 #include "debug.h"
29 /* Prototypes from common.h - only needed #if TNG */
31 enum nss_status winbindd_request(int req_type,
32 struct winbindd_request *request,
33 struct winbindd_response *response);
35 /* List groups a user is a member of */
37 static BOOL wbinfo_get_usergroups(char *user)
39 struct winbindd_request request;
40 struct winbindd_response response;
41 int result, i;
43 ZERO_STRUCT(response);
45 /* Send request */
47 fstrcpy(request.data.username, user);
49 result = winbindd_request(WINBINDD_GETGROUPS, &request, &response);
51 if (result != NSS_STATUS_SUCCESS) {
52 return False;
55 for (i = 0; i < response.data.num_entries; i++) {
56 printf("%d\n", ((gid_t *)response.extra_data)[i]);
59 return True;
62 /* List trusted domains */
64 static BOOL wbinfo_list_domains(void)
66 struct winbindd_response response;
67 fstring name;
69 ZERO_STRUCT(response);
71 /* Send request */
73 if (winbindd_request(WINBINDD_LIST_TRUSTDOM, NULL, &response) ==
74 WINBINDD_ERROR) {
75 return False;
78 /* Display response */
80 if (response.extra_data) {
81 while(next_token((char **)&response.extra_data, name, ",",
82 sizeof(fstring))) {
83 printf("%s\n", name);
87 return True;
90 /* Check trust account password */
92 static BOOL wbinfo_check_secret(void)
94 struct winbindd_response response;
95 BOOL result;
97 ZERO_STRUCT(response);
99 result = winbindd_request(WINBINDD_CHECK_MACHACC, NULL, &response) ==
100 NSS_STATUS_SUCCESS;
102 if (result) {
104 if (response.data.num_entries) {
105 printf("Secret is good\n");
106 } else {
107 printf("Secret is bad\n");
110 return True;
113 return False;
116 /* Convert uid to sid */
118 static BOOL wbinfo_uid_to_sid(uid_t uid)
120 struct winbindd_request request;
121 struct winbindd_response response;
123 ZERO_STRUCT(request);
124 ZERO_STRUCT(response);
126 /* Send request */
128 request.data.uid = uid;
129 if (winbindd_request(WINBINDD_UID_TO_SID, &request, &response) ==
130 WINBINDD_ERROR) {
131 return False;
134 /* Display response */
136 printf("%s\n", response.data.sid.sid);
138 return True;
141 /* Convert gid to sid */
143 static BOOL wbinfo_gid_to_sid(gid_t gid)
145 struct winbindd_request request;
146 struct winbindd_response response;
148 ZERO_STRUCT(request);
149 ZERO_STRUCT(response);
151 /* Send request */
153 request.data.gid = gid;
154 if (winbindd_request(WINBINDD_GID_TO_SID, &request, &response) ==
155 WINBINDD_ERROR) {
156 return False;
159 /* Display response */
161 printf("%s\n", response.data.sid.sid);
163 return True;
166 /* Convert sid to uid */
168 static BOOL wbinfo_sid_to_uid(char *sid)
170 struct winbindd_request request;
171 struct winbindd_response response;
173 ZERO_STRUCT(request);
174 ZERO_STRUCT(response);
176 /* Send request */
178 fstrcpy(request.data.sid, sid);
179 if (winbindd_request(WINBINDD_SID_TO_UID, &request, &response) ==
180 WINBINDD_ERROR) {
181 return False;
184 /* Display response */
186 printf("%d\n", response.data.uid);
188 return True;
191 static BOOL wbinfo_sid_to_gid(char *sid)
193 struct winbindd_request request;
194 struct winbindd_response response;
196 ZERO_STRUCT(request);
197 ZERO_STRUCT(response);
199 /* Send request */
201 fstrcpy(request.data.sid, sid);
202 if (winbindd_request(WINBINDD_SID_TO_GID, &request, &response) ==
203 WINBINDD_ERROR) {
204 return False;
207 /* Display response */
209 printf("%d\n", response.data.gid);
211 return True;
214 /* Convert sid to string */
216 static BOOL wbinfo_lookupsid(char *sid)
218 struct winbindd_request request;
219 struct winbindd_response response;
221 ZERO_STRUCT(request);
222 ZERO_STRUCT(response);
224 /* Send off request */
226 fstrcpy(request.data.sid, sid);
227 if (winbindd_request(WINBINDD_LOOKUPSID, &request, &response) ==
228 WINBINDD_ERROR) {
229 return False;
232 /* Display response */
234 printf("%s %d\n", response.data.name.name, response.data.name.type);
236 return True;
239 /* Convert string to sid */
241 static BOOL wbinfo_lookupname(char *name)
243 struct winbindd_request request;
244 struct winbindd_response response;
246 /* Send off request */
248 ZERO_STRUCT(request);
249 ZERO_STRUCT(response);
251 fstrcpy(request.data.name, name);
252 if (winbindd_request(WINBINDD_LOOKUPNAME, &request, &response) ==
253 WINBINDD_ERROR) {
254 return False;
257 /* Display response */
259 printf("%s %d\n", response.data.sid.sid, response.data.sid.type);
261 return True;
264 /* Print domain users */
266 static BOOL print_domain_users(void)
268 struct winbindd_response response;
269 fstring name;
271 /* Send request to winbind daemon */
273 ZERO_STRUCT(response);
275 if (winbindd_request(WINBINDD_LIST_USERS, NULL, &response) ==
276 WINBINDD_ERROR) {
277 return False;
280 /* Look through extra data */
282 if (!response.extra_data) {
283 return False;
286 while(next_token((char **)&response.extra_data, name, ",",
287 sizeof(fstring))) {
288 printf("%s\n", name);
291 return True;
294 /* Print domain groups */
296 static BOOL print_domain_groups(void)
298 struct winbindd_response response;
299 fstring name;
301 ZERO_STRUCT(response);
303 if (winbindd_request(WINBINDD_LIST_GROUPS, NULL, &response) ==
304 WINBINDD_ERROR) {
305 return False;
308 /* Look through extra data */
310 if (!response.extra_data) {
311 return False;
314 while(next_token((char **)&response.extra_data, name, ",",
315 sizeof(fstring))) {
316 printf("%s\n", name);
319 return True;
322 /* Print program usage */
324 static void usage(void)
326 printf("Usage: wbinfo -ug | -n name | -sSY sid | -UG uid/gid | -tm\n");
327 printf("\t-u\tlists all domain users\n");
328 printf("\t-g\tlists all domain groups\n");
329 printf("\t-n name\tconverts name to sid\n");
330 printf("\t-s sid\tconverts sid to name\n");
331 printf("\t-U uid\tconverts uid to sid\n");
332 printf("\t-G gid\tconverts gid to sid\n");
333 printf("\t-S sid\tconverts sid to uid\n");
334 printf("\t-Y sid\tconverts sid to gid\n");
335 printf("\t-t\tcheck shared secret\n");
336 printf("\t-m\tlist trusted domains\n");
337 printf("\t-r user\tget user groups\n");
340 /* Main program */
342 int main(int argc, char **argv)
344 extern pstring global_myname;
345 int opt;
347 /* Samba client initialisation */
349 if (!*global_myname) {
350 char *p;
352 fstrcpy(global_myname, myhostname());
353 p = strchr(global_myname, '.');
354 if (p) {
355 *p = 0;
359 TimeInit();
360 charset_initialise();
362 if (!lp_load(CONFIGFILE, True, False, False)) {
363 DEBUG(0, ("error opening config file\n"));
364 exit(1);
367 codepage_initialise(lp_client_code_page());
368 load_interfaces();
370 /* Parse command line options */
372 if (argc == 1) {
373 usage();
374 return 1;
377 while ((opt = getopt(argc, argv, "ugs:n:U:G:S:Y:tmr:")) != EOF) {
378 switch (opt) {
379 case 'u':
380 if (!print_domain_users()) {
381 printf("Error looking up domain users\n");
382 return 1;
384 break;
385 case 'g':
386 if (!print_domain_groups()) {
387 printf("Error looking up domain groups\n");
388 return 1;
390 break;
391 case 's':
392 if (!wbinfo_lookupsid(optarg)) {
393 printf("Could not lookup sid %s\n", optarg);
394 return 1;
396 break;
397 case 'n':
398 if (!wbinfo_lookupname(optarg)) {
399 printf("Could not lookup name %s\n", optarg);
400 return 1;
402 break;
403 case 'U':
404 if (!wbinfo_uid_to_sid(atoi(optarg))) {
405 printf("Could not convert uid %s to sid\n",
406 optarg);
407 return 1;
409 break;
410 case 'G':
411 if (!wbinfo_gid_to_sid(atoi(optarg))) {
412 printf("Could not convert gid %s to sid\n",
413 optarg);
414 return 1;
416 break;
417 case 'S':
418 if (!wbinfo_sid_to_uid(optarg)) {
419 printf("Could not convert sid %s to uid\n",
420 optarg);
421 return 1;
423 break;
424 case 'Y':
425 if (!wbinfo_sid_to_gid(optarg)) {
426 printf("Could not convert sid %s to gid\n",
427 optarg);
428 return 1;
430 break;
431 case 't':
432 if (!wbinfo_check_secret()) {
433 printf("Could not check secret\n");
434 return 1;
436 break;
437 case 'm':
438 if (!wbinfo_list_domains()) {
439 printf("Could not list trusted domains\n");
440 return 1;
442 break;
443 case 'r':
444 if (!wbinfo_get_usergroups(optarg)) {
445 printf("Could not get groups for user %s\n",
446 optarg);
447 return 1;
449 break;
451 /* Invalid option */
453 default:
454 usage();
455 return 1;
459 /* Clean exit */
461 return 0;