2 * Copyright (c) 1992, 1993 Regents of the University of Michigan.
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.
20 #include <ac/signal.h>
21 #include <ac/string.h>
22 #include <ac/termios.h>
24 #include <ac/unistd.h>
36 #include <ldapconfig.h>
41 mygetpass( char *prompt )
43 #if !defined(HAVE_TERMIOS) && !defined(HAVE_SGTTY_H)
49 printf("->mygetpass(%s)\n", prompt);
53 while ( (c = getch()) != EOF && c != '\n' && c != '\r' )
64 static char pbuf[513];
68 RETSIGTYPE (*sig)( int sig );
72 printf("->mygetpass(%s)\n", prompt);
75 * Stolen from the getpass() routine. Can't use the plain
76 * getpass() for two reasons. One is that LDAP passwords
77 * can be really, really long - much longer than 8 chars.
78 * The second is that we like to make this client available
79 * out of inetd via a Merit asynch port, and we need to be
80 * able to do telnet control codes to turn on and off line
83 if ((fi = fdopen(open("/dev/tty", 2), "r")) == NULL)
86 setbuf(fi, (char *)NULL);
87 sig = SIGNAL (SIGINT, SIG_IGN);
89 if (GETATTR(fileno(fi), &ttyb) < 0)
92 flags = GETFLAGS( ttyb );
93 SETFLAGS( ttyb, flags & ~ECHO );
95 if (SETATTR(fileno(fi), &ttyb) < 0)
99 /* blank the line if through Merit */
101 printf("%c%c%c", 255, 251, 1);
103 (void) scanf("%c%c%c", &i, &j, &k);
107 /* fetch the password */
108 fprintf(stdout, "%s", prompt);
110 for (p=pbuf; (c = getc(fi))!='\n' && c!=EOF;) {
120 if (*(p - 1) == '\r')
124 /* unblank the line if through Merit */
126 printf("%c%c%c", 255, 252, 1);
128 (void) scanf("%c%c%c", &i, &j, &k);
130 printf("\n"); fflush(stdout);
132 fprintf(stdout, "\n");
136 SETFLAGS( ttyb, flags );
138 if (SETATTR(fileno(fi), &ttyb) < 0)
141 (void) SIGNAL (SIGINT, sig);
153 printbase( char *lead, char *s )
160 printf("->printbase(%s, %s)\n", lead, s);
163 printf("%sroot\n", lead);
167 rdns = ldap_explode_dn(s, TRUE);
168 for (cp = rdns; ; ) {
169 printf("%s", friendly_name(*cp));
178 ldap_value_free(rdns);
183 fetch_buffer( char *buffer, int length, FILE *where )
190 printf("->fetch_buffer(%x, %d, %x)\n", buffer, length, where);
193 * Fetch a buffer and strip off any leading or trailing non-printing
194 * characters, namely newlines and carriage returns.
196 if (fgets(buffer, length, where) == NULL) {
198 errno = 0; /* so fatal() doesn't bitch */
201 for (i = strlen(buffer) - 1; i >= 0 && !isprint(buffer[i]); i--)
205 while ( *p != '\0' ) {
206 if ( isprint( *p )) {
235 printf("->isgroup()\n");
237 if ((i = attr_to_index("objectClass")) == -1)
239 vp = Entry.attrs[i].values;
240 for (i = 0; *vp != NULL; vp++) {
243 if (debug & D_GROUPS)
244 printf("class #%1d: (%s)\n", i, *vp);
246 if (!strcmp(*vp, "rfc822MailGroup"))
253 * Print out the string 's' on a field of 'width' chracters. Each line
254 * should be indented 'lead' characters.
257 format( char *str, int width, int lead )
259 char *s, *original, *leader = "";
264 printf("->format(%s, %d, %d)\n", str, width, lead);
267 fprintf(stderr, " Cannot format (%s, %d, %d)\n", str, width, lead);
271 leader = (char *) Malloc((unsigned) (lead + 1));
272 (void) memset(leader, ' ', lead);
273 *(leader + lead) = '\0';
277 * Some compilers get really unhappy with this function since it
278 * fiddles around with the first argument, which could be a string
279 * constant. We do a strdup() here so we can do whatever the hell
282 s = original = strdup(str);
284 if ((strlen(s) + lead) < width) {
285 printf("%s%s\n", leader, s);
291 cp = s + width - lead;
292 while (!isspace(*cp) && (cp != s))
297 printf("%s%s\n", leader, s);
303 * Print out the string 's' on a field of 'width' chracters. The first line
304 * should be indented 'first_indent' spaces, then followed by 'first_tag',
305 * and then followed by the first line of 's'. Subsequent lines should be
306 * indented 'indent' spaces, then followed by 'tag', and then followed by
307 * subsequent lines of 's'.
322 if (first_tag == NULL)
328 printf("format2(\"%s\", \"%s\", \"%s\", %1d, %1d, %1d)\n", s,
329 first_tag, tag, first_indent, indent, width);
332 /* make sure the indents are sane */
333 if ((first_indent >= width) || (indent >= width)) {
334 fprintf(stderr, " Cannot format: indent too large\n");
338 /* make the indentations */
339 if (first_indent > 0) {
340 fi = (char *) Malloc((unsigned) (first_indent + 1));
341 (void) memset(fi, ' ', first_indent);
342 *(fi + first_indent) = '\0';
347 i = (char *) Malloc((unsigned) (indent + 1));
348 (void) memset(i, ' ', indent);
349 *(i + indent) = '\0';
354 /* now do the first line */
355 if ((strlen(s) + strlen(first_tag) + first_indent) < width) {
356 printf("%s%s%s\n", fi, first_tag, s);
364 * 's' points to the beginning of the string we want to print.
365 * We point 'cp' to the end of the maximum amount of text we
366 * can print (i.e., total width less the indentation and the
367 * length of the tag). Once we have set 'cp' initially we
368 * back it up to the first space character.
370 cp = s + width - first_indent - strlen(first_tag);
371 while (!isspace(*cp) && (cp != s))
375 * Once there, we change that space character to a null, print the
376 * string, and then restore the space character.
380 printf("%s%s%s\n", fi, first_tag, s);
384 * Since 'cp' may have been set to a space initially (and so no
385 * back-tracking was performed), it could have a space after it
386 * as well. We should gobble up all of these since we don't want
387 * unexpected leading blanks.
389 for (s = cp + 1; isspace(*s); s++)
392 /* now do all of the other lines */
394 if ((strlen(s) + strlen(tag) + indent) < width) {
395 printf("%s%s%s\n", i, tag, s);
401 cp = s + width - indent - strlen(tag);
402 while (!isspace(*cp) && (cp != s))
406 printf("%s%s%s\n", i, tag, s);
408 *cp = c; /* don't mess up 's' */
413 #define OUT_OF_QUOTE 1
416 strip_ignore_chars( char *cp )
418 int had_a_comma = FALSE;
419 int flag = OUT_OF_QUOTE;
420 register char *rcp, *cp1;
425 printf("strip_ignore_chars(%s)\n", cp);
427 for (rcp = cp; *rcp != '\0'; rcp++)
428 if (isignorechar(*rcp) || (*rcp == '"'))
433 cp1 = tmp = (char *) Malloc((unsigned) strlen(cp));
434 for (rcp = cp; *rcp != '\0'; rcp++) {
435 /* toss quotes and flip the flag */
437 flag = OUT_OF_QUOTE - flag;
438 else if (isignorechar(*rcp)) {
439 if (flag == OUT_OF_QUOTE)
444 else if (*rcp == ',') {
453 /* re-quote the name if it had a comma in it */
454 if (had_a_comma == TRUE) {
455 rcp = cp1 = (char *) Malloc((unsigned) (strlen(tmp) + 3));
470 case LDAP_MOD_ADD : return("ADD");
471 case LDAP_MOD_DELETE : return("DELETE");
472 case LDAP_MOD_REPLACE : return("REPLACE");
473 default : return("?????");
478 friendly_name( char *s )
480 static FriendlyMap *map = NULL;
483 cp = ldap_friendly_name(FRIENDLYFILE, s, &map);
491 /* return TRUE if s has the syntax of a uniqname */
493 isauniqname( char *s )
497 if ((i < 3) || (i > 8)) /* uniqnames are 3-8 chars */
499 if (!isalpha(*s)) /* uniqnames begin with a letter */
501 for ( ; *s != '\0'; s++) /* uniqnames are alphanumeric */
508 /* return TRUE if this attribute should be printed as a DN */
514 for (i = 0; attrlist[i].quipu_name != NULL; i++)
515 if (!strcasecmp(s, attrlist[i].quipu_name))
517 if (attrlist[i].flags & ATTR_FLAG_IS_A_DN)
523 my_ldap_dn2ufn( char *s )
526 static char short_DN[BUFSIZ];
528 if (strstr(s, NULL) == NULL)
529 return(ldap_dn2ufn(s));
530 cpp = ldap_explode_dn(s, TRUE);
531 sprintf(short_DN, "%s, %s", *cpp, *(cpp + 1));
532 ldap_value_free(cpp);
536 /* return TRUE if this attribute should be printed as a URL */
542 for (i = 0; attrlist[i].quipu_name != NULL; i++)
543 if (!strcasecmp(s, attrlist[i].quipu_name))
545 if (attrlist[i].flags & ATTR_FLAG_IS_A_URL)
550 /* return TRUE if this attribute should be printed as a date and time */
556 for (i = 0; attrlist[i].quipu_name != NULL; i++)
557 if (!strcasecmp(s, attrlist[i].quipu_name))
559 if (attrlist[i].flags & ATTR_FLAG_IS_A_DATE)
565 Malloc( unsigned int size )
569 void_ptr = (void *) malloc(size);
570 if (void_ptr == NULL) {
596 while (isspace(*s) && (*s != '\0'))
606 free_mod_struct( LDAPMod *modp )
608 if (modp->mod_values != NULL)
609 (void) ldap_value_free(modp->mod_values);
610 Free(modp->mod_type);
615 StrFreeDup( char **ptr, char *new_value )
619 if (new_value == NULL)
622 *ptr = strdup(new_value);
627 confirm_action( char *msg )
629 char tmp[SMALL_BUF_SIZE];
634 format( msg, 75, 2 );
637 printf("\n Is this OK? ");
640 fetch_buffer(tmp, sizeof(tmp), stdin);
643 ( !strncasecmp(tmp, "YES", i) || !strncasecmp(tmp, "OK", i)));