2 * Copyright (c) 1991, 1992, 1993
3 * Regents of the University of Michigan. All rights reserved.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
12 * The University of Michigan would like to thank the following people for
13 * their contributions to this piece of software:
15 * Robert Urquhart <robert@sfu.ca>
16 * Simon Fraser University, Academic Computing Services
24 extern char *strdup (const char *);
25 extern char *getenv(const char *);
31 #include <ac/signal.h>
32 #include <ac/string.h>
34 #include <ac/termios.h>
36 #include <ac/unistd.h>
38 #ifdef HAVE_SYS_FILE_H
45 #include <ldapconfig.h>
50 "@(#) Copyright (c) 1991, 1992, 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
54 * Used with change_base() to indicate which base we are changing.
59 #define iscom(x) (!strncasecmp(x, cmd, strlen(cmd)))
61 static char *server = NULL;
62 static char *config_file = UD_CONFIG_FILE;
63 static char *filter_file = FILTERFILE;
64 static int ldap_port = LDAP_PORT;
65 static int dereference = TRUE;
67 char *default_bind_object = NULL;
69 char *bound_dn; /* bound user's Distinguished Name */
70 char *group_base; /* place in LDAP tree where groups are */
71 char *search_base; /* place in LDAP tree where searches start */
73 static jmp_buf env; /* spot to jump to on an interrupt */
75 int lpp; /* lines per page */
76 int verbose; /* 1 if verbose mode on */
77 int col_size; /* characters across on the screen */
78 int bind_status; /* user's bind status */
80 LDAP *ld; /* LDAP descriptor */
81 LDAPFiltDesc *lfdp; /* LDAP filter descriptor */
84 int debug; /* debug flag */
89 main( int argc, char **argv )
91 extern char *optarg; /* for parsing argv */
92 register int c; /* for parsing argv */
93 register char *cp; /* for parsing Version */
97 /* handle argument list */
98 while ((c = getopt(argc, argv, "c:d:Df:l:p:s:u:vV")) != -1) {
102 ldap_debug = (int) strtol(optarg, (char **) NULL, 0);
103 lber_debug = ldap_debug;
108 debug = (int) strtol(optarg, (char **) NULL, 0);
112 server = strdup(optarg);
115 filter_file = strdup(optarg);
118 config_file = optarg;
121 ldap_port = atoi(optarg);
124 default_bind_object = strdup(optarg);
127 verbose = 1; /* this is the default anyways... */
133 printf("\n\n Debug flag values\n\n");
134 printf(" 1 function trace\n");
135 printf(" 2 find() information\n");
136 printf(" 4 group information\n");
137 printf(" 8 mod() information\n");
138 printf(" 16 parsing information\n");
139 printf(" 32 output information\n");
140 printf(" 64 authentication information\n");
141 printf(" 128 initialization information\n\n");
142 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);
145 fprintf(stderr, "Usage: %s [-c filter-config-file] [-d debug-level] [-l ldap-debug-level] [-s server] [-p port] [-V]\n", argv[0]);
151 /* just print the first line of Version[] */
152 cp = strchr(Version, '\t');
159 initialize_attribute_strings();
161 /* now tackle the user's commands */
169 LDAPMessage *mp; /* returned by find() */
170 register char *cp; /* misc char pointer */
171 register char *ap; /* misc char pointer */
172 static char buf[MED_BUF_SIZE]; /* for prompting */
173 static char cmd[MED_BUF_SIZE]; /* holds the command */
174 static char input[MED_BUF_SIZE]; /* buffer for input */
178 printf("->do_commands()\n");
181 printf("\n Enter a command. If you need help, type 'h' or '?' and hit RETURN.\n\n");
182 /* jump here on an interrupt */
188 /* Temporary kludge - if cp is null, dumps core under Solaris */
191 fetch_buffer(input, sizeof(input), stdin);
192 if (*input == '\0') {
199 if (memset(cmd, '\0', sizeof(cmd)) == NULL)
201 while (!isspace(*cp) && (*cp != '\0'))
205 else if (iscom("stop") || iscom("quit"))
207 else if (iscom("cb") || iscom("cd") || iscom("moveto")) {
208 while (isspace(*cp) && (*cp != '\0'))
210 if (!strncasecmp(cp, "base", 4))
212 change_base(BASE_SEARCH, &search_base, nextstr(cp));
214 else if (iscom("memberships"))
215 (void) list_memberships(nextstr(cp));
216 else if (iscom("list"))
217 (void) list_groups(nextstr(cp));
218 else if (iscom("groupbase"))
219 change_base(BASE_GROUPS, &group_base, nextstr(cp));
220 else if (iscom("find") || iscom("display") || iscom("show")) {
222 if ((mp = find(cp, FALSE)) != NULL) {
228 printf(" Could not find \"%s\".\n", cp);
231 else if (iscom("vedit") && isatty( 1 )) {
233 else if (iscom("vedit")) {
235 (void) edit(nextstr(cp));
237 else if (iscom("modify") || iscom("change") || iscom("alter"))
238 (void) modify(nextstr(cp));
239 else if (iscom("bind") || iscom("iam"))
240 (void) auth(nextstr(cp), 0);
241 else if ((cmd[0] == '?') || iscom("help"))
242 print_help(nextstr(cp));
243 else if (iscom("join") || iscom("subscribe"))
244 (void) x_group(G_JOIN, nextstr(cp));
245 else if (iscom("resign") || iscom("unsubscribe"))
246 (void) x_group(G_RESIGN, nextstr(cp));
247 else if (!strncasecmp("create", cmd, strlen(cmd)))
248 add_group(nextstr(cp));
249 else if (!strncasecmp("remove", cmd, strlen(cmd)))
250 remove_group(nextstr(cp));
251 else if (!strncasecmp("purge", cmd, strlen(cmd)))
252 purge_group(nextstr(cp));
253 else if (!strncasecmp("verbose", cmd, strlen(cmd))) {
254 verbose = 1 - verbose;
256 printf(" Verbose mode has been turned on.\n");
258 else if (!strncasecmp("dereference", cmd, strlen(cmd))) {
260 dereference = 1 - dereference;
261 if (dereference == 1) {
262 deref = LDAP_DEREF_ALWAYS;
264 deref = LDAP_DEREF_NEVER;
266 ldap_set_option(ld, LDAP_OPT_DEREF, (void *) &deref);
268 else if (!strncasecmp("tidy", cmd, strlen(cmd)))
270 else if (cmd[0] == '\0')
273 printf(" Invalid command. Type \"help commands.\"\n");
275 printf(" Thank you!\n");
288 register char **rdns;
293 printf("->status()\n");
295 printf(" Current server is %s", server);
299 ldap_get_option(ld, LDAP_OPT_HOST_NAME, &host);
301 if (( host != NULL ) && (strcasecmp( host, server ) != 0 )) {
302 printf( " (%s)", host );
306 printbase(" Search base is ", search_base);
307 printbase(" Group base is ", group_base);
308 if ( bound_dn != NULL ) {
309 rdns = ldap_explode_dn(bound_dn, TRUE);
310 printf(" Bound as \"%s\"\n", *rdns);
311 ldap_value_free(rdns);
313 printf(" Bound as Nobody\n" );
315 printf( " Verbose mode is %sabled\n", ( verbose ? "en" : "dis" ));
317 int deref = LDAP_DEREF_NEVER;
318 ldap_get_option(ld, LDAP_OPT_DEREF, &deref);
319 printf( " Aliases are %sbeing dereferenced\n",
320 ( deref == LDAP_DEREF_ALWAYS ) ? "" : "not" );
325 change_base( int type, char **base, char *s )
327 register char *cp; /* utility pointers */
328 char **rdns; /* for parsing */
329 char *output_string; /* for nice output */
330 int num_picked; /* # of selected base */
331 int j; /* used with num_picked */
332 int i = 1; /* index into choices array */
333 int matches; /* # of matches found */
334 int rest = 1; /* # left to display */
335 char tmp[MED_BUF_SIZE]; /* temporary buffer */
336 static char *choices[MED_BUF_SIZE]; /* bases from which to choose */
337 static char resp[SMALL_BUF_SIZE]; /* for prompting user */
338 static char buf[MED_BUF_SIZE];
339 static char *attrs[] = { "objectClass", NULL };
340 LDAPMessage *mp; /* results from a search */
341 LDAPMessage *ep; /* for going thru bases */
345 printf("->change_base(%s, %s)\n", s, s);
348 * If s is NULL we need to prompt the user for an argument.
352 printf(" You need to specify how the base is to be changed. Valid choices are:\n");
353 printf(" ? - list the choices immediately below this level\n");
354 printf(" .. - move up one level in the Directory tree\n");
355 printf(" root - move to the root of the Directory tree\n");
356 printf(" default - move to the default level built into this program\n");
357 printf(" <entry> - move to the entry specified\n");
359 printf(" Change base to? ");
361 fetch_buffer(buf, sizeof(buf), stdin);
362 if ((buf != NULL) && (buf[0] != '\0'))
366 /* set the output string */
367 if (type == BASE_SEARCH)
368 output_string = " Search base is now ";
369 else if (type == BASE_GROUPS)
370 output_string = " Group base is now ";
372 if (!strcasecmp(s, "root")) {
373 StrFreeDup(base, NULL);
374 printbase(" Search base is ", *base);
379 * User wants to ascend one level in the LDAP tree.
380 * Easy: Just strip off the first element of the
381 * current search base, unless it's the root, in
382 * which case we just do nothing.
384 if (!strcasecmp(s, "..")) {
386 printf(" You are already at the root\n");
389 cp = strchr(*base, '=');
392 * If there isn't a second "=" in the base, then this was
393 * a one element base, and so now it should be NULL.
395 if ((cp = strchr(cp, '=')) == NULL)
396 StrFreeDup(base, NULL);
399 * Back up to the start of this
403 * sequence now that 'cp' is pointing to the '='.
409 * Goofy, but need to do it this way since both *base
410 * and cp point into the same chunk of memory, and
411 * we want to free *base, but keep part of it around.
414 StrFreeDup(base, cp);
417 printbase(output_string, *base);
421 /* user wants to see what is directly below this level */
424 * Fetch the list of entries directly below this level.
425 * Once we have the list, we will print it for the user, one
426 * screenful at a time. At the end of each screen, we ask
427 * the user if they want to see more. They can also just
428 * type a number at that point too.
430 if (ldap_search_s(ld, *base, LDAP_SCOPE_ONELEVEL, "(|(objectClass=quipuNonLeafObject)(objectClass=externalNonLeafObject))", attrs, FALSE, &mp) != LDAP_SUCCESS) {
432 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
433 if ((ld_errno == LDAP_TIMELIMIT_EXCEEDED) ||
434 (ld_errno == LDAP_SIZELIMIT_EXCEEDED)) {
436 printf(" Your query was too general and a limit was exceeded. The results listed\n");
437 printf(" are not complete. You may want to try again with a more refined query.\n\n");
440 printf(" Time or size limit exceeded. Partial results follow.\n\n");
442 ldap_perror(ld, "ldap_search_s");
446 if ((matches = ldap_count_entries(ld, mp)) < 1) {
447 printf(" There is nothing below this level.\n");
448 (void) ldap_msgfree(mp);
452 printf(" There are %d choices:\n", matches);
453 for (ep = ldap_first_entry(ld, mp); ep != NULL; ep = ldap_next_entry(ld, ep)) {
455 * Put the last component of the DN into 'lastDN'.
456 * If we are at the root level, convert any country
457 * codes to recognizable names for printing.
459 choices[i] = ldap_get_dn(ld, ep);
460 rdns = ldap_explode_dn(choices[i], TRUE);
461 printf(" %2d. %s\n", i, friendly_name(*rdns));
462 (void) ldap_value_free(rdns);
464 if ((rest++ > (lpp - 3)) && (i < matches)) {
467 fetch_buffer(resp, sizeof(resp), stdin);
468 if ((resp[0] == 'n') || (resp[0] == 'N'))
470 else if (((num_picked = atoi(resp)) != 0) && (num_picked < i))
476 if (num_picked != 0) {
481 printf(" Which number? ");
483 fetch_buffer(resp, sizeof(resp), stdin);
487 (void) ldap_msgfree(mp);
488 for (i = 0; i < matches; i++)
492 if ((j < 1) || (j >= i))
493 printf(" Invalid number\n");
495 StrFreeDup(base, choices[j]);
496 printbase(output_string, *base);
497 (void) ldap_msgfree(mp);
498 for (i = 0; choices[i] != NULL; i++)
504 /* set the search base back to the original default value */
505 else if (!strcasecmp(s, "default")) {
506 if (type == BASE_SEARCH)
507 StrFreeDup(base, NULL);
508 else if (type == BASE_GROUPS)
509 StrFreeDup(base, UD_WHERE_GROUPS_ARE_CREATED);
510 printbase(output_string, *base);
512 /* they typed in something -- see if it is legit */
514 /* user cannot do something like 'cb 33' */
516 printf(" \"%s\" is not a valid search base\n", s);
517 printf(" Base unchanged.\n");
518 printf(" Try using 'cb ?'\n");
521 /* was it a fully-specified DN? */
524 printbase(output_string, *base);
527 /* was it a RDN relative to the current base? */
528 sprintf(tmp, "ou=%s, %s", s, *base);
530 StrFreeDup(base, tmp);
531 printbase(output_string, *base);
534 printf(" \"%s\" is not a valid base\n Base unchanged.\n", s);
539 initialize_client( void )
541 FILE *fp; /* for config file */
542 static char buffer[MED_BUF_SIZE]; /* for input */
543 struct passwd *pw; /* for getting the home dir */
544 register char *cp; /* for fiddling with buffer */
545 char *term; /* for tty set-up */
546 char *config; /* config file to use */
547 static char bp[1024]; /* for tty set-up */
551 printf("->initialize_client()\n");
554 * A per-user config file has precedence over any system-wide
555 * config file, if one exists.
557 #ifdef HAVE_GETPWUID_H
558 if ((pw = getpwuid((uid_t) geteuid())) == (struct passwd *) NULL)
559 config = config_file;
561 if (pw->pw_dir == NULL)
562 config = config_file;
564 sprintf(buffer, "%s/%s", pw->pw_dir,
565 UD_USER_CONFIG_FILE);
566 if (access(buffer, R_OK) == 0)
569 config = config_file;
572 #endif /* getpwduid() */
574 if (debug & D_INITIALIZE)
575 printf("Using config file %s\n", config);
579 * If there is a config file, read it.
581 * Could have lines that look like this:
583 * server <ip-address or domain-name>
584 * base <default search base>
585 * groupbase <default place where groups are created>
588 if ((fp = fopen(config, "r")) != NULL) {
589 while (fgets(buffer, sizeof(buffer), fp) != NULL) {
590 buffer[strlen(buffer) - 1] = '\0';
591 if (!strncasecmp(buffer, "server", 6)) {
597 if ((*cp == '\0') || (*cp == '\n'))
601 else if (!strncasecmp(buffer, "host", 4)) {
607 if ((*cp == '\0') || (*cp == '\n'))
611 else if (!strncasecmp(buffer, "base", 4)) {
615 if ((*cp == '\0') || (*cp == '\n'))
617 search_base = strdup(cp);
619 else if (!strncasecmp(buffer, "groupbase", 9)) {
623 if ((*cp == '\0') || (*cp == '\n'))
625 group_base = strdup(cp);
628 fprintf(stderr, "?? -> %s\n", buffer);
631 if (group_base == NULL)
632 group_base = strdup(UD_WHERE_GROUPS_ARE_CREATED);
635 * Set up our LDAP connection. The values of retry and timeout
636 * are meaningless since we will immediately be doing a null bind
637 * because we want to be sure to use TCP, not UDP.
639 if ((ld = ldap_open(server, ldap_port)) == NULL) {
640 fprintf(stderr, " The LDAP Directory is temporarily unavailable. Please try again later.\n");
644 if (ldap_bind_s(ld, (char *) default_bind_object, NULL,
645 LDAP_AUTH_SIMPLE) != LDAP_SUCCESS) {
647 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
649 fprintf(stderr, " The LDAP Directory is temporarily unavailable. Please try again later.\n");
650 if (ld_errno != LDAP_UNAVAILABLE)
651 ldap_perror(ld, " ldap_bind_s");
656 int deref = LDAP_DEREF_ALWAYS;
657 ldap_set_option(ld, LDAP_OPT_DEREF, (void *) &deref);
659 bind_status = UD_NOT_BOUND;
660 if ( default_bind_object != NULL ) {
661 bound_dn = strdup(default_bind_object);
666 /* enabled local caching of ldap results, 15 minute lifetime */
667 ldap_enable_cache( ld, 60 * 15, 0 ); /* no memory limit */
669 /* initialize the search filters */
670 if ((lfdp = ldap_init_getfilter(filter_file)) == NULL) {
671 fprintf(stderr, " Problem with ldap_init_getfilter\n");
676 /* terminal initialization stuff goes here */
677 lpp = DEFAULT_TTY_HEIGHT;
678 col_size = DEFAULT_TTY_WIDTH;
680 (void) SIGNAL (SIGINT, attn);
684 struct winsize win; /* for tty set-up */
686 if (((term = getenv("TERM")) == NULL) || (tgetent(bp, term) <= 0))
690 if (ioctl(fileno(stdout), TIOCGWINSZ, &win) < 0) {
692 col_size = tgetnum("co");
696 if ((lpp = win.ws_row) == 0)
698 if ((col_size = win.ws_col) == 0)
699 col_size = tgetnum("co");
700 if ((lpp <= 0) || tgetflag("hc"))
701 lpp = DEFAULT_TTY_HEIGHT;
702 if ((col_size <= 0) || tgetflag("hc"))
703 col_size = DEFAULT_TTY_WIDTH;
706 (void) SIGNAL (SIGWINCH, chwinsz);
717 printf("\n\n INTERRUPTED!\n");
719 (void) SIGNAL (SIGINT, attn);
724 #if !defined(NO_TERMCAP) && defined(TIOCGWINSZ)
730 (void) SIGNAL (SIGWINCH, SIG_IGN);
731 if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) {
735 col_size = win.ws_col;
738 (void) SIGNAL (SIGWINCH, chwinsz);