summaryrefslogtreecommitdiffstats
path: root/src/lib/krb5/os/an_to_ln.c
blob: 39528d27288bf203780bc90433c50f2d545b6302 (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
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
/*
 * lib/krb5/os/an_to_ln.c
 *
 * Copyright 1990,1991 by the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 * 
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 * 
 *
 * krb5_aname_to_localname()
 */

/*
 * We're only to AN_TO_LN rules at this point, and not doing the
 * database lookup  (moved from configure script)
 */
#define AN_TO_LN_RULES

#include "k5-int.h"
#include <ctype.h>
#if	HAVE_REGEX_H
#include <regex.h>
#endif	/* HAVE_REGEX_H */
/*
 * Use compile(3) if no regcomp present.
 */
#if	!defined(HAVE_REGCOMP) && defined(HAVE_REGEXPR_H) && defined(HAVE_COMPILE)
#define	RE_BUF_SIZE	1024
#include <regexpr.h>
#endif	/* !HAVE_REGCOMP && HAVE_REGEXP_H && HAVE_COMPILE */

#define	MAX_FORMAT_BUFFER	1024
#ifndef	min
#define	min(a,b)	((a>b) ? b : a)
#endif	/* min */
#ifdef ANAME_DB
/*
 * Use standard DBM code.
 */
#define	KDBM_OPEN(db, fl, mo)	dbm_open(db, fl, mo)
#define	KDBM_CLOSE(db)		dbm_close(db)
#define	KDBM_FETCH(db, key)	dbm_fetch(db, key)
#endif /*ANAME_DB*/

/*
 * Find the portion of the flattened principal name that we use for mapping.
 */
static char *
aname_full_to_mapping_name(fprincname)
    char *fprincname;
{
    char	*atp;
    size_t	mlen;
    char	*mname;

    mname = (char *) NULL;
    if (fprincname) {
	atp = strrchr(fprincname, '@');
	if (!atp)
	    atp = &fprincname[strlen(fprincname)];
	mlen = (size_t) (atp - fprincname);
	
	if ((mname = (char *) malloc(mlen+1))) {
	    strncpy(mname, fprincname, mlen);
	    mname[mlen] = '\0';
	}
    }
    return(mname);
}

#ifdef ANAME_DB
/*
 * Implementation:  This version uses a DBM database, indexed by aname,
 * to generate a lname.
 *
 * The entries in the database are normal C strings, and include the trailing
 * null in the DBM datum.size.
 */
static krb5_error_code
db_an_to_ln(context, dbname, aname, lnsize, lname)
    krb5_context context;
    char *dbname;
    krb5_const_principal aname;
    const int lnsize;
    char *lname;
{
#if	(!defined(_MSDOS) && !defined(_WIN32) && !defined(macintosh))
    DBM *db;
    krb5_error_code retval;
    datum key, contents;
    char *princ_name;

    if ((retval = krb5_unparse_name(context, aname, &princ_name)))
	return(retval);
    key.dptr = princ_name;
    key.dsize = strlen(princ_name)+1;	/* need to store the NULL for
					   decoding */

    db = KDBM_OPEN(dbname, O_RDONLY, 0600);
    if (!db) {
	krb5_xfree(princ_name);
	return KRB5_LNAME_CANTOPEN;
    }

    contents = KDBM_FETCH(db, key);

    krb5_xfree(princ_name);

    if (contents.dptr == NULL) {
	retval = KRB5_LNAME_NOTRANS;
    } else {
	strncpy(lname, contents.dptr, lnsize);
	if (lnsize < contents.dsize)
	    retval = KRB5_CONFIG_NOTENUFSPACE;
	else if (lname[contents.dsize-1] != '\0')
	    retval = KRB5_LNAME_BADFORMAT;
	else
	    retval = 0;
    }
    /* can't close until we copy the contents. */
    (void) KDBM_CLOSE(db);
    return retval;
#else	/* !_MSDOS && !_WIN32 && !MACINTOSH */
    /*
     * If we don't have support for a database mechanism, then we can't
     * translate this now, can we?
     */
    return KRB5_LNAME_NOTRANS;
#endif	/* !_MSDOS && !_WIN32 && !MACINTOSH */
}
#endif /*ANAME_DB*/

#ifdef	AN_TO_LN_RULES
/*
 * Format and transform a principal name to a local name.  This is particularly
 * useful when Kerberos principals and local user names are formatted to
 * some particular convention.
 *
 * There are three parts to each rule:
 * First part - formulate the string to perform operations on:  If not present
 * then the string defaults to the fully flattened principal minus the realm
 * name.  Otherwise the syntax is as follows:
 *	"[" <ncomps> ":" <format> "]"
 *		Where:
 *			<ncomps> is the number of expected components for this
 *			rule.  If the particular principal does not have this
 *			many components, then this rule does not apply.
 *
 *			<format> is a string of <component> or verbatim
 *			characters to be inserted.
 *
 *			<component> is of the form "$"<number> to select the
 *			<number>th component.  <number> begins from 1.
 *
 * Second part - select rule validity:  If not present, then this rule may
 * apply to all selections.  Otherwise the syntax is as follows:
 *	"(" <regexp> ")"
 *		Where:	<regexp> is a selector regular expression.  If this
 *			regular expression matches the whole pattern generated
 *			from the first part, then this rule still applies.
 *
 * Last part - Transform rule:  If not present, then the selection string
 * is passed verbatim and is matched.  Otherwise, the syntax is as follows:
 *	<rule> ...
 *		Where:	<rule> is of the form:
 *			"s/" <regexp> "/" <text> "/" ["g"]
 * 
 * In order to be able to select rule validity, the native system must support
 * one of compile(3), re_comp(3) or regcomp(3).  In order to be able to
 * transform (e.g. substitute), the native system must support regcomp(3) or
 * compile(3).
 */

/*
 * aname_do_match() 	- Does our name match the parenthesized regular
 *			  expression?
 * 
 * Chew up the match portion of the regular expression and update *contextp.
 * If no re_comp() or regcomp(), then always return a match.
 */
static krb5_error_code
aname_do_match(string, contextp)
    char	*string;
    char	**contextp;
{
    krb5_error_code	kret;
    char		*regexp, *startp, *endp = 0;
    size_t		regexlen;
#if	HAVE_REGCOMP
    regex_t		match_exp;
    regmatch_t		match_match;
#elif	HAVE_REGEXPR_H
    char		regexp_buffer[RE_BUF_SIZE];
#endif	/* HAVE_REGEXP_H */

    kret = 0;
    /*
     * Is this a match expression?
     */
    if (**contextp == '(') {
	kret = KRB5_CONFIG_BADFORMAT;
	startp = (*contextp) + 1;
	endp = strchr(startp, ')');
	/* Find the end of the match expression. */
	if (endp) {
	    regexlen = (size_t) (endp - startp);
	    regexp = (char *) malloc((size_t) regexlen+1);
	    kret = ENOMEM;
	    if (regexp) {
		strncpy(regexp, startp, regexlen);
		regexp[regexlen] = '\0';
		kret = KRB5_LNAME_NOTRANS;
		/*
		 * Perform the match.
		 */
#if	HAVE_REGCOMP
		if (!regcomp(&match_exp, regexp, REG_EXTENDED) &&
		    !regexec(&match_exp, string, 1, &match_match, 0)) {
		    if ((match_match.rm_so == 0) &&
			(match_match.rm_eo == strlen(string)))
			kret = 0;
		}
		regfree(&match_exp);
#elif	HAVE_REGEXPR_H
		compile(regexp,
			regexp_buffer,
			&regexp_buffer[RE_BUF_SIZE]);
		if (step(string, regexp_buffer)) {
		    if ((loc1 == string) &&
			(loc2 == &string[strlen(string)]))
			kret = 0;
		}
#elif	HAVE_RE_COMP
		if (!re_comp(regexp) && re_exec(string))
		    kret = 0;
#else	/* HAVE_RE_COMP */
		kret = 0;
#endif	/* HAVE_RE_COMP */
		free(regexp);
	    }
	    endp++;
	}
	else
	    endp = startp;
    }
    *contextp = endp;
    return(kret);
}

/*
 * do_replacement()	- Replace the regular expression with the specified
 * 			  replacement.
 *
 * If "doall" is set, it's a global replacement, otherwise, just a oneshot
 * deal.
 * If no regcomp() then just return the input string verbatim in the output
 * string.
 */
static void
do_replacement(regexp, repl, doall, in, out)
    char	*regexp;
    char	*repl;
    int		doall;
    char	*in;
    char	*out;
{
#if	HAVE_REGCOMP
    regex_t	match_exp;
    regmatch_t	match_match;
    int		matched;
    char	*cp;
    char	*op;

    if (!regcomp(&match_exp, regexp, REG_EXTENDED)) {
	cp = in;
	op = out;
	matched = 0;
	do {
	    if (!regexec(&match_exp, cp, 1, &match_match, 0)) {
		if (match_match.rm_so) {
		    strncpy(op, cp, match_match.rm_so);
		    op += match_match.rm_so;
		}
		strcpy(op, repl);
		op += strlen(repl);
		cp += match_match.rm_eo;
		if (!doall)
		    strcpy(op, cp);
		matched = 1;
	    }
	    else {
		strcpy(op, cp);
		matched = 0;
	    }
	} while (doall && matched);
	regfree(&match_exp);
    }
#elif	HAVE_REGEXPR_H
    int		matched;
    char	*cp;
    char	*op;
    char	regexp_buffer[RE_BUF_SIZE];
    size_t	sdispl, edispl;

    compile(regexp,
	    regexp_buffer,
	    &regexp_buffer[RE_BUF_SIZE]);
    cp = in;
    op = out;
    matched = 0;
    do {
	if (step(cp, regexp_buffer)) {
	    sdispl = (size_t) (loc1 - cp);
	    edispl = (size_t) (loc2 - cp);
	    if (sdispl) {
		strncpy(op, cp, sdispl);
		op += sdispl;
	    }
	    strcpy(op, repl);
	    op += strlen(repl);
	    cp += edispl;
	    if (!doall)
		strcpy(op, cp);
	    matched = 1;
	}
	else {
	    strcpy(op, cp);
	    matched = 0;
	}
    } while (doall && matched);
#else	/* HAVE_REGEXP_H */
    strcpy(out, in);
#endif	/* HAVE_REGCOMP */
}

/*
 * aname_replacer()	- Perform the specified substitutions on the input
 *			  string and return the result.
 *
 * This routine enforces the "s/<pattern>/<replacement>/[g]" syntax.
 */
static krb5_error_code
aname_replacer(string, contextp, result)
    char	*string;
    char	**contextp;
    char	**result;
{
    krb5_error_code	kret;
    char		*in;
    char		*out;
    char		*cp, *ep, *tp;
    char		*rule, *repl;
    size_t		rule_size, repl_size;
    int			doglobal;

    kret = ENOMEM;
    *result = (char *) NULL;
    /* Allocate the formatting buffers */
    if ((in = (char *) malloc(MAX_FORMAT_BUFFER)) &&
	(out = (char *) malloc(MAX_FORMAT_BUFFER))) {
	/*
	 * Prime the buffers.  Copy input string to "out" to simulate it
	 * being the result of an initial iteration.
	 */
	strcpy(out, string);
	in[0] = '\0';
	kret = 0;
	/*
	 * Pound through the expression until we're done.
	 */
	for (cp = *contextp; *cp; ) {
	    /* Skip leading whitespace */
	    while (isspace(*cp))
		cp++;

	    /*
	     * Find our separators.  First two characters must be "s/"
	     * We must also find another "/" followed by another "/".
	     */
	    if ((cp[0] == 's') &&
		(cp[1] == '/') &&
		(ep = strchr(&cp[2], '/')) &&
		(tp = strchr(&ep[1], '/'))) {

		/* Figure out sizes of strings and allocate them */
		rule_size = (size_t) (ep - &cp[2]);
		repl_size = (size_t) (tp - &ep[1]);
		if ((rule = (char *) malloc(rule_size+1)) &&
		    (repl = (char *) malloc(repl_size+1))) {

		    /* Copy the strings */
		    strncpy(rule, &cp[2], rule_size);
		    strncpy(repl, &ep[1], repl_size);
		    rule[rule_size] = repl[repl_size] = '\0';

		    /* Check for trailing "g" */
		    doglobal = (tp[1] == 'g') ? 1 : 0;
		    if (doglobal)
			tp++;

		    /* Swap previous in and out buffers */
		    ep = in;
		    in = out;
		    out = ep;

		    /* Do the replacemenbt */
		    do_replacement(rule, repl, doglobal, in, out);
		    free(rule);
		    free(repl);

		    /* If we have no output buffer left, this can't be good */
		    if (strlen(out) == 0) {
			kret = KRB5_LNAME_NOTRANS;
			break;
		    }
		}
		else {
		    /* No memory for copies */
		    kret = ENOMEM;
		    break;
		}
	    }
	    else {
		/* Bad syntax */
		kret = KRB5_CONFIG_BADFORMAT;
		break;
	    }
	    /* Advance past trailer */
	    cp = &tp[1];
	}
	free(in);
	if (!kret)
	    *result = out;
	else
	    free(out);
    }
    return(kret);
}

/*
 * rule_an_to_ln()	- Handle aname to lname translations for RULE rules.
 *
 * The initial part of this routine handles the formulation of the strings from
 * the principal name.
 */
static krb5_error_code
rule_an_to_ln(context, rule, aname, lnsize, lname)
    krb5_context		context;
    char *			rule;
    krb5_const_principal	aname;
    const int			lnsize;
    char *			lname;
{
    krb5_error_code	kret;
    char		*current;
    char		*fprincname;
    char		*selstring = 0;
    int			num_comps, compind;
    char		*cout;
    krb5_data		*datap;
    char		*outstring;

    /*
     * First flatten the name.
     */
    current = rule;
    if (!(kret = krb5_unparse_name(context, aname, &fprincname))) {
	/*
	 * First part.
	 */
	if (*current == '[') {
	    if (sscanf(current+1,"%d:", &num_comps) == 1) {
		if (num_comps == aname->length) {
		    /*
		     * We have a match based on the number of components.
		     */
		    current = strchr(current, ':');
		    selstring = (char *) malloc(MAX_FORMAT_BUFFER);
		    if (current && selstring) {
			current++;
			cout = selstring;
			/*
			 * Plow through the string.
			 */
			while ((*current != ']') &&
			       (*current != '\0')) {
			    /*
			     * Expand to a component.
			     */
			    if (*current == '$') {
				if ((sscanf(current+1, "%d", &compind) == 1) &&
				    (compind <= num_comps) &&
				    (datap = krb5_princ_component(context,
								  aname,
								  compind-1))
				    ) {
				    strncpy(cout,
					    datap->data,
					    datap->length);
				    cout += datap->length;
				    *cout = '\0';
				    current++;
				    /* Point past number */
				    while (isdigit(*current))
					current++;
				}
				else
				    kret = KRB5_CONFIG_BADFORMAT;
			    }
			    else {
				/* Copy in verbatim. */
				*cout = *current;
				cout++;
				*cout = '\0';
				current++;
			    }
			}

			/*
			 * Advance past separator if appropriate.
			 */
			if (*current == ']')
			    current++;
			else
			    kret = KRB5_CONFIG_BADFORMAT;

			if (kret)
			    free(selstring);
		    }
		}
		else
		    kret = KRB5_LNAME_NOTRANS;
	    }
	    else
		kret = KRB5_CONFIG_BADFORMAT;
	}
	else {
	    if (!(selstring = aname_full_to_mapping_name(fprincname)))
		kret = ENOMEM;
	}
	krb5_xfree(fprincname);
    }
    if (!kret) {
	/*
	 * Second part
	 */
	if (*current == '(')
	    kret = aname_do_match(selstring, &current);

	/*
	 * Third part.
	 */
	if (!kret) {
	    outstring = (char *) NULL;
	    kret = aname_replacer(selstring, &current, &outstring);
	    if (outstring) {
		/* Copy out the value if there's enough room */
		if (strlen(outstring)+1 <= (size_t) lnsize)
		    strcpy(lname, outstring);
		else
		    kret = KRB5_CONFIG_NOTENUFSPACE;
		free(outstring);
	    }
	}
	free(selstring);
    }

    return(kret);
}
#endif	/* AN_TO_LN_RULES */

/*
 * Implementation:  This version checks the realm to see if it is the local
 * realm; if so, and there is exactly one non-realm component to the name,
 * that name is returned as the lname.
 */
static krb5_error_code
default_an_to_ln(context, aname, lnsize, lname)
    krb5_context context;
    krb5_const_principal aname;
    const int lnsize;
    char *lname;
{
    krb5_error_code retval;
    char *def_realm;
    int realm_length;

    realm_length = krb5_princ_realm(context, aname)->length;
    
    if ((retval = krb5_get_default_realm(context, &def_realm))) {
	return(retval);
    }
    if (((size_t) realm_length != strlen(def_realm)) ||
        (memcmp(def_realm, krb5_princ_realm(context, aname)->data, realm_length))) {
        free(def_realm);
        return KRB5_LNAME_NOTRANS;
    }

    if (krb5_princ_size(context, aname) != 1) {
        if (krb5_princ_size(context, aname) == 2 ) {
           /* Check to see if 2nd component is the local realm. */
           if ( strncmp(krb5_princ_component(context, aname,1)->data,def_realm,
                        realm_length) ||
                realm_length != krb5_princ_component(context, aname,1)->length)
                return KRB5_LNAME_NOTRANS;
        }
        else
           /* no components or more than one component to non-realm part of name
           --no translation. */
            return KRB5_LNAME_NOTRANS;
    }

    free(def_realm);
    strncpy(lname, krb5_princ_component(context, aname,0)->data, 
	    min(krb5_princ_component(context, aname,0)->length,lnsize));
    if (lnsize < krb5_princ_component(context, aname,0)->length ) {
	retval = KRB5_CONFIG_NOTENUFSPACE;
    } else {
	lname[krb5_princ_component(context, aname,0)->length] = '\0';
	retval = 0;
    }
    return retval;
}

/*
 Converts an authentication name to a local name suitable for use by
 programs wishing a translation to an environment-specific name (e.g.
 user account name).

 lnsize specifies the maximum length name that is to be filled into
 lname.
 The translation will be null terminated in all non-error returns.

 returns system errors, NOT_ENOUGH_SPACE
*/

krb5_error_code
krb5_aname_to_localname(context, aname, lnsize, lname)
    krb5_context context;
    krb5_const_principal aname;
    const int lnsize;
    char *lname;
{
    krb5_error_code	kret;
    char		*realm;
    char		*pname;
    char		*mname;
    const char		*hierarchy[5];
    char		**mapping_values;
    int			i, nvalid;
    char		*cp;
    char		*typep, *argp;

    /*
     * First get the default realm.
     */
    if (!(kret = krb5_get_default_realm(context, &realm))) {
	/* Flatten the name */
	if (!(kret = krb5_unparse_name(context, aname, &pname))) {
	    if ((mname = aname_full_to_mapping_name(pname))) {
		/*
		 * Search first for explicit mappings of the form:
		 *
		 * [realms]->realm->"auth_to_local_names"->mapping_name
		 */
		hierarchy[0] = "realms";
		hierarchy[1] = realm;
		hierarchy[2] = "auth_to_local_names";
		hierarchy[3] = mname;
		hierarchy[4] = (char *) NULL;
		if (!(kret = profile_get_values(context->profile,
						hierarchy,
						&mapping_values))) {
		    /* We found one or more explicit mappings. */
		    for (nvalid=0; mapping_values[nvalid]; nvalid++);

		    /* Just use the last one. */
		    /* Trim the value. */
		    cp = &mapping_values[nvalid-1]
			[strlen(mapping_values[nvalid-1])];
		    while (isspace(*cp)) cp--;
		    cp++;
		    *cp = '\0';

		    /* Copy out the value if there's enough room */
		    if (strlen(mapping_values[nvalid-1])+1 <= (size_t) lnsize)
			strcpy(lname, mapping_values[nvalid-1]);
		    else
			kret = KRB5_CONFIG_NOTENUFSPACE;

		    /* Free residue */
		    profile_free_list(mapping_values);
		}
		else {
		    /*
		     * OK - There's no explicit mapping.  Now check for
		     * general auth_to_local rules of the form:
		     *
		     * [realms]->realm->"auth_to_local"
		     *
		     * This can have one or more of the following kinds of
		     * values:
		     *	DB:<filename>	- Look up principal in aname database.
		     *	RULE:<sed-exp>	- Formulate lname from sed-exp.
		     *	DEFAULT		- Use default rule.
		     * The first rule to find a match is used.
		     */
		    hierarchy[0] = "realms";
		    hierarchy[1] = realm;
		    hierarchy[2] = "auth_to_local";
		    hierarchy[3] = (char *) NULL;
		    if (!(kret = profile_get_values(context->profile,
						    hierarchy,
						    &mapping_values))) {
			/*
			 * Loop through all the mapping values.
			 */
			for (i=0; mapping_values[i]; i++) {
			    typep = mapping_values[i];
			    argp = strchr(typep, ':');
			    if (argp) {
				*argp = '\0';
				argp++;
			    }
#ifdef ANAME_DB
			    if (!strcmp(typep, "DB") && argp) {
				kret = db_an_to_ln(context,
						   argp,
						   aname,
						   lnsize,
						   lname);
				if (kret != KRB5_LNAME_NOTRANS)
				    break;
			    }
			    else
#endif
#ifdef	AN_TO_LN_RULES
			    if (!strcmp(typep, "RULE") && argp) {
				kret = rule_an_to_ln(context,
						     argp,
						     aname,
						     lnsize,
						     lname);
				if (kret != KRB5_LNAME_NOTRANS)
				    break;
			    }
			    else
#endif	/* AN_TO_LN_RULES */
			    if (!strcmp(typep, "DEFAULT") && !argp) {
				kret = default_an_to_ln(context,
							aname,
							lnsize,
							lname);
				if (kret != KRB5_LNAME_NOTRANS)
				    break;
			    }
			    else {
				kret = KRB5_CONFIG_BADFORMAT;
				break;
			    }
			}

			/* We're done, clean up the droppings. */
			profile_free_list(mapping_values);
		    }
		    else {
			/*
			 * No profile relation found, try default mapping.
			 */
			kret = default_an_to_ln(context,
						aname,
						lnsize,
						lname);
		    }
		}
		free(mname);
	    }
	    else
		kret = ENOMEM;
	    krb5_xfree(pname);
	}
	krb5_xfree(realm);
    }
    return(kret);
}