summaryrefslogtreecommitdiffstats
path: root/src/lib/krb4/kparse.c
blob: f6607a9cad708f891beceab16d66bb3038e085a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
137
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
220
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
467
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
584
585
586
587
588
589
590
591
592
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
/*
 * kparse.c
 *
 * Copyright 1988 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 *
 * Purpose:
 * This module was developed to parse the "~/.klogin" files for
 * Kerberos-authenticated rlogin/rcp/rsh services.  However, it is
 * general purpose and can be used to parse any such parameter file.
 *
 * The parameter file should consist of one or more entries, with each
 * entry on a separate line and consisting of zero or more
 * "keyword=value" combinations.  The keyword is case insensitive, but
 * the value is not.  Any string may be enclosed in quotes, and
 * c-style "\" literals are supported.  A comma may be used to
 * separate the k/v combinations, and multiple commas are ignored.
 * Whitespace (blank or tab) may be used freely and is ignored.
 *
 * Full error processing is available.  When PS_BAD_KEYWORD or
 * PS_SYNTAX is returned from fGetParameterSet(), the string ErrorMsg
 * contains a meaningful error message.
 *
 * Keywords and their default values are programmed by an external
 * table.
 *
 * Routines:
 * fGetParameterSet()      parse one line of the parameter file
 * fGetKeywordValue()      parse one "keyword=value" combo
 * fGetToken()             parse one token
 */

#include "mit-copyright.h"
#include "krb.h"
#include <stdio.h>
#include <ctype.h>
#include <kparse.h>
#include <string.h>
#include "autoconf.h"
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#ifndef FALSE
#define FALSE 0
#define TRUE 1
#endif

#define MAXKEY          80
#define MAXVALUE        80

static char *strutol (char *);


#ifndef HAVE_STRDUP
static char *strdup();
#endif
#ifndef HAVE_STDLIB_H
extern char *malloc();
#endif

static int sLineNbr=1;		/* current line nbr in parameter file */
static char ErrorMsg[80];	/* meaningful only when KV_SYNTAX, PS_SYNTAX,
				 * or PS_BAD_KEYWORD is returned by
				 * fGetKeywordValue or fGetParameterSet */

int fGetParameterSet( fp,parm,parmcount )
    FILE *fp;
    parmtable parm[];
    int parmcount;
{
    int rc,i;
    char keyword[MAXKEY];
    char value[MAXVALUE];

    while (TRUE) {
        rc=fGetKeywordValue(fp,keyword,MAXKEY,value,MAXVALUE);

        switch (rc) {

        case KV_EOF:
            return(PS_EOF);

        case KV_EOL:
            return(PS_OKAY);

        case KV_SYNTAX:
            return(PS_SYNTAX);

        case KV_OKAY:
            /*
             * got a reasonable keyword/value pair.  Search the
             * parameter table to see if we recognize the keyword; if
             * not, return an error.  If we DO recognize it, make sure
             * it has not already been given.  If not already given,
             * save the value.
             */
            for (i=0; i<parmcount; i++) {
                if (strcmp(strutol(keyword),parm[i].keyword)==0) {
                    if (parm[i].value) {
                        sprintf(ErrorMsg,"duplicate keyword \"%s\" found",
                                keyword);
                        return(PS_BAD_KEYWORD);
                    }
                    parm[i].value = strdup(value);
                    break;
                }
            }
            if (i >= parmcount) {
                sprintf(ErrorMsg, "unrecognized keyword \"%s\" found",
			keyword);
                return(PS_BAD_KEYWORD);
            }
            break;

        default:
            sprintf(ErrorMsg,
		    "panic: bad return (%d) from fGetToken()",rc);
            break;
        }
    }
}

/*
 * Routine: ParmCompare
 *
 * Purpose:
 * ParmCompare checks a specified value for a particular keyword.
 * fails if keyword not found or keyword found but the value was
 * different. Like strcmp, ParmCompare returns 0 for a match found, -1
 * otherwise
 */
int ParmCompare( parm, parmcount, keyword, value )
    parmtable parm[];
    int parmcount;
    char *keyword;
    char *value;
{
    int i;

    for (i=0; i<parmcount; i++) {
        if (strcmp(parm[i].keyword,keyword)==0) {
            if (parm[i].value) {
                return(strcmp(parm[i].value,value));
            } else {
                return(strcmp(parm[i].defvalue,value));
            }
        }
    }
    return(-1);
}

void FreeParameterSet(parm,parmcount)
    parmtable parm[];
    int parmcount;
{
    int i;

    for (i=0; i<parmcount; i++) {
        if (parm[i].value) {
            free(parm[i].value);
            parm[i].value = (char *)NULL;
        }
    }
}

int fGetKeywordValue( fp, keyword, klen, value, vlen )
    FILE *fp;
    char *keyword;
    int klen;
    char *value;
    int vlen;
{
    int rc;
    int gotit;

    *keyword = *value = '\0';   /* preset strings to NULL */

    /*
     * Looking for a keyword.
     *          return an exception for EOF or BAD_QSTRING
     *          ignore leading WHITEspace
     *          ignore any number of leading commas
     *          newline means we have all the parms for this
     *          	statement; give an indication that there is
     *          	nothing more on this line.
     *          stop looking if we find QSTRING, STRING, or NUMBER
     *          return syntax error for any other PUNKtuation
     */
    gotit = FALSE;
    do {
        rc = fGetToken(fp,keyword,klen);

        switch (rc) {

        case GTOK_WHITE:
            break;

        case GTOK_EOF:
            return(KV_EOF);

        case GTOK_BAD_QSTRING:
            sprintf(ErrorMsg,"unterminated string \"%s found",keyword);
            return(KV_SYNTAX);

        case GTOK_PUNK:
            if (strcmp("\n",keyword)==0) {
                return(KV_EOL);
            } else if (strcmp(",",keyword)!=0) {
                sprintf(ErrorMsg,"expecting rvalue, found \'%s\'",keyword);
            }
            break;

        case GTOK_STRING:
        case GTOK_QSTRING:
        case GTOK_NUMBER:
            gotit = TRUE;
            break;

        default:
            sprintf(ErrorMsg,"panic: bad return (%d) from fGetToken()",rc);
            return(KV_SYNTAX);
        }

    } while (!gotit);

    /*
     * now we expect an equal sign.
     *          skip any whitespace
     *          stop looking if we find an equal sign
     *          anything else causes a syntax error
     */
    gotit = FALSE;
    do {
        rc = fGetToken(fp,value,vlen);

        switch (rc) {

        case GTOK_WHITE:
            break;

        case GTOK_BAD_QSTRING:
            sprintf(ErrorMsg,
		    "expecting \'=\', found unterminated string \"%s",
                    value);
            return(KV_SYNTAX);

        case GTOK_PUNK:
            if (strcmp("=",value)==0) {
                gotit = TRUE;
            } else {
                if (strcmp("\n",value)==0) {
                    sprintf(ErrorMsg,"expecting \"=\", found newline");
                    fUngetChar('\n',fp);
                } else {
                    sprintf(ErrorMsg,
			    "expecting rvalue, found \'%s\'",keyword);
                }
                return(KV_SYNTAX);
            }
            break;

        case GTOK_STRING:
        case GTOK_QSTRING:
        case GTOK_NUMBER:
            sprintf(ErrorMsg,"expecting \'=\', found \"%s\"",value);
            return(KV_SYNTAX);

        case GTOK_EOF:
            sprintf(ErrorMsg,"expecting \'=\', found EOF");
            return(KV_SYNTAX);

        default:
            sprintf(ErrorMsg,
		    "panic: bad return (%d) from fGetToken()",rc);
            return(KV_SYNTAX);
        }

    } while ( !gotit );

    /*
     * got the keyword and equal sign, now get a value.
     *          ignore any whitespace
     *          any punctuation is a syntax error
     */
    gotit = FALSE;
    do {
        rc = fGetToken(fp,value,vlen);

        switch (rc) {

        case GTOK_WHITE:
            break;

        case GTOK_EOF:
            sprintf(ErrorMsg,"expecting rvalue, found EOF");
            return(KV_SYNTAX);

        case GTOK_BAD_QSTRING:
            sprintf(ErrorMsg,"unterminated quoted string \"%s",value);
            return(KV_SYNTAX);

        case GTOK_PUNK:
            if (strcmp("\n",value)==0) {
                sprintf(ErrorMsg,"expecting rvalue, found newline");
                fUngetChar('\n',fp);
            } else {
                sprintf(ErrorMsg,
			"expecting rvalue, found \'%s\'",value);
            }
            return(KV_SYNTAX);
            break;

        case GTOK_STRING:
        case GTOK_QSTRING:
        case GTOK_NUMBER:
            gotit = TRUE;
            return(KV_OKAY);

        default:
            sprintf(ErrorMsg,
		    "panic: bad return (%d) from fGetToken()",rc);
            return(KV_SYNTAX);
        }

    } while ( !gotit );
    /*NOTREACHED*/
    return 0; /* to keep gcc happy */
}

/*
 * Routine Name: fGetToken
 *
 * Function: read the next token from the specified file.
 * A token is defined as a group of characters
 * terminated by a white space char (SPACE, CR,
 * LF, FF, TAB). The token returned is stripped of
 * both leading and trailing white space, and is
 * terminated by a NULL terminator.  An alternate
 * definition of a token is a string enclosed in
 * single or double quotes.
 *
 * Explicit Parameters:
 * fp              pointer to the input FILE
 * dest    pointer to destination buffer
 * maxlen  length of the destination buffer. The buffer
 * length INCLUDES the NULL terminator.
 *
 * Implicit Parameters: stderr  where the "token too long" message goes
 *
 * External Procedures: fgetc
 *
 * Side Effects:                None
 *
 * Return Value:                A token classification value, as
 *				defined in kparse.h. Note that the
 *				classification for end of file is
 *				always zero.
 */
int fGetToken(fp, dest, maxlen)
    FILE *fp;
    char *dest;
    int  maxlen;
{
    int ch='\0';
    int len=0;
    char *p = dest;
    int digits;

    ch=fGetChar(fp);

    /*
     * check for a quoted string.  If found, take all characters
     * that fit until a closing quote is found.  Note that this
     * algorithm will not behave well for a string which is too long.
     */
    if (ISQUOTE(ch)) {
        int done = FALSE;
        do {
            ch = fGetChar(fp);
            done = ((maxlen<++len)||ISLINEFEED(ch)||(ch==EOF)
		    ||ISQUOTE(ch));
            if (ch=='\\')
                ch = fGetLiteral(fp);
            if (!done)
                *p++ = ch;
            else if ((ch!=EOF) && !ISQUOTE(ch))
                fUngetChar(ch,fp);
        } while (!done);
        *p = '\0';
        if (ISLINEFEED(ch)) return(GTOK_BAD_QSTRING);
        return(GTOK_QSTRING);
    }

    /*
     * Not a quoted string.  If its a token character (rules are
     * defined via the ISTOKENCHAR macro, in kparse.h) take it and all
     * token chars following it until we run out of space.
     */
    digits=TRUE;
    if (ISTOKENCHAR(ch)) {
        while ( (ISTOKENCHAR(ch)) && len<maxlen-1 ) {
            if (!isdigit(ch)) digits=FALSE;
            *p++ = ch;
            len++;
            ch = fGetChar(fp);
        };
        *p = '\0';

        if (ch!=EOF) {
            fUngetChar(ch,fp);
        }
        if (digits) {
            return(GTOK_NUMBER);
        } else {
            return(GTOK_STRING);
        }
    }

    /*
     * Neither a quoted string nor a token character.  Return a string
     * with just that one character in it.
     */
    if (ch==EOF) {
        return(GTOK_EOF);
    }
    if (!ISWHITESPACE(ch)) {
        *p++ = ch;
        *p='\0';
    } else {
        *p++ = ' ';		/* white space is always the
				 * blank character */
        *p='\0';
        /*
         * The character is a white space. Flush all additional white
         * space.
         */
        while (ISWHITESPACE(ch) && ((ch=fGetChar(fp)) != EOF))
            ;
        if (ch!=EOF) {
            fUngetChar(ch,fp);
        }
        return(GTOK_WHITE);
    }
    return(GTOK_PUNK);
}

/*
 * fGetLiteral is called after we find a '\' in the input stream.  A
 * string of numbers following the backslash are converted to the
 * appropriate value; hex (0xn), octal (0n), and decimal (otherwise)
 * are all supported.  If the char after the \ is not a number, we
 * special case certain values (\n, \f, \r, \b) or return a literal
 * otherwise (useful for \", for example).
 */
int fGetLiteral(fp)
    FILE *fp;
{
    int ch;
    int n=0;
    int base;

    ch = fGetChar(fp);

    if (!isdigit(ch)) {
        switch (ch) {
        case 'n':       return('\n');
        case 'f':       return('\f');
        case 'r':       return('\r');
        case 'b':       return('\b');
        default:        return(ch);
        }
    }

    /*
     * got a number.  might be decimal (no prefix), octal (prefix 0),
     * or hexadecimal (prefix 0x).  Set the base appropriately.
     */
    if (ch!='0') {
        base=10;                /* its a decimal number */
    } else {
        /*
         * found a zero, its either hex or octal
         */
        ch = fGetChar(fp);
        if ((ch!='x') && (ch!='X')) {
            base=010;
        } else {
            ch = fGetChar(fp);
            base=0x10;
        }
    }

    switch (base) {

    case 010:                   /* octal */
        while (ISOCTAL(ch)) {
            n = (n*base) + ch - '0';
            ch = fGetChar(fp);
        }
        break;

    case 10:                    /* decimal */
        while (isdigit(ch)) {
            n = (n*base) + ch - '0';
            ch = fGetChar(fp);
        }
        break;
    case 0x10:                  /* hexadecimal */
        while (isxdigit(ch)) {
            if (isdigit(ch)) {
                n = (n*base) + ch - '0';
            } else {
                n = (n*base) + toupper(ch) - 'A' + 0xA ;
            }
            ch = fGetChar(fp);
        }
        break;
    default:
#ifdef DEBUG
        fprintf(stderr,"fGetLiteral() died real bad. Fix kparse.c.");
#endif
        break;
    }
    fUngetChar(ch,fp);
    return(n);
}

/*
 * exactly the same as ungetc(3) except that the line number of the
 * input file is maintained.
 */
int fUngetChar(ch,fp)
    int ch;
    FILE *fp;
{
    if (ch=='\n') sLineNbr--;
    return(ungetc(ch,fp));
}


/*
 * exactly the same as fgetc(3) except that the line number of the
 * input file is maintained.
 */
int fGetChar(fp)
    FILE *fp;
{
    int ch = fgetc(fp);
    if (ch=='\n') sLineNbr++;
    return(ch);
}

/*
 * strutol changes all characters in a string to lower case, in place.
 * the pointer to the beginning of the string is returned.
 */

static char * strutol( start )
    char *start;
{
    char *q;
    for (q=start; *q; q++)
        if (isupper((unsigned char) *q))
	    *q=tolower((unsigned char) *q);
    return(start);
}

#ifdef GTOK_TEST	     /* mainline test routine for fGetToken() */

#define MAXTOKEN 100

char *pgm = "gettoken";

main(argc,argv)
    int argc;
    char **argv;
{
    char *p;
    int type;
    FILE *fp;

    if (--argc) {
        fp = fopen(*++argv,"ra");
        if (fp == (FILE *)NULL) {
            fprintf(stderr,"can\'t open \"%s\"\n",*argv);
        }
    } else
        fp = stdin;

    p = malloc(MAXTOKEN);
    while (type = fGetToken(fp,p,MAXTOKEN)) {
        switch(type) {
        case GTOK_BAD_QSTRING:
	    printf("BAD QSTRING!\t");
	    break;
        case GTOK_EOF:
	    printf("EOF!\t");
	    break;
        case GTOK_QSTRING:
	    printf("QSTRING\t");
	    break;
        case GTOK_STRING:
	    printf("STRING\t");
	    break;
        case GTOK_NUMBER:
	    printf("NUMBER\t");
	    break;
        case GTOK_PUNK:
	    printf("PUNK\t");
	    break;
        case GTOK_WHITE:
	    printf("WHITE\t");
	    break;
        default:
	    printf("HUH?\t");
	    break;
        }
        if (*p=='\n')
            printf("\\n\n");
	else
            printf("%s\n",p);
    }
    exit(0);
}
#endif

#ifdef KVTEST

main(argc,argv)
    int argc;
    char **argv;
{
    int rc,ch;
    FILE *fp;
    char key[MAXKEY],valu[MAXVALUE];
    char *filename;

    if (argc != 2) {
        fprintf(stderr,"usage: test <filename>\n");
        exit(1);
    }

    if (!(fp=fopen(*++argv,"r"))) {
        fprintf(stderr,"can\'t open input file \"%s\"\n",filename);
        exit(1);
    }
    filename = *argv;

    while ((rc=fGetKeywordValue(fp,key,MAXKEY,valu,MAXVALUE))!=KV_EOF){

        switch (rc) {

        case KV_EOL:
            printf("%s, line %d: nada mas.\n",filename,sLineNbr-1);
            break;

        case KV_SYNTAX:
            printf("%s, line %d: syntax error: %s\n",
                   filename,sLineNbr,ErrorMsg);
            while ( ((ch=fGetChar(fp))!=EOF) && (ch!='\n') );
            break;

        case KV_OKAY:
            printf("%s, line %d: okay, %s=\"%s\"\n",
                   filename,sLineNbr,key,valu);
            break;

        default:
            printf("panic: bad return (%d) from fGetKeywordValue\n",rc);
            break;
        }
    }
    printf("EOF");
    fclose(fp);
    exit(0);
}
#endif

#ifdef PSTEST

parmtable kparm[] = {
    /*  keyword, default, found value */
    { "user",       "",    (char *)NULL },
    { "realm",   "Athena", (char *)NULL },
    { "instance",   "",    (char *)NULL }
};

main(argc,argv)
    int argc;
    char **argv;
{
    int rc,i,ch;
    FILE *fp;
    char *filename;

    if (argc != 2) {
        fprintf(stderr,"usage: test <filename>\n");
        exit(1);
    }

    if (!(fp=fopen(*++argv,"r"))) {
        fprintf(stderr,"can\'t open input file \"%s\"\n",filename);
        exit(1);
    }
    filename = *argv;

    while ((rc=fGetParameterSet(fp,kparm,PARMCOUNT(kparm))) != PS_EOF) {

        switch (rc) {

        case PS_BAD_KEYWORD:
            printf("%s, line %d: %s\n",filename,sLineNbr,ErrorMsg);
            while ( ((ch=fGetChar(fp))!=EOF) && (ch!='\n') );
            break;

        case PS_SYNTAX:
            printf("%s, line %d: syntax error: %s\n",
                   filename,sLineNbr,ErrorMsg);
            while ( ((ch=fGetChar(fp))!=EOF) && (ch!='\n') );
            break;

        case PS_OKAY:
            printf("%s, line %d: valid parameter set found:\n",
                   filename,sLineNbr-1);
            for (i=0; i<PARMCOUNT(kparm); i++) {
                printf("\t%s = \"%s\"\n",kparm[i].keyword,
                       (kparm[i].value ? kparm[i].value
			: kparm[i].defvalue));
            }
            break;

        default:
            printf("panic: bad return (%d) from fGetParameterSet\n",rc);
            break;
        }
        FreeParameterSet(kparm,PARMCOUNT(kparm));
    }
    printf("EOF");
    fclose(fp);
    exit(0);
}
#endif

/*
 * Copyright (c) 1988 The Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that: (1) source distributions retain this entire copyright
 * notice and comment, and (2) distributions including binaries display
 * the following acknowledgement:  ``This product includes software
 * developed by the University of California, Berkeley and its contributors''
 * in the documentation or other materials provided with the distribution
 * and in all advertising materials mentioning features or use of this
 * software. Neither the name of the University nor the names of its
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

/* based on @(#)strdup.c	5.3 (Berkeley) 6/1/90 */

#ifndef HAVE_STRDUP
static char *
strdup(str)
	const char *str;
{
	int len;
	char *copy;

	if (!str)
		return((char *)0);
	len = strlen(str) + 1;
	if (!(copy = malloc((u_int)len)))
		return((char *)0);
	memcpy(copy, str, len);
	return(copy);
}
#endif