2 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
8 * lib/krb5/keytab/ktbase.c
10 * Copyright 1990 by the Massachusetts Institute of Technology.
11 * All Rights Reserved.
13 * Export of this software from the United States of America may
14 * require a specific license from the United States Government.
15 * It is the responsibility of any person or organization contemplating
16 * export to obtain such a license before exporting.
18 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
19 * distribute this software and its documentation for any purpose and
20 * without fee is hereby granted, provided that the above copyright
21 * notice appear in all copies and that both that copyright notice and
22 * this permission notice appear in supporting documentation, and that
23 * the name of M.I.T. not be used in advertising or publicity pertaining
24 * to distribution of the software without specific, written prior
25 * permission. Furthermore if you modify this software you must label
26 * your software as modified software and not distribute it in such a
27 * fashion that it might be confused with the original M.I.T. software.
28 * M.I.T. makes no representations about the suitability of
29 * this software for any purpose. It is provided "as is" without express
30 * or implied warranty.
33 * Registration functions for keytab.
37 #include "k5-thread.h"
40 extern const krb5_kt_ops krb5_ktf_ops
;
41 extern const krb5_kt_ops krb5_ktf_writable_ops
;
42 extern const krb5_kt_ops krb5_kts_ops
;
44 struct krb5_kt_typelist
{
45 const krb5_kt_ops
*ops
;
46 const struct krb5_kt_typelist
*next
;
48 /* Solaris Kerberos */
49 static const struct krb5_kt_typelist krb5_kt_typelist_wrfile
= {
50 &krb5_ktf_writable_ops
,
53 /* Solaris Kerberos */
54 static const struct krb5_kt_typelist krb5_kt_typelist_file
= {
56 &krb5_kt_typelist_wrfile
58 /* Solaris Kerberos */
59 static const struct krb5_kt_typelist krb5_kt_typelist_srvtab
= {
61 &krb5_kt_typelist_file
63 static const struct krb5_kt_typelist
*kt_typehead
= &krb5_kt_typelist_srvtab
;
64 /* Lock for protecting the type list. */
65 static k5_mutex_t kt_typehead_lock
= K5_MUTEX_PARTIAL_INITIALIZER
;
67 int krb5int_kt_initialize(void)
69 return k5_mutex_finish_init(&kt_typehead_lock
);
73 krb5int_kt_finalize(void)
75 struct krb5_kt_typelist
*t
, *t_next
;
76 k5_mutex_destroy(&kt_typehead_lock
);
77 /* Solaris Kerberos */
78 for (t
= (struct krb5_kt_typelist
*)kt_typehead
; t
!= &krb5_kt_typelist_srvtab
;
80 t_next
= (struct krb5_kt_typelist
*)t
->next
;
87 * Register a new key table type
88 * don't replace if it already exists; return an error instead.
91 krb5_error_code KRB5_CALLCONV
92 krb5_kt_register(krb5_context context
, const krb5_kt_ops
*ops
)
94 const struct krb5_kt_typelist
*t
;
95 struct krb5_kt_typelist
*newt
;
98 err
= k5_mutex_lock(&kt_typehead_lock
);
101 for (t
= kt_typehead
; t
&& strcmp(t
->ops
->prefix
,ops
->prefix
);t
= t
->next
)
104 k5_mutex_unlock(&kt_typehead_lock
);
105 return KRB5_KT_TYPE_EXISTS
;
107 if (!(newt
= (struct krb5_kt_typelist
*) malloc(sizeof(*t
)))) {
108 k5_mutex_unlock(&kt_typehead_lock
);
111 newt
->next
= kt_typehead
;
114 k5_mutex_unlock(&kt_typehead_lock
);
119 * Resolve a key table name into a keytab object.
121 * The name is currently constrained to be of the form "type:residual";
123 * The "type" portion corresponds to one of the registered key table
124 * types, while the "residual" portion is specific to the
125 * particular keytab type.
129 krb5_error_code KRB5_CALLCONV
130 krb5_kt_resolve (krb5_context context
, const char *name
, krb5_keytab
*ktid
)
132 const struct krb5_kt_typelist
*tlist
;
135 const char *cp
, *resid
;
138 cp
= strchr (name
, ':');
140 return (*krb5_kt_dfl_ops
.resolve
)(context
, name
, ktid
);
145 if ( pfxlen
== 1 && isalpha((unsigned char) name
[0]) ) {
146 /* We found a drive letter not a prefix - use FILE */
147 pfx
= strdup("FILE");
153 resid
= name
+ pfxlen
+ 1;
155 pfx
= malloc (pfxlen
+1);
159 memcpy (pfx
, name
, pfxlen
);
163 *ktid
= (krb5_keytab
) 0;
165 err
= k5_mutex_lock(&kt_typehead_lock
);
169 /* Don't need to hold the lock, since entries are never modified
170 or removed once they're in the list. Just need to protect
171 access to the list head variable itself. */
172 k5_mutex_unlock(&kt_typehead_lock
);
173 for (; tlist
; tlist
= tlist
->next
) {
174 if (strcmp (tlist
->ops
->prefix
, pfx
) == 0) {
176 return (*tlist
->ops
->resolve
)(context
, resid
, ktid
);
180 return KRB5_KT_UNKNOWN_TYPE
;
184 * Routines to deal with externalizingt krb5_keytab.
185 * krb5_keytab_size();
186 * krb5_keytab_externalize();
187 * krb5_keytab_internalize();
189 static krb5_error_code krb5_keytab_size
190 (krb5_context
, krb5_pointer
, size_t *);
191 static krb5_error_code krb5_keytab_externalize
192 (krb5_context
, krb5_pointer
, krb5_octet
**, size_t *);
193 static krb5_error_code krb5_keytab_internalize
194 (krb5_context
,krb5_pointer
*, krb5_octet
**, size_t *);
197 * Serialization entry for this type.
199 static const krb5_ser_entry krb5_keytab_ser_entry
= {
200 KV5M_KEYTAB
, /* Type */
201 krb5_keytab_size
, /* Sizer routine */
202 krb5_keytab_externalize
, /* Externalize routine */
203 krb5_keytab_internalize
/* Internalize routine */
206 static krb5_error_code
207 krb5_keytab_size(krb5_context kcontext
, krb5_pointer arg
, size_t *sizep
)
209 krb5_error_code kret
;
211 krb5_ser_handle shandle
;
214 /* Solaris Kerberos */
215 keytab
= (krb5_keytab
) arg
;
216 shandle
= (krb5_ser_handle
) keytab
->ops
->serializer
;
217 if ((keytab
!= NULL
) && (keytab
->ops
) &&
218 (shandle
!= NULL
) && (shandle
->sizer
))
219 kret
= (*shandle
->sizer
)(kcontext
, arg
, sizep
);
223 static krb5_error_code
224 krb5_keytab_externalize(krb5_context kcontext
, krb5_pointer arg
, krb5_octet
**buffer
, size_t *lenremain
)
226 krb5_error_code kret
;
228 krb5_ser_handle shandle
;
231 /* Solaris Kerberos */
232 keytab
= (krb5_keytab
) arg
;
233 shandle
= (krb5_ser_handle
) keytab
->ops
->serializer
;
234 if ((keytab
!= NULL
) && (keytab
->ops
) &&
235 (shandle
!= NULL
) && (shandle
->externalizer
))
236 kret
= (*shandle
->externalizer
)(kcontext
, arg
, buffer
, lenremain
);
240 static krb5_error_code
241 krb5_keytab_internalize(krb5_context kcontext
, krb5_pointer
*argp
, krb5_octet
**buffer
, size_t *lenremain
)
243 krb5_error_code kret
;
244 krb5_ser_handle shandle
;
247 /* Solaris Kerberos */
248 shandle
= (krb5_ser_handle
) krb5_kt_dfl_ops
.serializer
;
249 if ((shandle
!= NULL
) && (shandle
->internalizer
))
250 kret
= (*shandle
->internalizer
)(kcontext
, argp
, buffer
, lenremain
);
254 krb5_error_code KRB5_CALLCONV
255 krb5_ser_keytab_init(krb5_context kcontext
)
257 return(krb5_register_serializer(kcontext
, &krb5_keytab_ser_entry
));