util.c 12.9 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
2
3
4
5
6
7
8
9
10
11
12
/*
 * Copyright (c) 1992, 1993  Regents of the University of Michigan.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that this notice is preserved and that due credit is given
 * to the University of Michigan at Ann Arbor. The name of the University
 * may not be used to endorse or promote products derived from this
 * software without specific prior written permission. This software
 * is provided ``as is'' without express or implied warranty.
 */

Kurt Zeilenga's avatar
Kurt Zeilenga committed
13
14
#include "portable.h"

Kurt Zeilenga's avatar
Kurt Zeilenga committed
15
#include <stdio.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
16
17
#include <stdlib.h>

18
19
20
#include <ac/ctype.h>
#include <ac/errno.h>
#include <ac/signal.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
21
22
#include <ac/string.h>
#include <ac/termios.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
23
#include <ac/time.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
24
#include <ac/unistd.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
25

Kurt Zeilenga's avatar
Kurt Zeilenga committed
26
27
28
#include <lber.h>
#include <ldap.h>
#include <ldapconfig.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
29

Kurt Zeilenga's avatar
Kurt Zeilenga committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "ud.h"

#ifdef DEBUG
extern int debug;
#endif

char * mygetpass(prompt)
char *prompt;
{
#if defined(DOS) || defined(VMS)
	static char buf[256];
	int i, c;

#ifdef DEBUG
	if (debug & D_TRACE)
		printf("->mygetpass(%s)\n", prompt);
#endif
	printf("%s", prompt);
	i = 0;
	while ( (c = getch()) != EOF && c != '\n' && c != '\r' )
		buf[i++] = c;
	if ( c == EOF )
		return( NULL );
	buf[i] = '\0';
	return (buf);
#else
	int no_pass = 0;
	char i, j, k;
	TERMIO_TYPE ttyb;
	TERMFLAG_TYPE flags;
	static char pbuf[513];
	register char *p;
	register int c;
	FILE *fi;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
64
	RETSIGTYPE (*sig)();
Kurt Zeilenga's avatar
Kurt Zeilenga committed
65
66
67
68
69
70
71

#ifdef DEBUG
	if (debug & D_TRACE)
		printf("->mygetpass(%s)\n", prompt);
#endif
	/*
	 *  Stolen from the getpass() routine.  Can't use the plain
Kurt Zeilenga's avatar
Kurt Zeilenga committed
72
	 *  getpass() for two reasons.  One is that LDAP passwords
Kurt Zeilenga's avatar
Kurt Zeilenga committed
73
74
75
76
77
78
79
80
81
82
	 *  can be really, really long - much longer than 8 chars.
	 *  The second is that we like to make this client available
	 *  out of inetd via a Merit asynch port, and we need to be
	 *  able to do telnet control codes to turn on and off line
	 *  blanking.
	 */
	if ((fi = fdopen(open("/dev/tty", 2), "r")) == NULL)
		fi = stdin;
	else
		setbuf(fi, (char *)NULL);
83
	sig = SIGNAL (SIGINT, SIG_IGN);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
	if (fi != stdin) {
		if (GETATTR(fileno(fi), &ttyb) < 0)
			perror("GETATTR");
	}
	flags = GETFLAGS( ttyb );
	SETFLAGS( ttyb, flags & ~ECHO );
	if (fi != stdin) {
		if (SETATTR(fileno(fi), &ttyb) < 0)
			perror("SETATTR");
	}

	/*  blank the line if through Merit */
	if (fi == stdin) {
		printf("%c%c%c", 255, 251, 1);
		fflush(stdout);
		(void) scanf("%c%c%c", &i, &j, &k);
		fflush(stdin);
	}

	/* fetch the password */
	fprintf(stdout, "%s", prompt); 
	fflush(stdout);
	for (p=pbuf; (c = getc(fi))!='\n' && c!=EOF;) {
		if (c == '\r')
			break;
		if (p < &pbuf[512])
			*p++ = c;
	}
	if (c == EOF)
		no_pass = 1;
	else {
		*p = '\0';
		if (*(p - 1) == '\r')
			*(p - 1) = '\0';
	}

	/*  unblank the line if through Merit */
	if (fi == stdin) {
		printf("%c%c%c", 255, 252, 1);
		fflush(stdout);
		(void) scanf("%c%c%c", &i, &j, &k);
		fflush(stdin);
		printf("\n"); fflush(stdout);
	}
	fprintf(stdout, "\n"); 
	fflush(stdout);

	/* tidy up */
	SETFLAGS( ttyb, flags );
	if (fi != stdin) {
		if (SETATTR(fileno(fi), &ttyb) < 0)
			perror("SETATTR");
	}
137
	(void) SIGNAL (SIGINT, sig);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
	if (fi != stdin)
		(void) fclose(fi);
	else
		i = getchar();
	if (no_pass)
		return(NULL);
	return(pbuf);
#endif /* DOS */
}

void printbase(lead, s)
char *lead, *s;
{
	register char **cp;
	char **rdns;
	char * friendly_name();

#ifdef DEBUG
	if (debug & D_TRACE)
		printf("->printbase(%s, %s)\n", lead, s);
#endif
	if (s == NULL) {
		printf("%sroot\n", lead);
		return;
	}
	printf("%s", lead);
	rdns = ldap_explode_dn(s, TRUE);
	for (cp = rdns; ; ) {
		printf("%s", friendly_name(*cp));
		cp++;
		if (*cp == NULL) {
			printf("\n");
			break;
		}
		else
			printf(", ");
	}
	ldap_value_free(rdns);
	return;
}

fetch_buffer(buffer, length, where)
char *buffer;
int length;
FILE *where;
{
	extern LDAP *ld;
	register int i;
    	char *p;

#ifdef DEBUG
	if (debug & D_TRACE)
		printf("->fetch_buffer(%x, %d, %x)\n", buffer, length, where);
#endif
	/*
	 *  Fetch a buffer and strip off any leading or trailing non-printing
	 *  characters, namely newlines and carriage returns.
	 */
	if (fgets(buffer, length, where) == NULL) {
		if (feof(where))
			errno = 0;	 /* so fatal() doesn't bitch */
		fatal("fgets");
	}
	for (i = strlen(buffer) - 1; i >= 0 && !isprint(buffer[i]); i--)
		buffer[i] = '\0';

	p = buffer;
	while ( *p != '\0' ) {
		if ( isprint( *p )) {
			++p;
		} else {
			strcpy( p, p + 1 ); 
		}
	}

}

fatal(s)
char *s;
{
	if (errno != 0)
		perror(s);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
220
#ifdef HAVE_KERBEROS
Kurt Zeilenga's avatar
Kurt Zeilenga committed
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
	destroy_tickets();
#endif
	exit(-1);
}

isgroup()
{
	extern struct entry Entry;
	char **vp;
	register int i;
	int group = FALSE;

#ifdef DEBUG
	if (debug & D_TRACE)
		printf("->isgroup()\n");
#endif
	if ((i = attr_to_index("objectClass")) == -1)
		return(FALSE);
	vp = Entry.attrs[i].values;
	for (i = 0; *vp != NULL; vp++) {
#ifdef DEBUG
		i++;
		if (debug & D_GROUPS)
			printf("class #%1d: (%s)\n", i, *vp);
#endif
		if (!strcmp(*vp, "rfc822MailGroup"))
			group = TRUE;
	}
	return(group);
}

/*
 *  Print out the string 's' on a field of 'width' chracters.  Each line
 *  should be indented 'lead' characters.
 */
format(str, width, lead)
char *str;
int width, lead;
{
	char *s, *original, *leader = "";
	register char *cp;
	void * Malloc();
	void Free();

#ifdef DEBUG
	if (debug & D_TRACE)
		printf("->format(%s, %d, %d)\n", str, width, lead);
#endif
	if (lead >= width) {
		fprintf(stderr, "  Cannot format (%s, %d, %d)\n", str, width, lead);
		return;
	}
	if (lead > 0) {
		leader = (char *) Malloc((unsigned) (lead + 1));
		(void) memset(leader, ' ', lead);
		*(leader + lead) = '\0';
	}

	/*
	 *  Some compilers get really unhappy with this function since it
	 *  fiddles around with the first argument, which could be a string
	 *  constant.  We do a strdup() here so we can do whatever the hell
	 *  we want.
	 */
	s = original = strdup(str);
	for (;;) {
		if ((strlen(s) + lead) < width) {
			printf("%s%s\n", leader, s);
			Free(leader);
			Free(original);
			return; 
			/*NOTREACHED*/
		}
		cp = s + width - lead;
		while (!isspace(*cp) && (cp != s))
			cp--;
		*cp = '\0';
		while (isspace(*s))
			s++;
		printf("%s%s\n", leader, s);
		s = cp + 1;
	}
}

/*
 *  Print out the string 's' on a field of 'width' chracters.  The first line
 *  should be indented 'first_indent' spaces, then followed by 'first_tag', 
 *  and then followed by the first line of 's'.  Subsequent lines should be
 *  indented 'indent' spaces, then followed by 'tag', and then followed by
 *  subsequent lines of 's'.
 */
format2(s, first_tag, tag, first_indent, indent, width)
char *s, *first_tag, *tag;
int first_indent, indent, width;
{
	char c, *fi, *i;
	register char *cp;
	void * Malloc();
	void Free();

	if (first_tag == NULL)
		first_tag = "";
	if (tag == NULL)
		tag = "";
#ifdef DEBUG
	if (debug & D_TRACE)
		printf("format2(\"%s\", \"%s\", \"%s\", %1d, %1d, %1d)\n", s, 
				first_tag, tag, first_indent, indent, width);
#endif

	/* make sure the indents are sane */
	if ((first_indent >= width) || (indent >= width)) {
		fprintf(stderr, "  Cannot format:  indent too large\n");
		return;
	}

	/* make the indentations */
	if (first_indent > 0) {
		fi = (char *) Malloc((unsigned) (first_indent + 1));
		(void) memset(fi, ' ', first_indent);
		*(fi + first_indent) = '\0';
	}
	else
		fi = "";
	if (indent > 0) {
		i = (char *) Malloc((unsigned) (indent + 1));
		(void) memset(i, ' ', indent);
		*(i + indent) = '\0';
	}
	else
		i = "";

	/* now do the first line */
	if ((strlen(s) + strlen(first_tag) + first_indent) < width) {
		printf("%s%s%s\n", fi, first_tag, s);
		Free(fi);
		Free(i);
		return; 
		/*NOTREACHED*/
	}

	/*
	 *  's' points to the beginning of the string we want to print.
	 *  We point 'cp' to the end of the maximum amount of text we
	 *  can print (i.e., total width less the indentation and the
	 *  length of the tag).  Once we have set 'cp' initially we
	 *  back it up to the first space character.
	 */
	cp = s + width - first_indent - strlen(first_tag);
	while (!isspace(*cp) && (cp != s))
		cp--;

	/*
	 *  Once there, we change that space character to a null, print the
	 *  string, and then restore the space character.
	 */
	c = *cp;
	*cp = '\0';
	printf("%s%s%s\n", fi, first_tag, s);
	*cp = c;

	/*
	 *  Since 'cp' may have been set to a space initially (and so no
	 *  back-tracking was performed), it could have a space after it
	 *  as well.  We should gobble up all of these since we don't want
	 *  unexpected leading blanks.
	 */  
	for (s = cp + 1; isspace(*s); s++)
		;

	/* now do all of the other lines */
	for (;;) {
		if ((strlen(s) + strlen(tag) + indent) < width) {
			printf("%s%s%s\n", i, tag, s);
			Free(fi);
			Free(i);
			return; 
			/*NOTREACHED*/
		}
		cp = s + width - indent - strlen(tag);
		while (!isspace(*cp) && (cp != s))
			cp--;
		c = *cp;
		*cp = '\0';
		printf("%s%s%s\n", i, tag, s);
		s = cp + 1;
		*cp = c;			/* don't mess up 's' */
	}
}

#define IN_A_QUOTE   0
#define OUT_OF_QUOTE 1

char * strip_ignore_chars(cp)
char *cp;
{
	int had_a_comma = FALSE;
	int flag = OUT_OF_QUOTE;
	register char *rcp, *cp1;
	char *tmp;
	void * Malloc();
	void Free();

#ifdef DEBUG
	if (debug & D_TRACE)
		printf("strip_ignore_chars(%s)\n", cp);
#endif
	for (rcp = cp; *rcp != '\0'; rcp++)
		if (isignorechar(*rcp) || (*rcp == '"'))
			break;
	if (*rcp == '\0')
		return(cp);

	cp1 = tmp = (char *) Malloc((unsigned) strlen(cp));
	for (rcp = cp; *rcp != '\0'; rcp++) {
		/* toss quotes and flip the flag */
		if (*rcp == '"')
			flag = OUT_OF_QUOTE - flag;
		else if (isignorechar(*rcp)) {
			if (flag == OUT_OF_QUOTE)
				*cp1++ = ' ';
			else
				*cp1++ = *rcp;
		}
		else if (*rcp == ',') {
			*cp1++ = *rcp;
			had_a_comma = TRUE;
		}
		else 
			*cp1++ = *rcp;
	}
	*cp1 = '\0';

	/* re-quote the name if it had a comma in it */
	if (had_a_comma == TRUE) {
		rcp = cp1 = (char *) Malloc((unsigned) (strlen(tmp) + 3));
		*rcp++ = '"';
		*rcp = '\0';
		strcat(rcp, tmp);
		strcat(rcp, "\"");
		Free(tmp);
		tmp = cp1;
	}
	return(tmp);
}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
467
char * code_to_str(int i)
Kurt Zeilenga's avatar
Kurt Zeilenga committed
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
{
	switch(i) {
	case LDAP_MOD_ADD : return("ADD");
	case LDAP_MOD_DELETE : return("DELETE");
	case LDAP_MOD_REPLACE : return("REPLACE");
	default : return("?????");
	}
}

char * friendly_name(s)
char *s;
{
	static FriendlyMap *map = NULL;
	static char *cp;

	cp = ldap_friendly_name(FRIENDLYFILE, s, &map);
	if (cp == NULL)
		return(s);
	return(cp);
}

#ifdef UOFM

/* return TRUE if s has the syntax of a uniqname */
isauniqname(s)
char *s;
{
	int i = strlen(s);

	if ((i < 3) || (i > 8))		/* uniqnames are 3-8 chars */
		return(FALSE);
	if (!isalpha(*s))		/* uniqnames begin with a letter */
		return(FALSE);
	for ( ; *s != '\0'; s++)	/* uniqnames are alphanumeric */
		if (!isalnum(*s))
			return(FALSE);
	return(TRUE);
}
#endif

/* return TRUE if this attribute should be printed as a DN */
isadn(s)
char *s;
{
	register int i;
	extern struct attribute attrlist[];

	for (i = 0; attrlist[i].quipu_name != NULL; i++)
		if (!strcasecmp(s, attrlist[i].quipu_name))
			break;
	if (attrlist[i].flags & ATTR_FLAG_IS_A_DN)
		return(TRUE);
	return(FALSE);
}

char * my_ldap_dn2ufn(s)
char *s;
{
	register char **cpp;
	static char short_DN[BUFSIZ];

	if (strstr(s, UD_BASE) == NULL)
		return(ldap_dn2ufn(s));
	cpp = ldap_explode_dn(s, TRUE);
	sprintf(short_DN, "%s, %s", *cpp, *(cpp + 1));
	ldap_value_free(cpp);
	return(short_DN);
}

/* return TRUE if this attribute should be printed as a URL */
isaurl(s)
char *s;
{
	register int i;
	extern struct attribute attrlist[];

	for (i = 0; attrlist[i].quipu_name != NULL; i++)
		if (!strcasecmp(s, attrlist[i].quipu_name))
			break;
	if (attrlist[i].flags & ATTR_FLAG_IS_A_URL)
		return(TRUE);
	return(FALSE);
}

/* return TRUE if this attribute should be printed as a date and time */
isadate(s)
char *s;
{
	register int i;
	extern struct attribute attrlist[];

	for (i = 0; attrlist[i].quipu_name != NULL; i++)
		if (!strcasecmp(s, attrlist[i].quipu_name))
			break;
	if (attrlist[i].flags & ATTR_FLAG_IS_A_DATE)
		return(TRUE);
	return(FALSE);
}

void * Malloc(size)
unsigned int size;
{
	void *void_ptr;

	void_ptr = (void *) malloc(size);
	if (void_ptr == NULL) {
		perror("malloc");
		exit(-1);
		/*NOTREACHED*/
	}
	return(void_ptr);
}

void Free(ptr)
char *ptr;
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
584
#ifndef STDC_HEADERS
Kurt Zeilenga's avatar
Kurt Zeilenga committed
585
586
587
588
589
	if (free(ptr) < 0) {
		perror("free");
		exit(-1);
		/*NOTREACHED*/
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
590
591
592
#else
	free(ptr);
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
	return;
}

char * nextstr(s)
char *s;
{
	while (isspace(*s) && (*s != '\0'))
		s++;
	if (s == NULL)
		return(NULL);
	if (*s == '\0')
		return(NULL);
	return(s);
}

void free_mod_struct(modp)
LDAPMod *modp;
{
	void Free();

	if (modp->mod_values != NULL)
		(void) ldap_value_free(modp->mod_values);
	Free(modp->mod_type);
	Free(modp);
}

void StrFreeDup(ptr, new_value)
char **ptr, *new_value;
{
	void Free();

	if (*ptr != NULL)
		Free(*ptr);
	if (new_value == NULL)
		*ptr = NULL;
	else
		*ptr = strdup(new_value);
}


confirm_action( msg )
	char	*msg;
{ 
        char 	tmp[SMALL_BUF_SIZE];
	int	i;

	if ( msg != NULL ) {
		putchar( '\n' );
		format( msg, 75, 2 );
	}

	printf("\n  Is this OK? ");
	fflush(stdout);
	tmp[0] = '\0';
	fetch_buffer(tmp, sizeof(tmp), stdin);
	i = strlen(tmp);
	return( i > 0 &&
	    ( !strncasecmp(tmp, "YES", i) || !strncasecmp(tmp, "OK", i)));
}