Danilo says we can get rid of the DLLIMP stuff now
[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.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  * 
26  *
27  * Registration functions for keytab.
28  */
29
30 #include "k5-int.h"
31
32 extern krb5_kt_ops krb5_ktf_ops;
33 extern krb5_kt_ops krb5_kts_ops;
34
35 struct krb5_kt_typelist {
36     krb5_kt_ops *ops;
37     struct krb5_kt_typelist *next;
38 };
39 static struct krb5_kt_typelist krb5_kt_typelist_file  = {
40     &krb5_ktf_ops,
41     0
42 };
43 static struct krb5_kt_typelist krb5_kt_typelist_srvtab = {
44     &krb5_kts_ops,
45     &krb5_kt_typelist_file
46 };
47 static struct krb5_kt_typelist *kt_typehead = &krb5_kt_typelist_srvtab;
48
49
50 /*
51  * Register a new key table type
52  * don't replace if it already exists; return an error instead.
53  */
54
55 krb5_error_code KRB5_CALLCONV
56 krb5_kt_register(context, ops)
57     krb5_context context;
58     krb5_kt_ops FAR *ops;
59 {
60     struct krb5_kt_typelist *t;
61     for (t = kt_typehead;t && strcmp(t->ops->prefix,ops->prefix);t = t->next)
62         ;
63     if (t) {
64         return KRB5_KT_TYPE_EXISTS;
65     }
66     if (!(t = (struct krb5_kt_typelist *) malloc(sizeof(*t))))
67         return ENOMEM;
68     t->next = kt_typehead;
69     t->ops = ops;
70     kt_typehead = t;
71     return 0;
72 }
73
74 /*
75  * Resolve a key table name into a keytab object.
76  *
77  * The name is currently constrained to be of the form "type:residual";
78  *
79  * The "type" portion corresponds to one of the registered key table
80  * types, while the "residual" portion is specific to the
81  * particular keytab type.
82  */
83
84 krb5_error_code KRB5_CALLCONV
85 krb5_kt_resolve (context, name, ktid)
86     krb5_context context;
87     krb5_const char FAR *name;
88     krb5_keytab FAR *ktid;
89 {
90     struct krb5_kt_typelist *tlist;
91     char *pfx;
92     unsigned int pfxlen;
93     const char *cp, *resid;
94     
95     cp = strchr (name, ':');
96     if (!cp) {
97             return (*krb5_kt_dfl_ops.resolve)(context, name, ktid);
98     }
99
100     pfxlen = cp - name;
101     resid = name + pfxlen + 1;
102         
103     pfx = malloc (pfxlen+1);
104     if (!pfx)
105         return ENOMEM;
106
107     memcpy (pfx, name, pfxlen);
108     pfx[pfxlen] = '\0';
109
110     *ktid = (krb5_keytab) 0;
111
112     for (tlist = kt_typehead; tlist; tlist = tlist->next) {
113         if (strcmp (tlist->ops->prefix, pfx) == 0) {
114             free(pfx);
115             return (*tlist->ops->resolve)(context, resid, ktid);
116         }
117     }
118     free(pfx);
119     return KRB5_KT_UNKNOWN_TYPE;
120 }
121
122 /*
123  * Routines to deal with externalizingt krb5_keytab.
124  *      krb5_keytab_size();
125  *      krb5_keytab_externalize();
126  *      krb5_keytab_internalize();
127  */
128 static krb5_error_code krb5_keytab_size
129         KRB5_PROTOTYPE((krb5_context, krb5_pointer, size_t *));
130 static krb5_error_code krb5_keytab_externalize
131         KRB5_PROTOTYPE((krb5_context, krb5_pointer, krb5_octet **, size_t *));
132 static krb5_error_code krb5_keytab_internalize
133         KRB5_PROTOTYPE((krb5_context,krb5_pointer *, krb5_octet **, size_t *));
134
135 /*
136  * Serialization entry for this type.
137  */
138 static const krb5_ser_entry krb5_keytab_ser_entry = {
139     KV5M_KEYTAB,                        /* Type                 */
140     krb5_keytab_size,                   /* Sizer routine        */
141     krb5_keytab_externalize,            /* Externalize routine  */
142     krb5_keytab_internalize             /* Internalize routine  */
143 };
144
145 static krb5_error_code
146 krb5_keytab_size(kcontext, arg, sizep)
147     krb5_context        kcontext;
148     krb5_pointer        arg;
149     size_t              *sizep;
150 {
151     krb5_error_code     kret;
152     krb5_keytab         keytab;
153     krb5_ser_handle     shandle;
154
155     kret = EINVAL;
156     if ((keytab = (krb5_keytab) arg) &&
157         keytab->ops &&
158         (shandle = (krb5_ser_handle) keytab->ops->serializer) &&
159         shandle->sizer)
160         kret = (*shandle->sizer)(kcontext, arg, sizep);
161     return(kret);
162 }
163
164 static krb5_error_code
165 krb5_keytab_externalize(kcontext, arg, buffer, lenremain)
166     krb5_context        kcontext;
167     krb5_pointer        arg;
168     krb5_octet          **buffer;
169     size_t              *lenremain;
170 {
171     krb5_error_code     kret;
172     krb5_keytab         keytab;
173     krb5_ser_handle     shandle;
174
175     kret = EINVAL;
176     if ((keytab = (krb5_keytab) arg) &&
177         keytab->ops &&
178         (shandle = (krb5_ser_handle) keytab->ops->serializer) &&
179         shandle->externalizer)
180         kret = (*shandle->externalizer)(kcontext, arg, buffer, lenremain);
181     return(kret);
182 }
183
184 static krb5_error_code
185 krb5_keytab_internalize(kcontext, argp, buffer, lenremain)
186     krb5_context        kcontext;
187     krb5_pointer        *argp;
188     krb5_octet          **buffer;
189     size_t              *lenremain;
190 {
191     krb5_error_code     kret;
192     krb5_ser_handle     shandle;
193
194     kret = EINVAL;
195     if ((shandle = (krb5_ser_handle) krb5_kt_dfl_ops.serializer) &&
196         shandle->internalizer)
197         kret = (*shandle->internalizer)(kcontext, argp, buffer, lenremain);
198     return(kret);
199 }
200
201 krb5_error_code KRB5_CALLCONV
202 krb5_ser_keytab_init(kcontext)
203     krb5_context        kcontext;
204 {
205     return(krb5_register_serializer(kcontext, &krb5_keytab_ser_entry));
206 }