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>
36 char * mygetpass(prompt)
39 #if defined(DOS) || defined(VMS)
45 printf("->mygetpass(%s)\n", prompt);
49 while ( (c = getch()) != EOF && c != '\n' && c != '\r' )
60 static char pbuf[513];
68 printf("->mygetpass(%s)\n", prompt);
71 * Stolen from the getpass() routine. Can't use the plain
72 * getpass() for two reasons. One is that LDAP passwords
73 * can be really, really long - much longer than 8 chars.
74 * The second is that we like to make this client available
75 * out of inetd via a Merit asynch port, and we need to be
76 * able to do telnet control codes to turn on and off line
79 if ((fi = fdopen(open("/dev/tty", 2), "r")) == NULL)
82 setbuf(fi, (char *)NULL);
83 sig = SIGNAL (SIGINT, SIG_IGN);
85 if (GETATTR(fileno(fi), &ttyb) < 0)
88 flags = GETFLAGS( ttyb );
89 SETFLAGS( ttyb, flags & ~ECHO );
91 if (SETATTR(fileno(fi), &ttyb) < 0)
95 /* blank the line if through Merit */
97 printf("%c%c%c", 255, 251, 1);
99 (void) scanf("%c%c%c", &i, &j, &k);
103 /* fetch the password */
104 fprintf(stdout, "%s", prompt);
106 for (p=pbuf; (c = getc(fi))!='\n' && c!=EOF;) {
116 if (*(p - 1) == '\r')
120 /* unblank the line if through Merit */
122 printf("%c%c%c", 255, 252, 1);
124 (void) scanf("%c%c%c", &i, &j, &k);
126 printf("\n"); fflush(stdout);
128 fprintf(stdout, "\n");
132 SETFLAGS( ttyb, flags );
134 if (SETATTR(fileno(fi), &ttyb) < 0)
137 (void) SIGNAL (SIGINT, sig);
148 void printbase(lead, s)
153 char * friendly_name();
157 printf("->printbase(%s, %s)\n", lead, s);
160 printf("%sroot\n", lead);
164 rdns = ldap_explode_dn(s, TRUE);
165 for (cp = rdns; ; ) {
166 printf("%s", friendly_name(*cp));
175 ldap_value_free(rdns);
179 fetch_buffer(buffer, length, 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 )) {
228 extern struct entry Entry;
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.
256 format(str, width, lead)
260 char *s, *original, *leader = "";
267 printf("->format(%s, %d, %d)\n", str, width, lead);
270 fprintf(stderr, " Cannot format (%s, %d, %d)\n", str, width, lead);
274 leader = (char *) Malloc((unsigned) (lead + 1));
275 (void) memset(leader, ' ', lead);
276 *(leader + lead) = '\0';
280 * Some compilers get really unhappy with this function since it
281 * fiddles around with the first argument, which could be a string
282 * constant. We do a strdup() here so we can do whatever the hell
285 s = original = strdup(str);
287 if ((strlen(s) + lead) < width) {
288 printf("%s%s\n", leader, s);
294 cp = s + width - lead;
295 while (!isspace(*cp) && (cp != s))
300 printf("%s%s\n", leader, s);
306 * Print out the string 's' on a field of 'width' chracters. The first line
307 * should be indented 'first_indent' spaces, then followed by 'first_tag',
308 * and then followed by the first line of 's'. Subsequent lines should be
309 * indented 'indent' spaces, then followed by 'tag', and then followed by
310 * subsequent lines of 's'.
312 format2(s, first_tag, tag, first_indent, indent, width)
313 char *s, *first_tag, *tag;
314 int first_indent, indent, width;
321 if (first_tag == NULL)
327 printf("format2(\"%s\", \"%s\", \"%s\", %1d, %1d, %1d)\n", s,
328 first_tag, tag, first_indent, indent, width);
331 /* make sure the indents are sane */
332 if ((first_indent >= width) || (indent >= width)) {
333 fprintf(stderr, " Cannot format: indent too large\n");
337 /* make the indentations */
338 if (first_indent > 0) {
339 fi = (char *) Malloc((unsigned) (first_indent + 1));
340 (void) memset(fi, ' ', first_indent);
341 *(fi + first_indent) = '\0';
346 i = (char *) Malloc((unsigned) (indent + 1));
347 (void) memset(i, ' ', indent);
348 *(i + indent) = '\0';
353 /* now do the first line */
354 if ((strlen(s) + strlen(first_tag) + first_indent) < width) {
355 printf("%s%s%s\n", fi, first_tag, s);
363 * 's' points to the beginning of the string we want to print.
364 * We point 'cp' to the end of the maximum amount of text we
365 * can print (i.e., total width less the indentation and the
366 * length of the tag). Once we have set 'cp' initially we
367 * back it up to the first space character.
369 cp = s + width - first_indent - strlen(first_tag);
370 while (!isspace(*cp) && (cp != s))
374 * Once there, we change that space character to a null, print the
375 * string, and then restore the space character.
379 printf("%s%s%s\n", fi, first_tag, s);
383 * Since 'cp' may have been set to a space initially (and so no
384 * back-tracking was performed), it could have a space after it
385 * as well. We should gobble up all of these since we don't want
386 * unexpected leading blanks.
388 for (s = cp + 1; isspace(*s); s++)
391 /* now do all of the other lines */
393 if ((strlen(s) + strlen(tag) + indent) < width) {
394 printf("%s%s%s\n", i, tag, s);
400 cp = s + width - indent - strlen(tag);
401 while (!isspace(*cp) && (cp != s))
405 printf("%s%s%s\n", i, tag, s);
407 *cp = c; /* don't mess up 's' */
412 #define OUT_OF_QUOTE 1
414 char * strip_ignore_chars(cp)
417 int had_a_comma = FALSE;
418 int flag = OUT_OF_QUOTE;
419 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));
467 char * code_to_str(int i)
470 case LDAP_MOD_ADD : return("ADD");
471 case LDAP_MOD_DELETE : return("DELETE");
472 case LDAP_MOD_REPLACE : return("REPLACE");
473 default : return("?????");
477 char * friendly_name(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 */
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 */
513 extern struct attribute attrlist[];
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)
523 char * my_ldap_dn2ufn(s)
527 static char short_DN[BUFSIZ];
529 if (strstr(s, UD_BASE) == 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 */
542 extern struct attribute attrlist[];
544 for (i = 0; attrlist[i].quipu_name != NULL; i++)
545 if (!strcasecmp(s, attrlist[i].quipu_name))
547 if (attrlist[i].flags & ATTR_FLAG_IS_A_URL)
552 /* return TRUE if this attribute should be printed as a date and time */
557 extern struct attribute attrlist[];
559 for (i = 0; attrlist[i].quipu_name != NULL; i++)
560 if (!strcasecmp(s, attrlist[i].quipu_name))
562 if (attrlist[i].flags & ATTR_FLAG_IS_A_DATE)
572 void_ptr = (void *) malloc(size);
573 if (void_ptr == NULL) {
599 while (isspace(*s) && (*s != '\0'))
608 void free_mod_struct(modp)
613 if (modp->mod_values != NULL)
614 (void) ldap_value_free(modp->mod_values);
615 Free(modp->mod_type);
619 void StrFreeDup(ptr, new_value)
620 char **ptr, *new_value;
626 if (new_value == NULL)
629 *ptr = strdup(new_value);
633 confirm_action( msg )
636 char tmp[SMALL_BUF_SIZE];
641 format( msg, 75, 2 );
644 printf("\n Is this OK? ");
647 fetch_buffer(tmp, sizeof(tmp), stdin);
650 ( !strncasecmp(tmp, "YES", i) || !strncasecmp(tmp, "OK", i)));