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>
28 #include <ldapconfig.h>
33 mygetpass( char *prompt )
35 #if !defined(HAVE_TERMIOS) && !defined(HAVE_SGTTY_H)
41 printf("->mygetpass(%s)\n", prompt);
45 while ( (c = getch()) != EOF && c != '\n' && c != '\r' )
56 static char pbuf[513];
60 RETSIGTYPE (*sig)( int sig );
64 printf("->mygetpass(%s)\n", prompt);
67 * Stolen from the getpass() routine. Can't use the plain
68 * getpass() for two reasons. One is that LDAP passwords
69 * can be really, really long - much longer than 8 chars.
70 * The second is that we like to make this client available
71 * out of inetd via a Merit asynch port, and we need to be
72 * able to do telnet control codes to turn on and off line
75 if ((fi = fdopen(open("/dev/tty", 2), "r")) == NULL)
78 setbuf(fi, (char *)NULL);
79 sig = SIGNAL (SIGINT, SIG_IGN);
81 if (GETATTR(fileno(fi), &ttyb) < 0)
84 flags = GETFLAGS( ttyb );
85 SETFLAGS( ttyb, flags & ~ECHO );
87 if (SETATTR(fileno(fi), &ttyb) < 0)
91 /* blank the line if through Merit */
93 printf("%c%c%c", 255, 251, 1);
95 (void) scanf("%c%c%c", &i, &j, &k);
99 /* fetch the password */
100 fprintf(stdout, "%s", prompt);
102 for (p=pbuf; (c = getc(fi))!='\n' && c!=EOF;) {
112 if (*(p - 1) == '\r')
116 /* unblank the line if through Merit */
118 printf("%c%c%c", 255, 252, 1);
120 (void) scanf("%c%c%c", &i, &j, &k);
122 printf("\n"); fflush(stdout);
124 fprintf(stdout, "\n");
128 SETFLAGS( ttyb, flags );
130 if (SETATTR(fileno(fi), &ttyb) < 0)
133 (void) SIGNAL (SIGINT, sig);
145 printbase( char *lead, char *s )
152 printf("->printbase(%s, %s)\n", lead, s);
155 printf("%sroot\n", lead);
159 rdns = ldap_explode_dn(s, TRUE);
160 for (cp = rdns; ; ) {
161 printf("%s", friendly_name(*cp));
170 ldap_value_free(rdns);
175 fetch_buffer( char *buffer, int length, FILE *where )
182 printf("->fetch_buffer(%x, %d, %x)\n", buffer, length, where);
185 * Fetch a buffer and strip off any leading or trailing non-printing
186 * characters, namely newlines and carriage returns.
188 if (fgets(buffer, length, where) == NULL) {
190 errno = 0; /* so fatal() doesn't bitch */
193 for (i = strlen(buffer) - 1; i >= 0 && !isprint(buffer[i]); i--)
197 while ( *p != '\0' ) {
198 if ( isprint( *p )) {
227 printf("->isgroup()\n");
229 if ((i = attr_to_index("objectClass")) == -1)
231 vp = Entry.attrs[i].values;
232 for (i = 0; *vp != NULL; vp++) {
235 if (debug & D_GROUPS)
236 printf("class #%1d: (%s)\n", i, *vp);
238 if (!strcmp(*vp, "rfc822MailGroup"))
245 * Print out the string 's' on a field of 'width' chracters. Each line
246 * should be indented 'lead' characters.
249 format( char *str, int width, int lead )
251 char *s, *original, *leader = "";
256 printf("->format(%s, %d, %d)\n", str, width, lead);
259 fprintf(stderr, " Cannot format (%s, %d, %d)\n", str, width, lead);
263 leader = (char *) Malloc((unsigned) (lead + 1));
264 (void) memset(leader, ' ', lead);
265 *(leader + lead) = '\0';
269 * Some compilers get really unhappy with this function since it
270 * fiddles around with the first argument, which could be a string
271 * constant. We do a strdup() here so we can do whatever the hell
274 s = original = strdup(str);
276 if ((strlen(s) + lead) < width) {
277 printf("%s%s\n", leader, s);
283 cp = s + width - lead;
284 while (!isspace(*cp) && (cp != s))
289 printf("%s%s\n", leader, s);
295 * Print out the string 's' on a field of 'width' chracters. The first line
296 * should be indented 'first_indent' spaces, then followed by 'first_tag',
297 * and then followed by the first line of 's'. Subsequent lines should be
298 * indented 'indent' spaces, then followed by 'tag', and then followed by
299 * subsequent lines of 's'.
314 if (first_tag == NULL)
320 printf("format2(\"%s\", \"%s\", \"%s\", %1d, %1d, %1d)\n", s,
321 first_tag, tag, first_indent, indent, width);
324 /* make sure the indents are sane */
325 if ((first_indent >= width) || (indent >= width)) {
326 fprintf(stderr, " Cannot format: indent too large\n");
330 /* make the indentations */
331 if (first_indent > 0) {
332 fi = (char *) Malloc((unsigned) (first_indent + 1));
333 (void) memset(fi, ' ', first_indent);
334 *(fi + first_indent) = '\0';
339 i = (char *) Malloc((unsigned) (indent + 1));
340 (void) memset(i, ' ', indent);
341 *(i + indent) = '\0';
346 /* now do the first line */
347 if ((strlen(s) + strlen(first_tag) + first_indent) < width) {
348 printf("%s%s%s\n", fi, first_tag, s);
356 * 's' points to the beginning of the string we want to print.
357 * We point 'cp' to the end of the maximum amount of text we
358 * can print (i.e., total width less the indentation and the
359 * length of the tag). Once we have set 'cp' initially we
360 * back it up to the first space character.
362 cp = s + width - first_indent - strlen(first_tag);
363 while (!isspace(*cp) && (cp != s))
367 * Once there, we change that space character to a null, print the
368 * string, and then restore the space character.
372 printf("%s%s%s\n", fi, first_tag, s);
376 * Since 'cp' may have been set to a space initially (and so no
377 * back-tracking was performed), it could have a space after it
378 * as well. We should gobble up all of these since we don't want
379 * unexpected leading blanks.
381 for (s = cp + 1; isspace(*s); s++)
384 /* now do all of the other lines */
386 if ((strlen(s) + strlen(tag) + indent) < width) {
387 printf("%s%s%s\n", i, tag, s);
393 cp = s + width - indent - strlen(tag);
394 while (!isspace(*cp) && (cp != s))
398 printf("%s%s%s\n", i, tag, s);
400 *cp = c; /* don't mess up 's' */
405 #define OUT_OF_QUOTE 1
408 strip_ignore_chars( char *cp )
410 int had_a_comma = FALSE;
411 int flag = OUT_OF_QUOTE;
412 register char *rcp, *cp1;
417 printf("strip_ignore_chars(%s)\n", cp);
419 for (rcp = cp; *rcp != '\0'; rcp++)
420 if (isignorechar(*rcp) || (*rcp == '"'))
425 cp1 = tmp = (char *) Malloc((unsigned) strlen(cp));
426 for (rcp = cp; *rcp != '\0'; rcp++) {
427 /* toss quotes and flip the flag */
429 flag = OUT_OF_QUOTE - flag;
430 else if (isignorechar(*rcp)) {
431 if (flag == OUT_OF_QUOTE)
436 else if (*rcp == ',') {
445 /* re-quote the name if it had a comma in it */
446 if (had_a_comma == TRUE) {
447 rcp = cp1 = (char *) Malloc((unsigned) (strlen(tmp) + 3));
462 case LDAP_MOD_ADD : return("ADD");
463 case LDAP_MOD_DELETE : return("DELETE");
464 case LDAP_MOD_REPLACE : return("REPLACE");
465 default : return("?????");
470 friendly_name( char *s )
472 static FriendlyMap *map = NULL;
475 cp = ldap_friendly_name(FRIENDLYFILE, s, &map);
483 /* return TRUE if s has the syntax of a uniqname */
485 isauniqname( char *s )
489 if ((i < 3) || (i > 8)) /* uniqnames are 3-8 chars */
491 if (!isalpha(*s)) /* uniqnames begin with a letter */
493 for ( ; *s != '\0'; s++) /* uniqnames are alphanumeric */
500 /* return TRUE if this attribute should be printed as a DN */
506 for (i = 0; attrlist[i].quipu_name != NULL; i++)
507 if (!strcasecmp(s, attrlist[i].quipu_name))
509 if (attrlist[i].flags & ATTR_FLAG_IS_A_DN)
515 my_ldap_dn2ufn( char *s )
518 static char short_DN[BUFSIZ];
520 if (strstr(s, NULL) == NULL)
521 return(ldap_dn2ufn(s));
522 cpp = ldap_explode_dn(s, TRUE);
523 sprintf(short_DN, "%s, %s", *cpp, *(cpp + 1));
524 ldap_value_free(cpp);
528 /* return TRUE if this attribute should be printed as a URL */
534 for (i = 0; attrlist[i].quipu_name != NULL; i++)
535 if (!strcasecmp(s, attrlist[i].quipu_name))
537 if (attrlist[i].flags & ATTR_FLAG_IS_A_URL)
542 /* return TRUE if this attribute should be printed as a date and time */
548 for (i = 0; attrlist[i].quipu_name != NULL; i++)
549 if (!strcasecmp(s, attrlist[i].quipu_name))
551 if (attrlist[i].flags & ATTR_FLAG_IS_A_DATE)
557 Malloc( unsigned int size )
561 void_ptr = (void *) malloc(size);
562 if (void_ptr == NULL) {
588 while (isspace(*s) && (*s != '\0'))
598 free_mod_struct( LDAPMod *modp )
600 if (modp->mod_values != NULL)
601 (void) ldap_value_free(modp->mod_values);
602 Free(modp->mod_type);
607 StrFreeDup( char **ptr, char *new_value )
611 if (new_value == NULL)
614 *ptr = strdup(new_value);
619 confirm_action( char *msg )
621 char tmp[SMALL_BUF_SIZE];
626 format( msg, 75, 2 );
629 printf("\n Is this OK? ");
632 fetch_buffer(tmp, sizeof(tmp), stdin);
635 ( !strncasecmp(tmp, "YES", i) || !strncasecmp(tmp, "OK", i)));