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.
21 #include <ac/signal.h>
22 #include <ac/string.h>
23 #include <ac/termios.h>
25 #include <ac/unistd.h>
29 #include <ldapconfig.h>
34 mygetpass( char *prompt )
36 #if !defined(HAVE_TERMIOS) && !defined(HAVE_SGTTY_H)
42 printf("->mygetpass(%s)\n", prompt);
46 while ( (c = getch()) != EOF && c != '\n' && c != '\r' )
57 static char pbuf[513];
61 RETSIGTYPE (*sig)( int sig );
65 printf("->mygetpass(%s)\n", prompt);
68 * Stolen from the getpass() routine. Can't use the plain
69 * getpass() for two reasons. One is that LDAP passwords
70 * can be really, really long - much longer than 8 chars.
71 * The second is that we like to make this client available
72 * out of inetd via a Merit asynch port, and we need to be
73 * able to do telnet control codes to turn on and off line
76 if ((fi = fdopen(open("/dev/tty", 2), "r")) == NULL)
79 setbuf(fi, (char *)NULL);
80 sig = SIGNAL (SIGINT, SIG_IGN);
82 if (GETATTR(fileno(fi), &ttyb) < 0)
85 flags = GETFLAGS( ttyb );
86 SETFLAGS( ttyb, flags & ~ECHO );
88 if (SETATTR(fileno(fi), &ttyb) < 0)
92 /* blank the line if through Merit */
94 printf("%c%c%c", 255, 251, 1);
96 (void) scanf("%c%c%c", &i, &j, &k);
100 /* fetch the password */
101 fprintf(stdout, "%s", prompt);
103 for (p=pbuf; (c = getc(fi))!='\n' && c!=EOF;) {
113 if (*(p - 1) == '\r')
117 /* unblank the line if through Merit */
119 printf("%c%c%c", 255, 252, 1);
121 (void) scanf("%c%c%c", &i, &j, &k);
123 printf("\n"); fflush(stdout);
125 fprintf(stdout, "\n");
129 SETFLAGS( ttyb, flags );
131 if (SETATTR(fileno(fi), &ttyb) < 0)
134 (void) SIGNAL (SIGINT, sig);
146 printbase( char *lead, char *s )
153 printf("->printbase(%s, %s)\n", lead, s);
156 printf("%sroot\n", lead);
160 rdns = ldap_explode_dn(s, TRUE);
161 for (cp = rdns; ; ) {
162 printf("%s", friendly_name(*cp));
171 ldap_value_free(rdns);
176 fetch_buffer( char *buffer, int length, FILE *where )
183 printf("->fetch_buffer(%x, %d, %x)\n", buffer, length, where);
186 * Fetch a buffer and strip off any leading or trailing non-printing
187 * characters, namely newlines and carriage returns.
189 if (fgets(buffer, length, where) == NULL) {
191 errno = 0; /* so fatal() doesn't bitch */
194 for (i = strlen(buffer) - 1; i >= 0 && !isprint(buffer[i]); i--)
198 while ( *p != '\0' ) {
199 if ( isprint( *p )) {
228 printf("->isgroup()\n");
230 if ((i = attr_to_index("objectClass")) == -1)
232 vp = Entry.attrs[i].values;
233 for (i = 0; *vp != NULL; vp++) {
236 if (debug & D_GROUPS)
237 printf("class #%1d: (%s)\n", i, *vp);
239 if (!strcmp(*vp, "rfc822MailGroup"))
246 * Print out the string 's' on a field of 'width' chracters. Each line
247 * should be indented 'lead' characters.
250 format( char *str, int width, int lead )
252 char *s, *original, *leader = "";
257 printf("->format(%s, %d, %d)\n", str, width, lead);
260 fprintf(stderr, " Cannot format (%s, %d, %d)\n", str, width, lead);
264 leader = (char *) Malloc((unsigned) (lead + 1));
265 (void) memset(leader, ' ', lead);
266 *(leader + lead) = '\0';
270 * Some compilers get really unhappy with this function since it
271 * fiddles around with the first argument, which could be a string
272 * constant. We do a strdup() here so we can do whatever the hell
275 s = original = strdup(str);
277 if ((strlen(s) + lead) < width) {
278 printf("%s%s\n", leader, s);
284 cp = s + width - lead;
285 while (!isspace(*cp) && (cp != s))
290 printf("%s%s\n", leader, s);
296 * Print out the string 's' on a field of 'width' chracters. The first line
297 * should be indented 'first_indent' spaces, then followed by 'first_tag',
298 * and then followed by the first line of 's'. Subsequent lines should be
299 * indented 'indent' spaces, then followed by 'tag', and then followed by
300 * subsequent lines of 's'.
315 if (first_tag == NULL)
321 printf("format2(\"%s\", \"%s\", \"%s\", %1d, %1d, %1d)\n", s,
322 first_tag, tag, first_indent, indent, width);
325 /* make sure the indents are sane */
326 if ((first_indent >= width) || (indent >= width)) {
327 fprintf(stderr, " Cannot format: indent too large\n");
331 /* make the indentations */
332 if (first_indent > 0) {
333 fi = (char *) Malloc((unsigned) (first_indent + 1));
334 (void) memset(fi, ' ', first_indent);
335 *(fi + first_indent) = '\0';
340 i = (char *) Malloc((unsigned) (indent + 1));
341 (void) memset(i, ' ', indent);
342 *(i + indent) = '\0';
347 /* now do the first line */
348 if ((strlen(s) + strlen(first_tag) + first_indent) < width) {
349 printf("%s%s%s\n", fi, first_tag, s);
357 * 's' points to the beginning of the string we want to print.
358 * We point 'cp' to the end of the maximum amount of text we
359 * can print (i.e., total width less the indentation and the
360 * length of the tag). Once we have set 'cp' initially we
361 * back it up to the first space character.
363 cp = s + width - first_indent - strlen(first_tag);
364 while (!isspace(*cp) && (cp != s))
368 * Once there, we change that space character to a null, print the
369 * string, and then restore the space character.
373 printf("%s%s%s\n", fi, first_tag, s);
377 * Since 'cp' may have been set to a space initially (and so no
378 * back-tracking was performed), it could have a space after it
379 * as well. We should gobble up all of these since we don't want
380 * unexpected leading blanks.
382 for (s = cp + 1; isspace(*s); s++)
385 /* now do all of the other lines */
387 if ((strlen(s) + strlen(tag) + indent) < width) {
388 printf("%s%s%s\n", i, tag, s);
394 cp = s + width - indent - strlen(tag);
395 while (!isspace(*cp) && (cp != s))
399 printf("%s%s%s\n", i, tag, s);
401 *cp = c; /* don't mess up 's' */
406 #define OUT_OF_QUOTE 1
409 strip_ignore_chars( char *cp )
411 int had_a_comma = FALSE;
412 int flag = OUT_OF_QUOTE;
413 register char *rcp, *cp1;
418 printf("strip_ignore_chars(%s)\n", cp);
420 for (rcp = cp; *rcp != '\0'; rcp++)
421 if (isignorechar(*rcp) || (*rcp == '"'))
426 cp1 = tmp = (char *) Malloc((unsigned) strlen(cp));
427 for (rcp = cp; *rcp != '\0'; rcp++) {
428 /* toss quotes and flip the flag */
430 flag = OUT_OF_QUOTE - flag;
431 else if (isignorechar(*rcp)) {
432 if (flag == OUT_OF_QUOTE)
437 else if (*rcp == ',') {
446 /* re-quote the name if it had a comma in it */
447 if (had_a_comma == TRUE) {
448 rcp = cp1 = (char *) Malloc((unsigned) (strlen(tmp) + 3));
463 case LDAP_MOD_ADD : return("ADD");
464 case LDAP_MOD_DELETE : return("DELETE");
465 case LDAP_MOD_REPLACE : return("REPLACE");
466 default : return("?????");
471 friendly_name( char *s )
473 static FriendlyMap *map = NULL;
476 cp = ldap_friendly_name(FRIENDLYFILE, s, &map);
484 /* return TRUE if s has the syntax of a uniqname */
486 isauniqname( char *s )
490 if ((i < 3) || (i > 8)) /* uniqnames are 3-8 chars */
492 if (!isalpha(*s)) /* uniqnames begin with a letter */
494 for ( ; *s != '\0'; s++) /* uniqnames are alphanumeric */
501 /* return TRUE if this attribute should be printed as a DN */
507 for (i = 0; attrlist[i].quipu_name != NULL; i++)
508 if (!strcasecmp(s, attrlist[i].quipu_name))
510 if (attrlist[i].flags & ATTR_FLAG_IS_A_DN)
516 my_ldap_dn2ufn( char *s )
519 static char short_DN[BUFSIZ];
521 if (strstr(s, NULL) == NULL)
522 return(ldap_dn2ufn(s));
523 cpp = ldap_explode_dn(s, TRUE);
524 sprintf(short_DN, "%s, %s", *cpp, *(cpp + 1));
525 ldap_value_free(cpp);
529 /* return TRUE if this attribute should be printed as a URL */
535 for (i = 0; attrlist[i].quipu_name != NULL; i++)
536 if (!strcasecmp(s, attrlist[i].quipu_name))
538 if (attrlist[i].flags & ATTR_FLAG_IS_A_URL)
543 /* return TRUE if this attribute should be printed as a date and time */
549 for (i = 0; attrlist[i].quipu_name != NULL; i++)
550 if (!strcasecmp(s, attrlist[i].quipu_name))
552 if (attrlist[i].flags & ATTR_FLAG_IS_A_DATE)
558 Malloc( unsigned int size )
562 void_ptr = (void *) malloc(size);
563 if (void_ptr == NULL) {
589 while (isspace(*s) && (*s != '\0'))
599 free_mod_struct( LDAPMod *modp )
601 if (modp->mod_values != NULL)
602 (void) ldap_value_free(modp->mod_values);
603 Free(modp->mod_type);
608 StrFreeDup( char **ptr, char *new_value )
612 if (new_value == NULL)
615 *ptr = strdup(new_value);
620 confirm_action( char *msg )
622 char tmp[SMALL_BUF_SIZE];
627 format( msg, 75, 2 );
630 printf("\n Is this OK? ");
633 fetch_buffer(tmp, sizeof(tmp), stdin);
636 ( !strncasecmp(tmp, "YES", i) || !strncasecmp(tmp, "OK", i)));