ktbase.c revision 2881:ea6360e7e1c5
1/*
2 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
3 * Use is subject to license terms.
4 */
5
6#pragma ident	"%Z%%M%	%I%	%E% SMI"
7
8/*
9 * lib/krb5/keytab/ktbase.c
10 *
11 * Copyright 1990 by the Massachusetts Institute of Technology.
12 * All Rights Reserved.
13 *
14 * Export of this software from the United States of America may
15 *   require a specific license from the United States Government.
16 *   It is the responsibility of any person or organization contemplating
17 *   export to obtain such a license before exporting.
18 *
19 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
20 * distribute this software and its documentation for any purpose and
21 * without fee is hereby granted, provided that the above copyright
22 * notice appear in all copies and that both that copyright notice and
23 * this permission notice appear in supporting documentation, and that
24 * the name of M.I.T. not be used in advertising or publicity pertaining
25 * to distribution of the software without specific, written prior
26 * permission.  Furthermore if you modify this software you must label
27 * your software as modified software and not distribute it in such a
28 * fashion that it might be confused with the original M.I.T. software.
29 * M.I.T. makes no representations about the suitability of
30 * this software for any purpose.  It is provided "as is" without express
31 * or implied warranty.
32 *
33 *
34 * Registration functions for keytab.
35 */
36
37#include <k5-int.h>
38#include <k5-thread.h>
39#include <kt-int.h>
40
41extern const krb5_kt_ops krb5_ktf_ops;
42extern const krb5_kt_ops krb5_ktf_writable_ops;
43extern const krb5_kt_ops krb5_kts_ops;
44
45struct krb5_kt_typelist {
46    const krb5_kt_ops *ops;
47    const struct krb5_kt_typelist *next;
48};
49static const struct krb5_kt_typelist krb5_kt_typelist_wrfile  = {
50    &krb5_ktf_writable_ops,
51    0
52};
53static const struct krb5_kt_typelist krb5_kt_typelist_file  = {
54    &krb5_ktf_ops,
55    &krb5_kt_typelist_wrfile
56};
57static const struct krb5_kt_typelist krb5_kt_typelist_srvtab = {
58    &krb5_kts_ops,
59    &krb5_kt_typelist_file
60};
61static const struct krb5_kt_typelist *kt_typehead = &krb5_kt_typelist_srvtab;
62/* Lock for protecting the type list.  */
63static k5_mutex_t kt_typehead_lock = K5_MUTEX_PARTIAL_INITIALIZER;
64
65int krb5int_kt_initialize(void)
66{
67    return k5_mutex_finish_init(&kt_typehead_lock);
68}
69
70void
71krb5int_kt_finalize(void)
72{
73    struct krb5_kt_typelist *t, *t_next;
74    k5_mutex_destroy(&kt_typehead_lock);
75    for (t = (struct krb5_kt_typelist *)kt_typehead; t != &krb5_kt_typelist_srvtab;
76	t = t_next) {
77        t_next = (struct krb5_kt_typelist *)t->next;
78	free(t);
79    }
80}
81
82
83/*
84 * Register a new key table type
85 * don't replace if it already exists; return an error instead.
86 */
87/*ARGSUSED*/
88krb5_error_code KRB5_CALLCONV
89krb5_kt_register(krb5_context context, const krb5_kt_ops *ops)
90{
91    const struct krb5_kt_typelist *t;
92    struct krb5_kt_typelist *newt;
93    krb5_error_code err;
94
95    err = k5_mutex_lock(&kt_typehead_lock);
96    if (err)
97	return err;
98    for (t = kt_typehead; t && strcmp(t->ops->prefix,ops->prefix);t = t->next)
99	;
100    if (t) {
101	k5_mutex_unlock(&kt_typehead_lock);
102	return KRB5_KT_TYPE_EXISTS;
103    }
104    if (!(newt = (struct krb5_kt_typelist *) malloc(sizeof(*t)))) {
105	k5_mutex_unlock(&kt_typehead_lock);
106	return ENOMEM;
107    }
108    newt->next = kt_typehead;
109    newt->ops = ops;
110    kt_typehead = newt;
111    k5_mutex_unlock(&kt_typehead_lock);
112    return 0;
113}
114
115/*
116 * Resolve a key table name into a keytab object.
117 *
118 * The name is currently constrained to be of the form "type:residual";
119 *
120 * The "type" portion corresponds to one of the registered key table
121 * types, while the "residual" portion is specific to the
122 * particular keytab type.
123 */
124
125#include <ctype.h>
126krb5_error_code KRB5_CALLCONV
127krb5_kt_resolve (krb5_context context, const char *name, krb5_keytab *ktid)
128{
129    const struct krb5_kt_typelist *tlist;
130    char *pfx;
131    unsigned int pfxlen;
132    const char *cp, *resid;
133    krb5_error_code err;
134
135    cp = strchr (name, ':');
136    if (!cp) {
137	    return (*krb5_kt_dfl_ops.resolve)(context, name, ktid);
138    }
139
140    pfxlen = cp - name;
141
142    if ( pfxlen == 1 && isalpha(name[0]) ) {
143        /* We found a drive letter not a prefix - use FILE: */
144        pfx = strdup("FILE:");
145        if (!pfx)
146            return ENOMEM;
147
148        resid = name;
149    } else {
150        resid = name + pfxlen + 1;
151
152        pfx = malloc (pfxlen+1);
153        if (!pfx)
154            return ENOMEM;
155
156        memcpy (pfx, name, pfxlen);
157        pfx[pfxlen] = '\0';
158    }
159
160    *ktid = (krb5_keytab) 0;
161
162    err = k5_mutex_lock(&kt_typehead_lock);
163    if (err)
164	return err;
165    tlist = kt_typehead;
166    /* Don't need to hold the lock, since entries are never modified
167       or removed once they're in the list.  Just need to protect
168       access to the list head variable itself.  */
169    k5_mutex_unlock(&kt_typehead_lock);
170    for (; tlist; tlist = tlist->next) {
171	if (strcmp (tlist->ops->prefix, pfx) == 0) {
172	    free(pfx);
173	    return (*tlist->ops->resolve)(context, resid, ktid);
174	}
175    }
176    free(pfx);
177    return KRB5_KT_UNKNOWN_TYPE;
178}
179
180
181/*
182 * Routines to deal with externalizingt krb5_keytab.
183 *	krb5_keytab_size();
184 *	krb5_keytab_externalize();
185 *	krb5_keytab_internalize();
186 */
187static krb5_error_code krb5_keytab_size
188	(krb5_context, krb5_pointer, size_t *);
189static krb5_error_code krb5_keytab_externalize
190	(krb5_context, krb5_pointer, krb5_octet **, size_t *);
191static krb5_error_code krb5_keytab_internalize
192	(krb5_context,krb5_pointer *, krb5_octet **, size_t *);
193
194/*
195 * Serialization entry for this type.
196 */
197static const krb5_ser_entry krb5_keytab_ser_entry = {
198    KV5M_KEYTAB,			/* Type			*/
199    krb5_keytab_size,			/* Sizer routine	*/
200    krb5_keytab_externalize,		/* Externalize routine	*/
201    krb5_keytab_internalize		/* Internalize routine	*/
202};
203
204static krb5_error_code
205krb5_keytab_size(krb5_context kcontext, krb5_pointer arg, size_t *sizep)
206{
207    krb5_error_code	kret;
208    krb5_keytab		keytab;
209    krb5_ser_handle	shandle;
210
211    kret = EINVAL;
212    keytab = (krb5_keytab) arg;
213    shandle = (krb5_ser_handle) keytab->ops->serializer;
214    if ((keytab != NULL) && (keytab->ops) &&
215	(shandle != NULL) && (shandle->sizer))
216	kret = (*shandle->sizer)(kcontext, arg, sizep);
217    return(kret);
218}
219
220static krb5_error_code
221krb5_keytab_externalize(krb5_context kcontext, krb5_pointer arg, krb5_octet **buffer, size_t *lenremain)
222{
223    krb5_error_code	kret;
224    krb5_keytab		keytab;
225    krb5_ser_handle	shandle;
226
227    kret = EINVAL;
228    keytab = (krb5_keytab) arg;
229    shandle = (krb5_ser_handle) keytab->ops->serializer;
230    if ((keytab != NULL) && (keytab->ops) &&
231	(shandle != NULL) && (shandle->externalizer))
232	kret = (*shandle->externalizer)(kcontext, arg, buffer, lenremain);
233    return(kret);
234}
235
236static krb5_error_code
237krb5_keytab_internalize(krb5_context kcontext, krb5_pointer *argp, krb5_octet **buffer, size_t *lenremain)
238{
239    krb5_error_code	kret;
240    krb5_ser_handle	shandle;
241
242    kret = EINVAL;
243    shandle = (krb5_ser_handle) krb5_kt_dfl_ops.serializer;
244    if ((shandle != NULL) && (shandle->internalizer))
245	kret = (*shandle->internalizer)(kcontext, argp, buffer, lenremain);
246    return(kret);
247}
248
249krb5_error_code KRB5_CALLCONV
250krb5_ser_keytab_init(krb5_context kcontext)
251{
252    return(krb5_register_serializer(kcontext, &krb5_keytab_ser_entry));
253}
254