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.
27 #include <ldapconfig.h>
28 #if !defined(DOS) && !defined( VMS)
29 #include <sys/types.h>
34 #else /* USE_TERMIOS */
36 #endif /* USE_TERMIOS */
47 char * mygetpass(prompt)
50 #if defined(DOS) || defined(VMS)
56 printf("->mygetpass(%s)\n", prompt);
60 while ( (c = getch()) != EOF && c != '\n' && c != '\r' )
71 static char pbuf[513];
79 printf("->mygetpass(%s)\n", prompt);
82 * Stolen from the getpass() routine. Can't use the plain
83 * getpass() for two reasons. One is that X.500 passwords
84 * can be really, really long - much longer than 8 chars.
85 * The second is that we like to make this client available
86 * out of inetd via a Merit asynch port, and we need to be
87 * able to do telnet control codes to turn on and off line
90 if ((fi = fdopen(open("/dev/tty", 2), "r")) == NULL)
93 setbuf(fi, (char *)NULL);
94 sig = signal(SIGINT, SIG_IGN);
96 if (GETATTR(fileno(fi), &ttyb) < 0)
99 flags = GETFLAGS( ttyb );
100 SETFLAGS( ttyb, flags & ~ECHO );
102 if (SETATTR(fileno(fi), &ttyb) < 0)
106 /* blank the line if through Merit */
108 printf("%c%c%c", 255, 251, 1);
110 (void) scanf("%c%c%c", &i, &j, &k);
114 /* fetch the password */
115 fprintf(stdout, "%s", prompt);
117 for (p=pbuf; (c = getc(fi))!='\n' && c!=EOF;) {
127 if (*(p - 1) == '\r')
131 /* unblank the line if through Merit */
133 printf("%c%c%c", 255, 252, 1);
135 (void) scanf("%c%c%c", &i, &j, &k);
137 printf("\n"); fflush(stdout);
139 fprintf(stdout, "\n");
143 SETFLAGS( ttyb, flags );
145 if (SETATTR(fileno(fi), &ttyb) < 0)
148 (void) signal(SIGINT, sig);
159 void printbase(lead, s)
164 char * friendly_name();
168 printf("->printbase(%s, %s)\n", lead, s);
171 printf("%sroot\n", lead);
175 rdns = ldap_explode_dn(s, TRUE);
176 for (cp = rdns; ; ) {
177 printf("%s", friendly_name(*cp));
186 ldap_value_free(rdns);
190 fetch_buffer(buffer, length, where)
201 printf("->fetch_buffer(%x, %d, %x)\n", buffer, length, where);
204 * Fetch a buffer and strip off any leading or trailing non-printing
205 * characters, namely newlines and carriage returns.
207 if (fgets(buffer, length, where) == NULL) {
209 errno = 0; /* so fatal() doesn't bitch */
212 for (i = strlen(buffer) - 1; i >= 0 && !isprint(buffer[i]); i--)
216 while ( *p != '\0' ) {
217 if ( isprint( *p )) {
241 extern struct entry Entry;
248 printf("->isgroup()\n");
250 if ((i = attr_to_index("objectClass")) == -1)
252 vp = Entry.attrs[i].values;
253 for (i = 0; *vp != NULL; vp++) {
256 if (debug & D_GROUPS)
257 printf("class #%1d: (%s)\n", i, *vp);
259 if (!strcmp(*vp, "rfc822MailGroup"))
266 * Print out the string 's' on a field of 'width' chracters. Each line
267 * should be indented 'lead' characters.
269 format(str, width, lead)
273 char *s, *original, *leader = "";
280 printf("->format(%s, %d, %d)\n", str, width, lead);
283 fprintf(stderr, " Cannot format (%s, %d, %d)\n", str, width, lead);
287 leader = (char *) Malloc((unsigned) (lead + 1));
288 (void) memset(leader, ' ', lead);
289 *(leader + lead) = '\0';
293 * Some compilers get really unhappy with this function since it
294 * fiddles around with the first argument, which could be a string
295 * constant. We do a strdup() here so we can do whatever the hell
298 s = original = strdup(str);
300 if ((strlen(s) + lead) < width) {
301 printf("%s%s\n", leader, s);
307 cp = s + width - lead;
308 while (!isspace(*cp) && (cp != s))
313 printf("%s%s\n", leader, s);
319 * Print out the string 's' on a field of 'width' chracters. The first line
320 * should be indented 'first_indent' spaces, then followed by 'first_tag',
321 * and then followed by the first line of 's'. Subsequent lines should be
322 * indented 'indent' spaces, then followed by 'tag', and then followed by
323 * subsequent lines of 's'.
325 format2(s, first_tag, tag, first_indent, indent, width)
326 char *s, *first_tag, *tag;
327 int first_indent, indent, width;
334 if (first_tag == NULL)
340 printf("format2(\"%s\", \"%s\", \"%s\", %1d, %1d, %1d)\n", s,
341 first_tag, tag, first_indent, indent, width);
344 /* make sure the indents are sane */
345 if ((first_indent >= width) || (indent >= width)) {
346 fprintf(stderr, " Cannot format: indent too large\n");
350 /* make the indentations */
351 if (first_indent > 0) {
352 fi = (char *) Malloc((unsigned) (first_indent + 1));
353 (void) memset(fi, ' ', first_indent);
354 *(fi + first_indent) = '\0';
359 i = (char *) Malloc((unsigned) (indent + 1));
360 (void) memset(i, ' ', indent);
361 *(i + indent) = '\0';
366 /* now do the first line */
367 if ((strlen(s) + strlen(first_tag) + first_indent) < width) {
368 printf("%s%s%s\n", fi, first_tag, s);
376 * 's' points to the beginning of the string we want to print.
377 * We point 'cp' to the end of the maximum amount of text we
378 * can print (i.e., total width less the indentation and the
379 * length of the tag). Once we have set 'cp' initially we
380 * back it up to the first space character.
382 cp = s + width - first_indent - strlen(first_tag);
383 while (!isspace(*cp) && (cp != s))
387 * Once there, we change that space character to a null, print the
388 * string, and then restore the space character.
392 printf("%s%s%s\n", fi, first_tag, s);
396 * Since 'cp' may have been set to a space initially (and so no
397 * back-tracking was performed), it could have a space after it
398 * as well. We should gobble up all of these since we don't want
399 * unexpected leading blanks.
401 for (s = cp + 1; isspace(*s); s++)
404 /* now do all of the other lines */
406 if ((strlen(s) + strlen(tag) + indent) < width) {
407 printf("%s%s%s\n", i, tag, s);
413 cp = s + width - indent - strlen(tag);
414 while (!isspace(*cp) && (cp != s))
418 printf("%s%s%s\n", i, tag, s);
420 *cp = c; /* don't mess up 's' */
425 #define OUT_OF_QUOTE 1
427 char * strip_ignore_chars(cp)
430 int had_a_comma = FALSE;
431 int flag = OUT_OF_QUOTE;
432 register char *rcp, *cp1;
439 printf("strip_ignore_chars(%s)\n", cp);
441 for (rcp = cp; *rcp != '\0'; rcp++)
442 if (isignorechar(*rcp) || (*rcp == '"'))
447 cp1 = tmp = (char *) Malloc((unsigned) strlen(cp));
448 for (rcp = cp; *rcp != '\0'; rcp++) {
449 /* toss quotes and flip the flag */
451 flag = OUT_OF_QUOTE - flag;
452 else if (isignorechar(*rcp)) {
453 if (flag == OUT_OF_QUOTE)
458 else if (*rcp == ',') {
467 /* re-quote the name if it had a comma in it */
468 if (had_a_comma == TRUE) {
469 rcp = cp1 = (char *) Malloc((unsigned) (strlen(tmp) + 3));
480 char * code_to_str(i)
483 case LDAP_MOD_ADD : return("ADD");
484 case LDAP_MOD_DELETE : return("DELETE");
485 case LDAP_MOD_REPLACE : return("REPLACE");
486 default : return("?????");
490 char * friendly_name(s)
493 static FriendlyMap *map = NULL;
496 cp = ldap_friendly_name(FRIENDLYFILE, s, &map);
504 /* return TRUE if s has the syntax of a uniqname */
510 if ((i < 3) || (i > 8)) /* uniqnames are 3-8 chars */
512 if (!isalpha(*s)) /* uniqnames begin with a letter */
514 for ( ; *s != '\0'; s++) /* uniqnames are alphanumeric */
521 /* return TRUE if this attribute should be printed as a DN */
526 extern struct attribute attrlist[];
528 for (i = 0; attrlist[i].quipu_name != NULL; i++)
529 if (!strcasecmp(s, attrlist[i].quipu_name))
531 if (attrlist[i].flags & ATTR_FLAG_IS_A_DN)
536 char * my_ldap_dn2ufn(s)
540 static char short_DN[BUFSIZ];
542 if (strstr(s, UD_BASE) == NULL)
543 return(ldap_dn2ufn(s));
544 cpp = ldap_explode_dn(s, TRUE);
545 sprintf(short_DN, "%s, %s", *cpp, *(cpp + 1));
546 ldap_value_free(cpp);
550 /* return TRUE if this attribute should be printed as a URL */
555 extern struct attribute attrlist[];
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_URL)
565 /* return TRUE if this attribute should be printed as a date and time */
570 extern struct attribute attrlist[];
572 for (i = 0; attrlist[i].quipu_name != NULL; i++)
573 if (!strcasecmp(s, attrlist[i].quipu_name))
575 if (attrlist[i].flags & ATTR_FLAG_IS_A_DATE)
585 void_ptr = (void *) malloc(size);
586 if (void_ptr == NULL) {
610 while (isspace(*s) && (*s != '\0'))
619 void free_mod_struct(modp)
624 if (modp->mod_values != NULL)
625 (void) ldap_value_free(modp->mod_values);
626 Free(modp->mod_type);
630 void StrFreeDup(ptr, new_value)
631 char **ptr, *new_value;
637 if (new_value == NULL)
640 *ptr = strdup(new_value);
644 confirm_action( msg )
647 char tmp[SMALL_BUF_SIZE];
652 format( msg, 75, 2 );
655 printf("\n Is this OK? ");
658 fetch_buffer(tmp, sizeof(tmp), stdin);
661 ( !strncasecmp(tmp, "YES", i) || !strncasecmp(tmp, "OK", i)));