97ea1a03eab1246f162fb9ff21d929ba6835ff82
[krb5.git] / src / lib / krb5 / keytab / ktbase.c
1 /*
2  * lib/krb5/keytab/ktbase.c
3  *
4  * Copyright 1990 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  * 
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  M.I.T. makes no representations about the suitability of
20  * this software for any purpose.  It is provided "as is" without express
21  * or implied warranty.
22  * 
23  *
24  * Registration functions for keytab.
25  */
26
27 #include "k5-int.h"
28
29 struct krb5_kt_typelist
30  {
31   krb5_kt_ops *ops;
32   struct krb5_kt_typelist *next;
33  };
34 static struct krb5_kt_typelist krb5_kt_typelist_dfl = { &krb5_kt_dfl_ops, 0 };
35 static struct krb5_kt_typelist *kt_typehead = &krb5_kt_typelist_dfl;
36
37 /*
38  * Register a new key table type
39  * don't replace if it already exists; return an error instead.
40  */
41
42 krb5_error_code INTERFACE
43 krb5_kt_register(context, ops)
44     krb5_context context;
45     krb5_kt_ops *ops;
46 {
47     struct krb5_kt_typelist *t;
48     for (t = kt_typehead;t && strcmp(t->ops->prefix,ops->prefix);t = t->next)
49         ;
50     if (t) {
51         return KRB5_KT_TYPE_EXISTS;
52     }
53     if (!(t = (struct krb5_kt_typelist *) malloc(sizeof(*t))))
54         return ENOMEM;
55     t->next = kt_typehead;
56     t->ops = ops;
57     kt_typehead = t;
58     return 0;
59 }
60
61 /*
62  * Resolve a key table name into a keytab object.
63  *
64  * The name is currently constrained to be of the form "type:residual";
65  *
66  * The "type" portion corresponds to one of the registered key table
67  * types, while the "residual" portion is specific to the
68  * particular keytab type.
69  */
70
71 krb5_error_code INTERFACE
72 krb5_kt_resolve (context, name, ktid)
73     krb5_context context;
74     const char *name;
75     krb5_keytab *ktid;
76 {
77     struct krb5_kt_typelist *tlist;
78     char *pfx, *resid, *cp;
79     int pfxlen;
80     
81     cp = strchr (name, ':');
82     if (!cp) {
83             return (*krb5_kt_dfl_ops.resolve)(context, name, ktid);
84     }
85
86     pfxlen = cp - (char *)name;
87     resid = (char *)name + pfxlen + 1;
88         
89     pfx = malloc (pfxlen+1);
90     if (!pfx)
91         return ENOMEM;
92
93     memcpy (pfx, name, pfxlen);
94     pfx[pfxlen] = '\0';
95
96     *ktid = (krb5_keytab) 0;
97
98     for (tlist = kt_typehead; tlist; tlist = tlist->next) {
99         if (strcmp (tlist->ops->prefix, pfx) == 0) {
100             free(pfx);
101             return (*tlist->ops->resolve)(context, resid, ktid);
102         }
103     }
104     free(pfx);
105     return KRB5_KT_UNKNOWN_TYPE;
106 }