2 * Copyright (c) 1994, Strata Software Limited, Ottawa, Ontario, Canada.
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 Eric Rosenquist and Strata Software Limited. The SSL name
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.
13 * 'saucer' LDAP command-line client source code.
15 * Author: Eric Rosenquist, 1994.
26 #define DN_MAXLEN 4096
44 char *attrs_null[] = { "0.10", NULL };
46 char default_dn[DN_MAXLEN];
47 char *hostname = "127.0.0.1";
53 int portnum = LDAP_PORT;
55 char true_filter[] = "objectClass=*"; /* Always succeeds */
58 int cmd_help(char **cmdargv, int cmdargc);
59 int cmd_list(char **cmdargv, int cmdargc);
60 int cmd_moveto(char **cmdargv, int cmdargc);
61 int cmd_quit(char **cmdargv, int cmdargc);
62 int cmd_search(char **cmdargv, int cmdargc);
63 int cmd_set(char **cmdargv, int cmdargc);
64 int cmd_show(char **cmdargv, int cmdargc);
65 char *make_dn(char *dn, int relative);
66 char *skip_to_char(register char *s, register int c);
67 char *skip_to_whitespace(register char *s);
68 char *skip_whitespace(register char *s);
69 FILE *user_tailor(void);
71 static char *binary_attrs[] = { "audio", "jpegPhoto", "personalSignature", "photo" };
73 CMDTABLE cmdtable[] = {
74 "help" , cmd_help , "[command]",
75 "list" , cmd_list , "[RDN-or-DN] [-absolute]",
76 "moveto", cmd_moveto, "[RDN-or-DN] [-absolute]",
77 "quit" , cmd_quit , "",
78 "search", cmd_search, "<filter> [-object RDN-or-DN] [-absolute]\n\t\t[-scope base|onelevel|subtree]",
79 "set" , cmd_set , "[-aliasderef never|search|find|always] [-sizelimit N] [-timelimit seconds]",
80 "show" , cmd_show , "[RDN-or-DN] [-absolute]"
86 if (ldap_simple_bind_s(ld, username, credentials) != LDAP_SUCCESS) {
87 ldap_perror(ld, progname);
91 printf("Bound to ldap server as `%s' (%s authentication)\n", username,
92 credentials ? "simple" : "no");
94 puts("Bound anonymously to ldap server");
99 int cmd_help(char **cmdargv, int cmdargc)
104 for (i = 0; i < sizeof(cmdtable) / sizeof(cmdtable[0]); i++)
105 if (strncasecmp(cmdargv[1], cmdtable[i].cmd, strlen(cmdargv[1])) == 0) {
109 cmdargc = 1; /* Command not found - make it display the list of commands */
113 puts("\nType 'help <command>' for help on a particular command.\n\n"
114 "Supported commands are:");
115 for (i = 0; i < sizeof(cmdtable) / sizeof(cmdtable[0]); i++)
116 printf(" %s\n", cmdtable[i].cmd);
117 puts("\nArguments to commands are separated by whitespace. Single (')\n"
118 "or double (\") quotes must be used around arguments that contain\n"
119 "embedded whitespace characters.\n");
121 show_syntax(CMD_HELP);
126 int cmd_list(char **cmdargv, int cmdargc)
131 static char *opts[] = { "absolute" };
135 for (i = 1; i < cmdargc; i++) {
136 if (cmdargv[i][0] == '-') {
137 switch (table_lookup(cmdargv[i] + 1, opts, sizeof(opts) / sizeof(opts[0]))) {
153 show_syntax(CMD_LIST);
157 if (ldap_search(ld, make_dn(dn, relative), LDAP_SCOPE_ONELEVEL,
158 true_filter, attrs_null, 1) == -1) {
159 ldap_perror(ld, progname);
163 if (ldap_result(ld, LDAP_RES_ANY, 1, (struct timeval *)0, &result) == -1) {
164 ldap_perror(ld, progname);
168 display_search_results(result);
173 int cmd_moveto(char **cmdargv, int cmdargc)
179 static char *opts[] = { "absolute" };
182 for (i = 1; i < cmdargc; i++) {
183 if (cmdargv[i][0] == '-') {
184 switch (table_lookup(cmdargv[i] + 1, opts, sizeof(opts) / sizeof(opts[0]))) {
200 show_syntax(CMD_MOVETO);
205 if (is_whitespace(dn))
208 if (strcmp(dn, "..") == 0) {
209 /* Move up one level */
210 if (exploded_dn = ldap_explode_dn(default_dn, 0)) {
211 if (exploded_dn[0]) {
215 for (rdn = exploded_dn + 1; *rdn; rdn++) {
217 strcat(default_dn, ", ");
218 strcat(default_dn, *rdn);
221 ldap_value_free(exploded_dn);
224 /* Use ldap_explode_dn() to parse the string & test its syntax */
225 if (exploded_dn = ldap_explode_dn(dn, 1)) {
226 if (relative && !is_whitespace(default_dn)) {
229 strcpy(default_dn, strcat(strcat(strcpy(buf, dn), ", "), default_dn));
231 strcpy(default_dn, dn);
232 ldap_value_free(exploded_dn);
234 puts("Invalid distinguished name.");
239 printf("Distinguished name suffix is `%s'\n", default_dn);
244 int cmd_quit(char **cmdargv, int cmdargc)
249 int cmd_search(char **cmdargv, int cmdargc)
255 static char *opts[] = { "absolute", "object", "scope" };
258 static char *scope_opts[] = { "base", "onelevel", "subtree" };
259 static int scope_vals[] = { LDAP_SCOPE_BASE, LDAP_SCOPE_ONELEVEL, LDAP_SCOPE_SUBTREE };
260 static int search_scope = LDAP_SCOPE_ONELEVEL;
262 for (i = 1; i < cmdargc; i++) {
263 if (cmdargv[i][0] == '-') {
264 switch (table_lookup(cmdargv[i] + 1, opts, sizeof(opts) / sizeof(opts[0]))) {
275 if ((++i < cmdargc) &&
276 (j = table_lookup(cmdargv[i], scope_opts, sizeof(scope_opts) / sizeof(scope_opts[0]))) >= 0)
277 search_scope = scope_vals[j];
292 if (errflag || !filter) {
293 show_syntax(CMD_SEARCH);
297 if (ldap_search(ld, make_dn(dn, relative), search_scope, filter, attrs_null, 0) == -1) {
298 ldap_perror(ld, progname);
302 if (ldap_result(ld, LDAP_RES_ANY, 1, (struct timeval *)0, &result) == -1) {
303 ldap_perror(ld, progname);
307 display_search_results(result);
312 int cmd_set(char **cmdargv, int cmdargc)
314 static char *alias_opts[] = { "never", "search", "find", "always" };
317 static char *opts[] = { "aliasderef", "sizelimit", "timelimit" };
319 for (i = 1; i < cmdargc; i++) {
320 if (cmdargv[i][0] == '-') {
321 switch (table_lookup(cmdargv[i] + 1, opts, sizeof(opts) / sizeof(opts[0]))) {
323 if ((++i < cmdargc) &&
324 (j = table_lookup(cmdargv[i], alias_opts, sizeof(alias_opts) / sizeof(alias_opts[0]))) >= 0)
325 ldap_set_option(ld, LDAP_OPT_DEREF, &j);
331 j = atoi(cmdargv[i]);
332 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &j);
338 j = atoi(cmdargv[i]);
339 ldap_set_option(ld, LDAP_OPT_TIMELIMIT, &j);
351 show_syntax(CMD_SET);
353 int opt_a, opt_s, opt_t;
354 ldap_get_option(ld, LDAP_OPT_DEREF, &opt_a);
355 ldap_get_option(ld, LDAP_OPT_SIZELIMIT, &opt_s);
356 ldap_get_option(ld, LDAP_OPT_TIMELIMIT, &opt_t);
357 printf("Alias dereferencing is %s, Sizelimit is %d entr%s, Timelimit is %d second%s.\n",
359 opt_s, opt_s == 1 ? "y" : "ies",
360 opt_t, opt_t == 1 ? "" : "s");
366 int cmd_show(char **cmdargv, int cmdargc)
372 static char *opts[] = { "absolute" };
376 for (i = 1; i < cmdargc; i++) {
377 if (cmdargv[i][0] == '-') {
378 switch (table_lookup(cmdargv[i] + 1, opts, sizeof(opts) / sizeof(opts[0]))) {
394 show_syntax(CMD_SHOW);
398 if (ldap_search(ld, make_dn(dn, relative), LDAP_SCOPE_BASE, true_filter, NULL, 0) == -1) {
399 ldap_perror(ld, progname);
403 if (ldap_result(ld, LDAP_RES_ANY, 1, (struct timeval *)0, &result) == -1) {
404 ldap_perror(ld, progname);
408 display_search_results(result);
413 display_search_results(LDAPMessage *result)
421 for (entry = ldap_first_entry(ld, result); entry; entry = ldap_next_entry(ld, entry)) {
422 if (s = ldap_get_dn(ld, entry)) {
427 /* Make one pass to calculate the length of the longest attribute name */
429 for (s = ldap_first_attribute(ld, entry, &cookie); s; s = ldap_next_attribute(ld, entry, cookie))
430 if ((i = strlen(s)) > maxname)
433 /* Now print the attributes and values */
434 for (s = ldap_first_attribute(ld, entry, &cookie); s; s = ldap_next_attribute(ld, entry, cookie)) {
437 if (table_lookup(s, binary_attrs, sizeof(binary_attrs) / sizeof(binary_attrs[0])) >= 0)
438 continue; /* Skip this attribute - it's binary */
440 printf(" %-*s - ", maxname, s);
442 /* Now print each of the values for the given attribute */
443 if (values = ldap_get_values(ld, entry, s)) {
446 for (val = values; *val; ) {
450 /* Watch out for values that have embedded \n characters */
451 while (nl = strchr(v, '\n')) {
456 printf(" %*s", maxname + 3, "");
461 printf(" %*s", maxname + 3, "");
463 ldap_value_free(values);
469 if (ldap_result2error(ld, result, 0))
470 ldap_perror(ld, progname);
473 int do_command(char *cmd)
479 /* Tokenize the input command, allowing for quoting */
481 cmd = skip_whitespace(cmd);
483 break; /* end of input */
485 cmdargv[cmdargc++] = cmd;
486 if (*cmd == '\'' || *cmd == '"') {
487 cmdargv[cmdargc - 1]++; /* Skip over the opening quote */
488 cmd = skip_to_char(cmd + 1, *cmd);
490 puts("Command is missing a trailing quote");
495 cmd = skip_to_whitespace(cmd);
502 printf("cmdargc = %d\n", cmdargc);
503 for (i = 0; i < cmdargc; i++)
507 if (cmdargv[0][0] == '?')
508 return cmd_help(cmdargv, cmdargc);
510 for (i = 0; i < sizeof(cmdtable) / sizeof(cmdtable[0]); i++)
511 if (strncasecmp(cmdargv[0], cmdtable[i].cmd, strlen(cmdargv[0])) == 0)
512 return (*cmdtable[i].func)(cmdargv, cmdargc);
514 if (!is_whitespace(cmdargv[0])) {
515 printf("Unrecognized command - %s\n", cmdargv[0]);
516 cmd_help(cmdargv, 1);
522 void do_commands(FILE *file)
524 char cmd_buf[BUFSIZ];
525 int tty = isatty(fileno(file));
530 if (!fgets(cmd_buf, sizeof(cmd_buf), file))
532 if (do_command(cmd_buf))
537 int is_whitespace(register char *s)
542 while (*s && isspace((unsigned char) *s))
548 int main(int argc, char **argv)
554 while ((option = getopt(argc, argv, "h:p:u:c:d:")) != EOF)
557 credentials = optarg;
561 lber_debug = atoi(optarg);
562 ldap_debug = atoi(optarg);
569 portnum = atoi(optarg);
579 fprintf(stderr, "usage: %s [-h host] [-p portnumber] [-u X500UserName]\n\t[-c credentials] [-d debug-level]\n",
586 if (!(ld = ldap_open(hostname, portnum))) {
587 fprintf(stderr, "%s: unable to connect to server at host `%s' on port %d\n",
588 progname, hostname, portnum);
606 char *make_dn(char *dn, int relative)
608 static char dn_buf[DN_MAXLEN];
614 if (!default_dn[0] || !relative)
620 return strcat(strcat(strcpy(dn_buf, dn), ", "), default_dn);
623 show_syntax(int cmdnum)
625 printf("Syntax: %s %s\n", cmdtable[cmdnum].cmd, cmdtable[cmdnum].help_msg);
628 char *skip_to_char(register char *s, register int c)
633 while (*s && *s != c)
639 char *skip_to_whitespace(register char *s)
644 while (*s && !isspace((unsigned char) *s))
650 char *skip_whitespace(register char *s)
655 while (*s && isspace((unsigned char) *s))
661 int table_lookup(char *word, char **table, int table_count)
669 wordlen = strlen(word);
671 for (i = 0; i < table_count; i++)
672 if (strncasecmp(word, table[i], wordlen) == 0)
677 FILE *user_tailor(void)
686 struct passwd *pwent;
688 if (pwent = getpwuid(getuid()))
689 strcat(strcpy(rcfile, pwent->pw_dir), "/");
690 strcat(rcfile, ".saucerrc");
693 strcpy(rcfile, "saucer.rc");
696 return fopen(rcfile, "r");