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_POSIX_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;
202 i >= 0 && !isprint((unsigned char) buffer[i]); i--)
206 while ( *p != '\0' ) {
207 if ( isprint( (unsigned char) *p )) {
210 SAFEMEMCPY( p, p + 1, strlen( p + 1 ) + 1 );
236 printf("->isgroup()\n");
238 if ((i = attr_to_index("objectClass")) == -1)
240 vp = Entry.attrs[i].values;
241 for (i = 0; *vp != NULL; vp++) {
244 if (debug & D_GROUPS)
245 printf("class #%1d: (%s)\n", i, *vp);
247 if (!strcmp(*vp, "rfc822MailGroup"))
254 * Print out the string 's' on a field of 'width' chracters. Each line
255 * should be indented 'lead' characters.
258 format( char *str, int width, int lead )
260 char *s, *original, *leader = "";
265 printf("->format(%s, %d, %d)\n", str, width, lead);
268 fprintf(stderr, " Cannot format (%s, %d, %d)\n", str, width, lead);
272 leader = (char *) Malloc((unsigned) (lead + 1));
273 (void) memset(leader, ' ', lead);
274 *(leader + lead) = '\0';
278 * Some compilers get really unhappy with this function since it
279 * fiddles around with the first argument, which could be a string
280 * constant. We do a strdup() here so we can do whatever the hell
283 s = original = strdup(str);
285 if (((int) strlen(s) + lead) < width) {
286 printf("%s%s\n", leader, s);
292 cp = s + width - lead;
293 while (!isspace((unsigned char)*cp) && (cp != s))
296 while (isspace((unsigned char)*s))
298 printf("%s%s\n", leader, s);
304 * Print out the string 's' on a field of 'width' chracters. The first line
305 * should be indented 'first_indent' spaces, then followed by 'first_tag',
306 * and then followed by the first line of 's'. Subsequent lines should be
307 * indented 'indent' spaces, then followed by 'tag', and then followed by
308 * subsequent lines of 's'.
323 if (first_tag == NULL)
329 printf("format2(\"%s\", \"%s\", \"%s\", %1d, %1d, %1d)\n", s,
330 first_tag, tag, first_indent, indent, width);
333 /* make sure the indents are sane */
334 if ((first_indent >= width) || (indent >= width)) {
335 fprintf(stderr, " Cannot format: indent too large\n");
339 /* make the indentations */
340 if (first_indent > 0) {
341 fi = (char *) Malloc((unsigned) (first_indent + 1));
342 (void) memset(fi, ' ', first_indent);
343 *(fi + first_indent) = '\0';
348 i = (char *) Malloc((unsigned) (indent + 1));
349 (void) memset(i, ' ', indent);
350 *(i + indent) = '\0';
355 /* now do the first line */
356 if (((int) strlen(s) + (int) strlen(first_tag) + first_indent) < width) {
357 printf("%s%s%s\n", fi, first_tag, s);
365 * 's' points to the beginning of the string we want to print.
366 * We point 'cp' to the end of the maximum amount of text we
367 * can print (i.e., total width less the indentation and the
368 * length of the tag). Once we have set 'cp' initially we
369 * back it up to the first space character.
371 cp = s + width - first_indent - strlen(first_tag);
372 while (!isspace((unsigned char)*cp) && (cp != s))
376 * Once there, we change that space character to a null, print the
377 * string, and then restore the space character.
381 printf("%s%s%s\n", fi, first_tag, s);
385 * Since 'cp' may have been set to a space initially (and so no
386 * back-tracking was performed), it could have a space after it
387 * as well. We should gobble up all of these since we don't want
388 * unexpected leading blanks.
390 for (s = cp + 1; isspace((unsigned char)*s); s++)
393 /* now do all of the other lines */
395 if (((int) strlen(s) + (int) strlen(tag) + indent) < width) {
396 printf("%s%s%s\n", i, tag, s);
402 cp = s + width - indent - strlen(tag);
403 while (!isspace((unsigned char)*cp) && (cp != s))
407 printf("%s%s%s\n", i, tag, s);
409 *cp = c; /* don't mess up 's' */
414 #define OUT_OF_QUOTE 1
417 strip_ignore_chars( char *cp )
419 int had_a_comma = FALSE;
420 int flag = OUT_OF_QUOTE;
421 register char *rcp, *cp1;
426 printf("strip_ignore_chars(%s)\n", cp);
428 for (rcp = cp; *rcp != '\0'; rcp++)
429 if (isignorechar(*rcp) || (*rcp == '"'))
434 cp1 = tmp = (char *) Malloc((unsigned) strlen(cp));
435 for (rcp = cp; *rcp != '\0'; rcp++) {
436 /* toss quotes and flip the flag */
438 flag = OUT_OF_QUOTE - flag;
439 else if (isignorechar(*rcp)) {
440 if (flag == OUT_OF_QUOTE)
445 else if (*rcp == ',') {
454 /* re-quote the name if it had a comma in it */
455 if (had_a_comma == TRUE) {
456 rcp = cp1 = (char *) Malloc((unsigned) (strlen(tmp) + 3));
471 case LDAP_MOD_ADD : return("ADD");
472 case LDAP_MOD_DELETE : return("DELETE");
473 case LDAP_MOD_REPLACE : return("REPLACE");
474 default : return("?????");
479 friendly_name( char *s )
481 static FriendlyMap *map = NULL;
484 cp = ldap_friendly_name(FRIENDLYFILE, s, &map);
492 /* return TRUE if s has the syntax of a uniqname */
494 isauniqname( char *s )
498 if ((i < 3) || (i > 8)) /* uniqnames are 3-8 chars */
500 if (!isalpha((unsigned char)*s)) /* uniqnames begin with a letter */
502 for ( ; *s != '\0'; s++) /* uniqnames are alphanumeric */
503 if (!isalnum((unsigned char)*s))
509 /* return TRUE if this attribute should be printed as a DN */
515 for (i = 0; attrlist[i].quipu_name != NULL; i++)
516 if (!strcasecmp(s, attrlist[i].quipu_name))
518 if (attrlist[i].flags & ATTR_FLAG_IS_A_DN)
524 my_ldap_dn2ufn( char *s )
527 static char short_DN[BUFSIZ];
529 if (strstr(s, NULL) == NULL)
530 return(ldap_dn2ufn(s));
531 cpp = ldap_explode_dn(s, TRUE);
532 sprintf(short_DN, "%s, %s", *cpp, *(cpp + 1));
533 ldap_value_free(cpp);
537 /* return TRUE if this attribute should be printed as a URL */
543 for (i = 0; attrlist[i].quipu_name != NULL; i++)
544 if (!strcasecmp(s, attrlist[i].quipu_name))
546 if (attrlist[i].flags & ATTR_FLAG_IS_A_URL)
551 /* return TRUE if this attribute should be printed as a date and time */
557 for (i = 0; attrlist[i].quipu_name != NULL; i++)
558 if (!strcasecmp(s, attrlist[i].quipu_name))
560 if (attrlist[i].flags & ATTR_FLAG_IS_A_DATE)
566 Malloc( unsigned int size )
570 void_ptr = (void *) malloc(size);
571 if (void_ptr == NULL) {
597 while (isspace((unsigned char) *s) && (*s != '\0'))
607 free_mod_struct( LDAPMod *modp )
609 if (modp->mod_values != NULL)
610 (void) ldap_value_free(modp->mod_values);
611 Free(modp->mod_type);
616 StrFreeDup( char **ptr, char *new_value )
620 if (new_value == NULL)
623 *ptr = strdup(new_value);
628 confirm_action( char *msg )
630 char tmp[SMALL_BUF_SIZE];
635 format( msg, 75, 2 );
638 printf("\n Is this OK? ");
641 fetch_buffer(tmp, sizeof(tmp), stdin);
644 ( !strncasecmp(tmp, "YES", i) || !strncasecmp(tmp, "OK", i)));