2 * lib/krb5/keytab/ktbase.c
4 * Copyright 1990 by the Massachusetts Institute of Technology.
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.
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.
27 * Registration functions for keytab.
32 extern krb5_kt_ops krb5_ktf_ops;
33 extern krb5_kt_ops krb5_kts_ops;
35 struct krb5_kt_typelist {
37 struct krb5_kt_typelist *next;
39 static struct krb5_kt_typelist krb5_kt_typelist_file = {
43 static struct krb5_kt_typelist krb5_kt_typelist_srvtab = {
45 &krb5_kt_typelist_file
47 static struct krb5_kt_typelist *kt_typehead = &krb5_kt_typelist_srvtab;
51 * Register a new key table type
52 * don't replace if it already exists; return an error instead.
55 krb5_error_code KRB5_CALLCONV
56 krb5_kt_register(krb5_context context, krb5_kt_ops *ops)
58 struct krb5_kt_typelist *t;
59 for (t = kt_typehead;t && strcmp(t->ops->prefix,ops->prefix);t = t->next)
62 return KRB5_KT_TYPE_EXISTS;
64 if (!(t = (struct krb5_kt_typelist *) malloc(sizeof(*t))))
66 t->next = kt_typehead;
73 * Resolve a key table name into a keytab object.
75 * The name is currently constrained to be of the form "type:residual";
77 * The "type" portion corresponds to one of the registered key table
78 * types, while the "residual" portion is specific to the
79 * particular keytab type.
82 krb5_error_code KRB5_CALLCONV
83 krb5_kt_resolve (krb5_context context, const char *name, krb5_keytab *ktid)
85 struct krb5_kt_typelist *tlist;
88 const char *cp, *resid;
90 cp = strchr (name, ':');
92 return (*krb5_kt_dfl_ops.resolve)(context, name, ktid);
96 resid = name + pfxlen + 1;
98 pfx = malloc (pfxlen+1);
102 memcpy (pfx, name, pfxlen);
105 *ktid = (krb5_keytab) 0;
107 for (tlist = kt_typehead; tlist; tlist = tlist->next) {
108 if (strcmp (tlist->ops->prefix, pfx) == 0) {
110 return (*tlist->ops->resolve)(context, resid, ktid);
114 return KRB5_KT_UNKNOWN_TYPE;
118 * Routines to deal with externalizingt krb5_keytab.
119 * krb5_keytab_size();
120 * krb5_keytab_externalize();
121 * krb5_keytab_internalize();
123 static krb5_error_code krb5_keytab_size
124 (krb5_context, krb5_pointer, size_t *);
125 static krb5_error_code krb5_keytab_externalize
126 (krb5_context, krb5_pointer, krb5_octet **, size_t *);
127 static krb5_error_code krb5_keytab_internalize
128 (krb5_context,krb5_pointer *, krb5_octet **, size_t *);
131 * Serialization entry for this type.
133 static const krb5_ser_entry krb5_keytab_ser_entry = {
134 KV5M_KEYTAB, /* Type */
135 krb5_keytab_size, /* Sizer routine */
136 krb5_keytab_externalize, /* Externalize routine */
137 krb5_keytab_internalize /* Internalize routine */
140 static krb5_error_code
141 krb5_keytab_size(krb5_context kcontext, krb5_pointer arg, size_t *sizep)
143 krb5_error_code kret;
145 krb5_ser_handle shandle;
148 if ((keytab = (krb5_keytab) arg) &&
150 (shandle = (krb5_ser_handle) keytab->ops->serializer) &&
152 kret = (*shandle->sizer)(kcontext, arg, sizep);
156 static krb5_error_code
157 krb5_keytab_externalize(krb5_context kcontext, krb5_pointer arg, krb5_octet **buffer, size_t *lenremain)
159 krb5_error_code kret;
161 krb5_ser_handle shandle;
164 if ((keytab = (krb5_keytab) arg) &&
166 (shandle = (krb5_ser_handle) keytab->ops->serializer) &&
167 shandle->externalizer)
168 kret = (*shandle->externalizer)(kcontext, arg, buffer, lenremain);
172 static krb5_error_code
173 krb5_keytab_internalize(krb5_context kcontext, krb5_pointer *argp, krb5_octet **buffer, size_t *lenremain)
175 krb5_error_code kret;
176 krb5_ser_handle shandle;
179 if ((shandle = (krb5_ser_handle) krb5_kt_dfl_ops.serializer) &&
180 shandle->internalizer)
181 kret = (*shandle->internalizer)(kcontext, argp, buffer, lenremain);
185 krb5_error_code KRB5_CALLCONV
186 krb5_ser_keytab_init(krb5_context kcontext)
188 return(krb5_register_serializer(kcontext, &krb5_keytab_ser_entry));