3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1991, 1992, 1993
8 * Regents of the University of Michigan. All rights reserved.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that this notice is preserved and that due credit is given
12 * to the University of Michigan at Ann Arbor. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission. This software
15 * is provided ``as is'' without express or implied warranty.
17 * The University of Michigan would like to thank the following people for
18 * their contributions to this piece of software:
20 * Robert Urquhart <robert@sfu.ca>
21 * Simon Fraser University, Academic Computing Services
28 #include <ac/stdlib.h>
36 #include <ac/signal.h>
37 #include <ac/string.h>
39 #include <ac/termios.h>
41 #include <ac/unistd.h>
43 #ifdef HAVE_SYS_FILE_H
49 #include "ldap_defaults.h"
53 * Used with change_base() to indicate which base we are changing.
58 #define iscom(x) (!strncasecmp((x), cmd, strlen(cmd)))
60 static char *server = NULL;
61 static char *config_file = UD_CONFIG_FILE;
62 static char *filter_file = FILTERFILE;
63 static int ldap_port = 0;
64 static int dereference = TRUE;
66 char *default_bind_object = NULL;
68 char *bound_dn; /* bound user's Distinguished Name */
69 char *group_base; /* place in LDAP tree where groups are */
70 char *search_base; /* place in LDAP tree where searches start */
72 static jmp_buf env; /* spot to jump to on an interrupt */
74 int lpp; /* lines per page */
75 int verbose; /* 1 if verbose mode on */
76 int col_size; /* characters across on the screen */
77 int bind_status; /* user's bind status */
79 LDAP *ld; /* LDAP descriptor */
80 LDAPFiltDesc *lfdp; /* LDAP filter descriptor */
83 int debug; /* debug flag */
85 int ldebug; /* library debug flag */
87 #ifndef HAVE_MKVERSION
88 char Version[] = OPENLDAP_PACKAGE " " OPENLDAP_VERSION " UserDirectory (ud)";
92 main( int argc, char **argv )
94 register int c; /* for parsing argv */
95 register char *cp; /* for parsing Version */
99 /* handle argument list */
100 while ((c = getopt(argc, argv, "c:d:Df:l:p:s:u:vV")) != -1) {
103 ldebug |= (int) strtol(optarg, (char **) NULL, 0);
107 debug |= (int) strtol(optarg, (char **) NULL, 0);
111 server = strdup(optarg);
114 filter_file = strdup(optarg);
117 config_file = optarg;
120 ldap_port = atoi(optarg);
123 default_bind_object = strdup(optarg);
126 verbose = 1; /* this is the default anyways... */
132 printf("\n\n Debug flag values\n\n");
133 printf(" 1 function trace\n");
134 printf(" 2 find() information\n");
135 printf(" 4 group information\n");
136 printf(" 8 mod() information\n");
137 printf(" 16 parsing information\n");
138 printf(" 32 output information\n");
139 printf(" 64 authentication information\n");
140 printf(" 128 initialization information\n\n");
141 format("These are masks, and may be added to form multiple debug levels. For example, '-d 35' would perform a function trace, print out information about the find() function, and would print out information about the output routines too.", 75, 2);
142 exit( EXIT_SUCCESS );
144 fprintf(stderr, "Usage: %s [-c filter-config-file] [-d debug-level] [-l ldap-debug-level] [-s server] [-p port] [-V]\n", argv[0]);
150 /* just print the first line of Version[] */
151 cp = strchr(Version, '\t');
158 (void) SIGNAL (SIGPIPE, SIG_IGN);
162 initialize_attribute_strings();
164 /* now tackle the user's commands */
174 LDAPMessage *mp; /* returned by find() */
175 register char *cp; /* misc char pointer */
176 register char *ap; /* misc char pointer */
177 static char cmd[MED_BUF_SIZE]; /* holds the command */
178 static char input[MED_BUF_SIZE]; /* buffer for input */
182 printf("->do_commands()\n");
185 printf("\n Enter a command. If you need help, type 'h' or '?' and hit RETURN.\n\n");
186 /* jump here on an interrupt */
192 /* Temporary kludge - if cp is null, dumps core under Solaris */
195 fetch_buffer(input, sizeof(input), stdin);
196 if (*input == '\0') {
200 while (isspace((unsigned char)*cp))
203 if (memset(cmd, '\0', sizeof(cmd)) == NULL)
205 while (!isspace((unsigned char)*cp) && (*cp != '\0'))
209 else if (iscom("stop") || iscom("quit"))
211 else if (iscom("cb") || iscom("cd") || iscom("moveto")) {
212 while (isspace((unsigned char)*cp) && (*cp != '\0'))
214 if (!strncasecmp(cp, "base", 4))
216 change_base(BASE_SEARCH, &search_base, nextstr(cp));
218 else if (iscom("memberships"))
219 (void) list_memberships(nextstr(cp));
220 else if (iscom("list"))
221 (void) list_groups(nextstr(cp));
222 else if (iscom("groupbase"))
223 change_base(BASE_GROUPS, &group_base, nextstr(cp));
224 else if (iscom("find") || iscom("display") || iscom("show")) {
226 if ((mp = find(cp, FALSE)) != NULL) {
232 printf(" Could not find \"%s\".\n", cp);
235 else if (iscom("vedit") && isatty( 1 )) {
237 else if (iscom("vedit")) {
239 (void) edit(nextstr(cp));
241 else if (iscom("modify") || iscom("change") || iscom("alter"))
242 (void) modify(nextstr(cp));
243 else if (iscom("bind") || iscom("iam"))
244 (void) auth(nextstr(cp), 0);
245 else if ((cmd[0] == '?') || iscom("help"))
246 print_help(nextstr(cp));
247 else if (iscom("join") || iscom("subscribe"))
248 (void) x_group(G_JOIN, nextstr(cp));
249 else if (iscom("resign") || iscom("unsubscribe"))
250 (void) x_group(G_RESIGN, nextstr(cp));
251 else if (!strncasecmp("create", cmd, strlen(cmd)))
252 add_group(nextstr(cp));
253 else if (!strncasecmp("remove", cmd, strlen(cmd)))
254 remove_group(nextstr(cp));
255 else if (!strncasecmp("purge", cmd, strlen(cmd)))
256 purge_group(nextstr(cp));
257 else if (!strncasecmp("verbose", cmd, strlen(cmd))) {
258 verbose = 1 - verbose;
260 printf(" Verbose mode has been turned on.\n");
262 else if (!strncasecmp("dereference", cmd, strlen(cmd))) {
264 dereference = 1 - dereference;
265 if (dereference == 1) {
266 deref = LDAP_DEREF_ALWAYS;
268 deref = LDAP_DEREF_NEVER;
270 ldap_set_option(ld, LDAP_OPT_DEREF, (void *) &deref);
272 else if (!strncasecmp("tidy", cmd, strlen(cmd)))
274 else if (cmd[0] == '\0')
277 printf(" Invalid command. Type \"help commands.\"\n");
279 printf(" Thank you!\n");
282 exit( EXIT_SUCCESS );
289 register char **rdns;
293 printf("->status()\n");
295 printf(" Current server is %s", server != NULL ? server : "<default>" );
299 ldap_get_option(ld, LDAP_OPT_HOST_NAME, &host);
302 ( server == NULL || strcasecmp( host, server ) != 0 ) )
304 printf( " (%s)", host );
308 printbase(" Search base is ", search_base);
309 printbase(" Group base is ", group_base);
310 if ( bound_dn != NULL ) {
311 rdns = ldap_explode_dn(bound_dn, TRUE);
312 printf(" Bound as \"%s\"\n", *rdns);
313 ldap_value_free(rdns);
315 printf(" Bound as Nobody\n" );
317 printf( " Verbose mode is %sabled\n", ( verbose ? "en" : "dis" ));
319 int deref = LDAP_DEREF_NEVER;
320 ldap_get_option(ld, LDAP_OPT_DEREF, &deref);
321 printf( " Aliases are %sbeing dereferenced\n",
322 ( deref == LDAP_DEREF_ALWAYS ) ? "" : "not" );
327 change_base( int type, char **base, char *s )
329 register char *cp; /* utility pointers */
330 char *output_string = NULL; /* for nice output */
331 int num_picked; /* # of selected base */
332 int j; /* used with num_picked */
333 int i = 1; /* index into choices array */
334 int matches; /* # of matches found */
335 int rest = 1; /* # left to display */
336 char tmp[MED_BUF_SIZE]; /* temporary buffer */
337 static char *choices[MED_BUF_SIZE]; /* bases from which to choose */
338 static char resp[SMALL_BUF_SIZE]; /* for prompting user */
339 static char buf[MED_BUF_SIZE];
340 static char *attrs[] = { "objectClass", NULL };
341 LDAPMessage *mp; /* results from a search */
342 LDAPMessage *ep; /* for going thru bases */
346 printf("->change_base(%s, %s)\n", s, s);
349 * If s is NULL we need to prompt the user for an argument.
353 printf(" You need to specify how the base is to be changed. Valid choices are:\n");
354 printf(" ? - list the choices immediately below this level\n");
355 printf(" .. - move up one level in the Directory tree\n");
356 printf(" root - move to the root of the Directory tree\n");
357 printf(" default - move to the default level built into this program\n");
358 printf(" <entry> - move to the entry specified\n");
360 printf(" Change base to? ");
362 fetch_buffer(buf, sizeof(buf), stdin);
363 if ((buf != NULL) && (buf[0] != '\0'))
369 /* set the output string */
370 if (type == BASE_SEARCH)
371 output_string = " Search base is now ";
372 else if (type == BASE_GROUPS)
373 output_string = " Group base is now ";
375 if (!strcasecmp(s, "root")) {
376 StrFreeDup(base, NULL);
377 printbase(" Search base is ", *base);
382 * User wants to ascend one level in the LDAP tree.
383 * Easy: Just strip off the first element of the
384 * current search base, unless it's the root, in
385 * which case we just do nothing.
387 if (!strcasecmp(s, "..")) {
389 printf(" You are already at the root\n");
392 cp = strchr(*base, '=');
395 * If there isn't a second "=" in the base, then this was
396 * a one element base, and so now it should be NULL.
398 if ((cp = strchr(cp, '=')) == NULL)
399 StrFreeDup(base, NULL);
402 * Back up to the start of this
406 * sequence now that 'cp' is pointing to the '='.
408 while(!isspace((unsigned char)*cp))
412 * Goofy, but need to do it this way since both *base
413 * and cp point into the same chunk of memory, and
414 * we want to free *base, but keep part of it around.
417 StrFreeDup(base, cp);
420 printbase(output_string, *base);
424 /* user wants to see what is directly below this level */
427 * Fetch the list of entries directly below this level.
428 * Once we have the list, we will print it for the user, one
429 * screenful at a time. At the end of each screen, we ask
430 * the user if they want to see more. They can also just
431 * type a number at that point too.
433 if (ldap_search_s(ld, *base, LDAP_SCOPE_ONELEVEL, NULL, attrs, FALSE, &mp) != LDAP_SUCCESS) {
435 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
436 if ((ld_errno == LDAP_TIMELIMIT_EXCEEDED) ||
437 (ld_errno == LDAP_SIZELIMIT_EXCEEDED)) {
439 printf(" Your query was too general and a limit was exceeded. The results listed\n");
440 printf(" are not complete. You may want to try again with a more refined query.\n\n");
443 printf(" Time or size limit exceeded. Partial results follow.\n\n");
445 ldap_perror(ld, "ldap_search_s");
449 if ((matches = ldap_count_entries(ld, mp)) < 1) {
450 printf(" There is nothing below this level.\n");
451 (void) ldap_msgfree(mp);
455 printf(" There are %d choices:\n", matches);
456 for (ep = ldap_first_entry(ld, mp); ep != NULL; ep = ldap_next_entry(ld, ep)) {
458 * Put the last component of the DN into 'lastDN'.
459 * If we are at the root level, convert any country
460 * codes to recognizable names for printing.
462 choices[i] = ldap_get_dn(ld, ep);
463 printf(" %2d. %s\n", i, choices[i]);
465 if ((rest++ > (lpp - 3)) && (i < matches)) {
468 fetch_buffer(resp, sizeof(resp), stdin);
469 if ((resp[0] == 'n') || (resp[0] == 'N'))
471 else if (((num_picked = atoi(resp)) != 0) && (num_picked < i))
477 if (num_picked != 0) {
482 printf(" Which number? ");
484 fetch_buffer(resp, sizeof(resp), stdin);
488 (void) ldap_msgfree(mp);
489 for (i = 0; i < matches; i++)
490 ldap_memfree(choices[i]);
493 if ((j < 1) || (j >= i))
494 printf(" Invalid number\n");
496 StrFreeDup(base, choices[j]);
497 printbase(output_string, *base);
498 (void) ldap_msgfree(mp);
499 for (i = 0; choices[i] != NULL; i++)
500 ldap_memfree(choices[i]);
505 /* set the search base back to the original default value */
506 else if (!strcasecmp(s, "default")) {
507 if (type == BASE_SEARCH)
508 StrFreeDup(base, NULL);
509 else if (type == BASE_GROUPS)
510 StrFreeDup(base, UD_WHERE_GROUPS_ARE_CREATED);
511 printbase(output_string, *base);
513 /* they typed in something -- see if it is legit */
515 /* user cannot do something like 'cb 33' */
517 printf(" \"%s\" is not a valid search base\n", s);
518 printf(" Base unchanged.\n");
519 printf(" Try using 'cb ?'\n");
522 /* was it a fully-specified DN? */
525 printbase(output_string, *base);
528 /* was it a RDN relative to the current base? */
529 sprintf(tmp, "ou=%s, %s", s, *base);
531 StrFreeDup(base, tmp);
532 printbase(output_string, *base);
535 printf(" \"%s\" is not a valid base\n Base unchanged.\n", s);
540 initialize_client( void )
542 FILE *fp; /* for config file */
543 static char buffer[MED_BUF_SIZE]; /* for input */
545 struct passwd *pw; /* for getting the home dir */
547 register char *cp; /* for fiddling with buffer */
548 char *config; /* config file to use */
549 static char bp[1024]; /* for tty set-up */
553 printf("->initialize_client()\n");
557 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldebug );
558 ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &ldebug );
562 * A per-user config file has precedence over any system-wide
563 * config file, if one exists.
566 if ((pw = getpwuid((uid_t) geteuid())) == (struct passwd *) NULL)
567 config = config_file;
569 if (pw->pw_dir == NULL)
570 config = config_file;
572 sprintf(buffer, "%s/%s", pw->pw_dir,
573 UD_USER_CONFIG_FILE);
574 if (access(buffer, R_OK) == 0)
577 config = config_file;
581 config = config_file;
582 #endif /* getpwduid() */
584 if (debug & D_INITIALIZE)
585 printf("Using config file %s\n", config);
589 * If there is a config file, read it.
591 * Could have lines that look like this:
593 * server <ip-address or domain-name>
594 * base <default search base>
595 * groupbase <default place where groups are created>
598 if ((fp = fopen(config, "r")) != NULL) {
599 while (fgets(buffer, sizeof(buffer), fp) != NULL) {
600 buffer[strlen(buffer) - 1] = '\0';
601 if (!strncasecmp(buffer, "server", 6)) {
605 while (isspace((unsigned char)*cp))
607 if ((*cp == '\0') || (*cp == '\n'))
611 else if (!strncasecmp(buffer, "host", 4)) {
615 while (isspace((unsigned char)*cp))
617 if ((*cp == '\0') || (*cp == '\n'))
621 else if (!strncasecmp(buffer, "base", 4)) {
623 while (isspace((unsigned char)*cp))
625 if ((*cp == '\0') || (*cp == '\n'))
627 search_base = strdup(cp);
629 else if (!strncasecmp(buffer, "groupbase", 9)) {
631 while (isspace((unsigned char)*cp))
633 if ((*cp == '\0') || (*cp == '\n'))
635 group_base = strdup(cp);
638 fprintf(stderr, "?? -> %s\n", buffer);
641 if (group_base == NULL)
642 group_base = strdup(UD_WHERE_GROUPS_ARE_CREATED);
645 * Set up our LDAP connection. The values of retry and timeout
646 * are meaningless since we will immediately be doing a null bind
647 * because we want to be sure to use TCP, not UDP.
649 if ((ld = ldap_init(server, ldap_port)) == NULL) {
650 fprintf(stderr, " Initialization of LDAP session failed.\n");
651 exit( EXIT_FAILURE );
654 if (ldap_bind_s(ld, (char *) default_bind_object, NULL,
655 LDAP_AUTH_SIMPLE) != LDAP_SUCCESS) {
656 ldap_perror(ld, " ldap_bind_s");
657 exit( EXIT_FAILURE );
661 int deref = LDAP_DEREF_ALWAYS;
662 ldap_set_option(ld, LDAP_OPT_DEREF, (void *) &deref);
664 bind_status = UD_NOT_BOUND;
665 if ( default_bind_object != NULL ) {
666 bound_dn = strdup(default_bind_object);
671 /* enabled local caching of ldap results, 15 minute lifetime */
672 ldap_enable_cache( ld, 60 * 15, 0 ); /* no memory limit */
674 /* initialize the search filters */
675 if ((lfdp = ldap_init_getfilter(filter_file)) == NULL) {
676 fprintf(stderr, " Problem with ldap_init_getfilter\n");
681 /* terminal initialization stuff goes here */
682 lpp = DEFAULT_TTY_HEIGHT;
683 col_size = DEFAULT_TTY_WIDTH;
685 (void) SIGNAL (SIGINT, attn);
691 if (((term = getenv("TERM")) == NULL) || (tgetent(bp, term) <= 0))
695 struct winsize win; /* for tty set-up */
696 if (ioctl(fileno(stdout), TIOCGWINSZ, &win) >= 0) {
697 if ((lpp = win.ws_row) == 0)
699 if ((col_size = win.ws_col) == 0)
700 col_size = tgetnum("co");
701 if ((lpp <= 0) || tgetflag("hc"))
702 lpp = DEFAULT_TTY_HEIGHT;
703 if ((col_size <= 0) || tgetflag("hc"))
704 col_size = DEFAULT_TTY_WIDTH;
705 (void) SIGNAL (SIGWINCH, chwinsz);
710 col_size = tgetnum("co");
722 printf("\n\n INTERRUPTED!\n");
724 (void) SIGNAL_REINSTALL (SIGINT, attn);
729 #if !defined(NO_TERMCAP) && defined(TIOCGWINSZ)
735 (void) SIGNAL (SIGWINCH, SIG_IGN);
736 if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) {
740 col_size = win.ws_col;
743 (void) SIGNAL_REINSTALL (SIGWINCH, chwinsz);