remove XXX
[krb5.git] / src / lib / krb5 / keytab / ktbase.c
1 /*
2  * $Source$
3  * $Author$
4  *
5  * Copyright 1990 by the Massachusetts Institute of Technology.
6  * All Rights Reserved.
7  *
8  * For copying and distribution information, please see the file
9  * <krb5/copyright.h>.
10  *
11  * Registration functions for keytab.
12  */
13
14 #if !defined(lint) && !defined(SABER)
15 static char rcsid_ktbase_c[] =
16 "$Id$";
17 #endif  /* !lint & !SABER */
18
19 #include <krb5/krb5.h>
20 #include <krb5/ext-proto.h>
21
22 struct krb5_kt_typelist
23  {
24   krb5_kt_ops *ops;
25   struct krb5_kt_typelist *next;
26  };
27 static struct krb5_kt_typelist krb5_kt_typelist_dfl = { &krb5_kt_dfl_ops, 0 };
28 static struct krb5_kt_typelist *kt_typehead = &krb5_kt_typelist_dfl;
29
30 /*
31  * Register a new key table type
32  * don't replace if it already exists; return an error instead.
33  */
34
35 krb5_error_code
36 krb5_kt_register(ops)
37 krb5_kt_ops *ops;
38 {
39     struct krb5_kt_typelist *t;
40     for (t = kt_typehead;t && strcmp(t->ops->prefix,ops->prefix);t = t->next)
41         ;
42     if (t) {
43         return KRB5_KT_TYPE_EXISTS;
44     }
45     if (!(t = (struct krb5_kt_typelist *) malloc(sizeof(*t))))
46         return ENOMEM;
47     t->next = kt_typehead;
48     t->ops = ops;
49     kt_typehead = t;
50     return 0;
51 }
52
53 /*
54  * Resolve a key table name into a keytab object.
55  *
56  * The name is currently constrained to be of the form "type:residual";
57  *
58  * The "type" portion corresponds to one of the registered key table
59  * types, while the "residual" portion is specific to the
60  * particular keytab type.
61  */
62
63 krb5_error_code krb5_kt_resolve (name, ktid)
64     const char *name;
65     krb5_keytab *ktid;
66 {
67     struct krb5_kt_typelist *tlist;
68     char *pfx, *resid, *cp;
69     int pfxlen;
70     
71     cp = strchr (name, ':');
72     if (!cp)
73         return KRB5_KT_BADNAME;
74
75     pfxlen = cp - (char *)name;
76     resid = (char *)name + pfxlen + 1;
77         
78     pfx = malloc (pfxlen+1);
79     if (!pfx)
80         return ENOMEM;
81
82     memcpy (pfx, name, pfxlen);
83     pfx[pfxlen] = '\0';
84
85     *ktid = (krb5_keytab) 0;
86
87     for (tlist = kt_typehead; tlist; tlist = tlist->next) {
88         if (strcmp (tlist->ops->prefix, pfx) == 0) {
89             free(pfx);
90             return (*tlist->ops->resolve)(resid, ktid);
91         }
92     }
93     free(pfx);
94     return KRB5_KT_UNKNOWN_TYPE;
95 }