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
20 #include <sys/types.h>
30 #if defined( NeXT ) || defined( ultrix ) || defined( osf1 ) || (defined(SunOS) && SunOS < 40)
32 #else /* defined( NeXT ) || defined( ultrix ) etc. */
34 #endif /* defined( NeXT ) || defined( ultrix ) etc. */
36 #if defined( aix ) || defined( __NetBSD__ ) \
37 || defined( __FreeBSD__ ) || defined( linux )
38 #include <sys/ioctl.h>
39 #endif /* aix || __NetBSD__ */
46 #include <ldapconfig.h>
52 "@(#) Copyright (c) 1991, 1992, 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
56 * Used with change_base() to indicate which base we are changing.
61 #define iscom(x) (!strncasecmp(x, cmd, strlen(cmd)))
63 static char *server = NULL;
64 static char *config_file = UD_CONFIG_FILE;
65 static char *filter_file = FILTERFILE;
66 static int ldap_port = LDAP_PORT;
67 static int dereference = TRUE;
69 char *default_bind_object = UD_BINDDN;
71 char *bound_dn; /* bound user's Distinguished Name */
72 char *group_base; /* place in LDAP tree where groups are */
73 char *search_base; /* place in LDAP tree where searches start */
75 static jmp_buf env; /* spot to jump to on an interrupt */
77 int lpp; /* lines per page */
78 int verbose; /* 1 if verbose mode on */
79 int col_size; /* characters across on the screen */
80 int bind_status; /* user's bind status */
82 LDAP *ld; /* LDAP descriptor */
83 LDAPFiltDesc *lfdp; /* LDAP filter descriptor */
86 int debug; /* debug flag */
93 extern char Version[]; /* version number */
94 extern char *optarg; /* for parsing argv */
95 register int c; /* for parsing argv */
96 register char *cp; /* for parsing Version */
97 extern void initialize_attribute_strings();
101 /* handle argument list */
102 while ((c = getopt(argc, argv, "c:d:Df:l:p:s:u:vV")) != -1) {
106 ldap_debug = (int) strtol(optarg, (char **) NULL, 0);
107 lber_debug = ldap_debug;
112 debug = (int) strtol(optarg, (char **) NULL, 0);
116 server = strdup(optarg);
119 filter_file = strdup(optarg);
122 config_file = optarg;
125 ldap_port = atoi(optarg);
128 default_bind_object = strdup(optarg);
131 verbose = 1; /* this is the default anyways... */
137 printf("\n\n Debug flag values\n\n");
138 printf(" 1 function trace\n");
139 printf(" 2 find() information\n");
140 printf(" 4 group information\n");
141 printf(" 8 mod() information\n");
142 printf(" 16 parsing information\n");
143 printf(" 32 output information\n");
144 printf(" 64 authentication information\n");
145 printf(" 128 initialization information\n\n");
146 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);
149 fprintf(stderr, "Usage: %s [-c filter-config-file] [-d debug-level] [-l ldap-debug-level] [-s server] [-p port] [-V]\n", argv[0]);
155 /* just print the first line of Version[] */
156 cp = strchr(Version, '\t');
163 initialize_attribute_strings();
165 /* now tackle the user's commands */
172 LDAPMessage *mp; /* returned by find() */
173 register char *cp; /* misc char pointer */
174 register char *ap; /* misc char pointer */
175 static char buf[MED_BUF_SIZE]; /* for prompting */
176 static char cmd[MED_BUF_SIZE]; /* holds the command */
177 static char input[MED_BUF_SIZE]; /* buffer for input */
178 extern LDAPMessage *find();
179 extern void purge_group(), add_group(), remove_group(), x_group(),
180 tidy_up(), list_groups(), list_memberships(), edit();
181 extern char *nextstr();
185 printf("->do_commands()\n");
188 printf("\n Enter a command. If you need help, type 'h' or '?' and hit RETURN.\n\n");
189 /* jump here on an interrupt */
195 /* Temporary kludge - if cp is null, dumps core under Solaris */
198 fetch_buffer(input, sizeof(input), stdin);
199 if (*input == '\0') {
206 if (memset(cmd, '\0', sizeof(cmd)) == NULL)
208 while (!isspace(*cp) && (*cp != '\0'))
212 else if (iscom("stop") || iscom("quit"))
214 else if (iscom("cb") || iscom("cd") || iscom("moveto")) {
215 while (isspace(*cp) && (*cp != '\0'))
217 if (!strncasecmp(cp, "base", 4))
219 change_base(BASE_SEARCH, &search_base, nextstr(cp));
221 else if (iscom("memberships"))
222 (void) list_memberships(nextstr(cp));
223 else if (iscom("list"))
224 (void) list_groups(nextstr(cp));
225 else if (iscom("groupbase"))
226 change_base(BASE_GROUPS, &group_base, nextstr(cp));
227 else if (iscom("find") || iscom("display") || iscom("show")) {
229 if ((mp = find(cp, FALSE)) != NULL) {
235 printf(" Could not find \"%s\".\n", cp);
238 else if (iscom("vedit") && isatty( 1 )) {
240 else if (iscom("vedit")) {
242 (void) edit(nextstr(cp));
244 else if (iscom("modify") || iscom("change") || iscom("alter"))
245 (void) modify(nextstr(cp));
246 else if (iscom("bind") || iscom("iam"))
247 (void) auth(nextstr(cp), 0);
248 else if ((cmd[0] == '?') || iscom("help"))
249 print_help(nextstr(cp));
250 else if (iscom("join") || iscom("subscribe"))
251 (void) x_group(G_JOIN, nextstr(cp));
252 else if (iscom("resign") || iscom("unsubscribe"))
253 (void) x_group(G_RESIGN, nextstr(cp));
254 else if (!strncasecmp("create", cmd, strlen(cmd)))
255 add_group(nextstr(cp));
256 else if (!strncasecmp("remove", cmd, strlen(cmd)))
257 remove_group(nextstr(cp));
258 else if (!strncasecmp("purge", cmd, strlen(cmd)))
259 purge_group(nextstr(cp));
260 else if (!strncasecmp("verbose", cmd, strlen(cmd))) {
261 verbose = 1 - verbose;
263 printf(" Verbose mode has been turned on.\n");
265 else if (!strncasecmp("dereference", cmd, strlen(cmd))) {
266 dereference = 1 - dereference;
267 if (dereference == 1)
268 ld->ld_deref = LDAP_DEREF_ALWAYS;
270 ld->ld_deref = LDAP_DEREF_NEVER;
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");
292 register char **rdns;
296 printf("->status()\n");
298 printf(" Current server is %s", server);
299 if ( ld != NULL && ld->ld_host != NULL && strcasecmp( ld->ld_host,
301 printf( " (%s)", ld->ld_host );
303 printbase(" Search base is ", search_base);
304 printbase(" Group base is ", group_base);
305 if ( bound_dn != NULL ) {
306 rdns = ldap_explode_dn(bound_dn, TRUE);
307 printf(" Bound as \"%s\"\n", *rdns);
308 ldap_value_free(rdns);
310 printf(" Bound as Nobody\n" );
312 printf( " Verbose mode is %sabled\n", ( verbose ? "en" : "dis" ));
314 printf( " Aliases are %sbeing dereferenced\n", ( ld->ld_deref == LDAP_DEREF_ALWAYS ) ? "" : "not" );
318 change_base(type, base, s)
322 register char *cp; /* utility pointers */
323 char **rdns; /* for parsing */
324 char *output_string; /* for nice output */
325 int num_picked; /* # of selected base */
326 int j; /* used with num_picked */
327 int i = 1; /* index into choices array */
328 int matches; /* # of matches found */
329 int rest = 1; /* # left to display */
330 char tmp[MED_BUF_SIZE]; /* temporary buffer */
331 static char *choices[MED_BUF_SIZE]; /* bases from which to choose */
332 static char resp[SMALL_BUF_SIZE]; /* for prompting user */
333 static char buf[MED_BUF_SIZE];
335 static char *attrs[] = { "objectClass", NULL };
336 LDAPMessage *mp; /* results from a search */
337 LDAPMessage *ep; /* for going thru bases */
338 extern char * friendly_name();
339 extern void StrFreeDup();
344 printf("->change_base(%s, %s)\n", s, s);
347 * If s is NULL we need to prompt the user for an argument.
351 printf(" You need to specify how the base is to be changed. Valid choices are:\n");
352 printf(" ? - list the choices immediately below this level\n");
353 printf(" .. - move up one level in the Directory tree\n");
354 printf(" root - move to the root of the Directory tree\n");
355 printf(" default - move to the default level built into this program\n");
356 printf(" <entry> - move to the entry specified\n");
358 printf(" Change base to? ");
360 fetch_buffer(buf, sizeof(buf), stdin);
361 if ((buf != NULL) && (buf[0] != '\0'))
365 /* set the output string */
366 if (type == BASE_SEARCH)
367 output_string = " Search base is now ";
368 else if (type == BASE_GROUPS)
369 output_string = " Group base is now ";
371 if (!strcasecmp(s, "root")) {
372 StrFreeDup(base, NULL);
373 printbase(" Search base is ", *base);
378 * User wants to ascend one level in the LDAP tree.
379 * Easy: Just strip off the first element of the
380 * current search base, unless it's the root, in
381 * which case we just do nothing.
383 if (!strcasecmp(s, "..")) {
385 printf(" You are already at the root\n");
388 cp = strchr(*base, '=');
391 * If there isn't a second "=" in the base, then this was
392 * a one element base, and so now it should be NULL.
394 if ((cp = strchr(cp, '=')) == NULL)
395 StrFreeDup(base, NULL);
398 * Back up to the start of this
402 * sequence now that 'cp' is pointing to the '='.
408 * Goofy, but need to do it this way since both *base
409 * and cp point into the same chunk of memory, and
410 * we want to free *base, but keep part of it around.
413 StrFreeDup(base, cp);
416 printbase(output_string, *base);
420 /* user wants to see what is directly below this level */
423 * Fetch the list of entries directly below this level.
424 * Once we have the list, we will print it for the user, one
425 * screenful at a time. At the end of each screen, we ask
426 * the user if they want to see more. They can also just
427 * type a number at that point too.
429 if (ldap_search_s(ld, *base, LDAP_SCOPE_ONELEVEL, "(|(objectClass=quipuNonLeafObject)(objectClass=externalNonLeafObject))", attrs, FALSE, &mp) != LDAP_SUCCESS) {
430 if ((ld->ld_errno == LDAP_TIMELIMIT_EXCEEDED) ||
431 (ld->ld_errno == LDAP_SIZELIMIT_EXCEEDED)) {
433 printf(" Your query was too general and a limit was exceeded. The results listed\n");
434 printf(" are not complete. You may want to try again with a more refined query.\n\n");
437 printf(" Time or size limit exceeded. Partial results follow.\n\n");
439 ldap_perror(ld, "ldap_search_s");
443 if ((matches = ldap_count_entries(ld, mp)) < 1) {
444 printf(" There is nothing below this level.\n");
445 (void) ldap_msgfree(mp);
449 printf(" There are %d choices:\n", matches);
450 for (ep = ldap_first_entry(ld, mp); ep != NULL; ep = ldap_next_entry(ld, ep)) {
452 * Put the last component of the DN into 'lastDN'.
453 * If we are at the root level, convert any country
454 * codes to recognizable names for printing.
456 choices[i] = ldap_get_dn(ld, ep);
457 rdns = ldap_explode_dn(choices[i], TRUE);
458 printf(" %2d. %s\n", i, friendly_name(*rdns));
459 (void) ldap_value_free(rdns);
461 if ((rest++ > (lpp - 3)) && (i < matches)) {
464 fetch_buffer(resp, sizeof(resp), stdin);
465 if ((resp[0] == 'n') || (resp[0] == 'N'))
467 else if (((num_picked = atoi(resp)) != 0) && (num_picked < i))
473 if (num_picked != 0) {
478 printf(" Which number? ");
480 fetch_buffer(resp, sizeof(resp), stdin);
484 (void) ldap_msgfree(mp);
485 for (i = 0; i < matches; i++)
489 if ((j < 1) || (j >= i))
490 printf(" Invalid number\n");
492 StrFreeDup(base, choices[j]);
493 printbase(output_string, *base);
494 (void) ldap_msgfree(mp);
495 for (i = 0; choices[i] != NULL; i++)
501 /* set the search base back to the original default value */
502 else if (!strcasecmp(s, "default")) {
503 if (type == BASE_SEARCH)
504 StrFreeDup(base, UD_BASE);
505 else if (type == BASE_GROUPS)
506 StrFreeDup(base, UD_WHERE_GROUPS_ARE_CREATED);
507 printbase(output_string, *base);
509 /* they typed in something -- see if it is legit */
511 /* user cannot do something like 'cb 33' */
513 printf(" \"%s\" is not a valid search base\n", s);
514 printf(" Base unchanged.\n");
515 printf(" Try using 'cb ?'\n");
518 /* was it a fully-specified DN? */
521 printbase(output_string, *base);
524 /* was it a RDN relative to the current base? */
525 sprintf(tmp, "ou=%s, %s", s, *base);
527 StrFreeDup(base, tmp);
528 printbase(output_string, *base);
531 printf(" \"%s\" is not a valid base\n Base unchanged.\n", s);
537 FILE *fp; /* for config file */
538 static char buffer[MED_BUF_SIZE]; /* for input */
539 struct passwd *pw; /* for getting the home dir */
540 register char *cp; /* for fiddling with buffer */
541 char *term; /* for tty set-up */
542 char *config; /* config file to use */
543 static char bp[1024]; /* for tty set-up */
544 extern SIG_FN attn(); /* ^C signal handler */
545 extern char *getenv();
550 printf("->initialize_client()\n");
553 * A per-user config file has precedence over any system-wide
554 * config file, if one exists.
556 if ((pw = getpwuid((uid_t) geteuid())) == (struct passwd *) NULL)
557 config = config_file;
559 if (pw->pw_dir == NULL)
560 config = config_file;
562 sprintf(buffer, "%s/%s", pw->pw_dir,
563 UD_USER_CONFIG_FILE);
564 if (access(buffer, R_OK) == 0)
567 config = config_file;
571 if (debug & D_INITIALIZE)
572 printf("Using config file %s\n", config);
576 * If there is a config file, read it.
578 * Could have lines that look like this:
580 * server <ip-address or domain-name>
581 * base <default search base>
582 * groupbase <default place where groups are created>
585 if ((fp = fopen(config, "r")) != NULL) {
586 while (fgets(buffer, sizeof(buffer), fp) != NULL) {
587 buffer[strlen(buffer) - 1] = '\0';
588 if (!strncasecmp(buffer, "server", 6)) {
594 if ((*cp == '\0') || (*cp == '\n'))
598 else if (!strncasecmp(buffer, "base", 4)) {
602 if ((*cp == '\0') || (*cp == '\n'))
604 search_base = strdup(cp);
606 else if (!strncasecmp(buffer, "groupbase", 9)) {
610 if ((*cp == '\0') || (*cp == '\n'))
612 group_base = strdup(cp);
615 fprintf(stderr, "?? -> %s\n", buffer);
618 if (group_base == NULL)
619 group_base = strdup(UD_WHERE_GROUPS_ARE_CREATED);
620 if (search_base == NULL)
621 search_base = strdup(UD_BASE);
623 server = strdup(LDAPHOST);
626 * Set up our LDAP connection. The values of retry and timeout
627 * are meaningless since we will immediately be doing a null bind
628 * because we want to be sure to use TCP, not UDP.
630 if ((ld = ldap_open(server, ldap_port)) == NULL) {
631 fprintf(stderr, " The LDAP Directory is temporarily unavailable. Please try again later.\n");
635 if (ldap_bind_s(ld, (char *) default_bind_object, (char *) UD_BIND_CRED,
636 LDAP_AUTH_SIMPLE) != LDAP_SUCCESS) {
637 fprintf(stderr, " The LDAP Directory is temporarily unavailable. Please try again later.\n");
638 if (ld->ld_errno != LDAP_UNAVAILABLE)
639 ldap_perror(ld, " ldap_bind_s");
643 ld->ld_deref = LDAP_DEREF_ALWAYS;
644 bind_status = UD_NOT_BOUND;
645 if ( default_bind_object != NULL ) {
646 bound_dn = strdup(default_bind_object);
651 /* enabled local caching of ldap results, 15 minute lifetime */
653 ldap_enable_cache( ld, 60 * 15, 100 * 1024 ); /* 100k max memory */
655 ldap_enable_cache( ld, 60 * 15, 0 ); /* no memory limit */
658 /* initialize the search filters */
659 if ((lfdp = ldap_init_getfilter(filter_file)) == NULL) {
660 fprintf(stderr, " Problem with ldap_init_getfilter\n");
665 /* terminal initialization stuff goes here */
666 lpp = DEFAULT_TTY_HEIGHT;
667 col_size = DEFAULT_TTY_WIDTH;
669 (void) signal(SIGINT, attn);
671 #if !defined(DOS) && !defined(NOTERMCAP)
673 struct winsize win; /* for tty set-up */
674 extern SIG_FN chwinsz(); /* WINSZ signal handler */
676 if (((term = getenv("TERM")) == NULL) || (tgetent(bp, term) <= 0))
679 if (ioctl(fileno(stdout), TIOCGWINSZ, &win) < 0) {
681 col_size = tgetnum("co");
684 if ((lpp = win.ws_row) == 0)
686 if ((col_size = win.ws_col) == 0)
687 col_size = tgetnum("co");
688 if ((lpp <= 0) || tgetflag("hc"))
689 lpp = DEFAULT_TTY_HEIGHT;
690 if ((col_size <= 0) || tgetflag("hc"))
691 col_size = DEFAULT_TTY_WIDTH;
694 (void) signal(SIGWINCH, chwinsz);
704 printf("\n\n INTERRUPTED!\n");
705 #if defined(DOS) || defined(SYSV)
706 (void) signal(SIGINT, attn);
711 #if !defined(DOS) && !defined(NOTERMCAP)
716 (void) signal(SIGWINCH, SIG_IGN);
717 if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) {
721 col_size = win.ws_col;
723 (void) signal(SIGWINCH, chwinsz);
727 #if defined(NO_CACHE)
729 void ldap_uncache_entry( LDAP *ld, char *dn )
734 int ldap_enable_cache( LDAP *ld, long timeout, long maxmem )
739 void ldap_flush_cache( LDAP *ld )
744 #endif /* NO_CACHE */