1 /*
2  * Copyright 2002 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/ccache/ccbase.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 ccache.
35  */
36 
37 #include <k5-int.h>
38 
39 extern krb5_cc_ops *krb5_cc_dfl_ops;
40 struct krb5_cc_typelist
41  {
42   krb5_cc_ops *ops;
43   struct krb5_cc_typelist *next;
44  };
45 extern krb5_cc_ops krb5_mcc_ops;
46 
47 static struct krb5_cc_typelist cc_entry = { &krb5_mcc_ops, NULL };
48 
49 static struct krb5_cc_typelist *cc_typehead = &cc_entry;
50 
51 /*
52  * Register a new credentials cache type
53  * If override is set, replace any existing ccache with that type tag
54  */
55 
56 /*ARGSUSED*/
57 KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
58 krb5_cc_register(context, ops, override)
59    krb5_context context;
60    krb5_cc_ops FAR *ops;
61    krb5_boolean override;
62 {
63     struct krb5_cc_typelist *t;
64     for (t = cc_typehead;t && strcmp(t->ops->prefix,ops->prefix);t = t->next)
65 	;
66     if (t) {
67 	if (override) {
68 	    t->ops = ops;
69 	    return 0;
70 	} else
71 	    return KRB5_CC_TYPE_EXISTS;
72     }
73     if (!(t = (struct krb5_cc_typelist *) malloc(sizeof(*t))))
74 	return ENOMEM;
75     t->next = cc_typehead;
76     t->ops = ops;
77     cc_typehead = t;
78     return 0;
79 }
80 
81 /*
82  * Resolve a credential cache name into a cred. cache object.
83  *
84  * The name is currently constrained to be of the form "type:residual";
85  *
86  * The "type" portion corresponds to one of the predefined credential
87  * cache types, while the "residual" portion is specific to the
88  * particular cache type.
89  */
90 
91 KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
92 krb5_cc_resolve (context, name, cache)
93    krb5_context context;
94    const char *name;
95    krb5_ccache *cache;
96 {
97     struct krb5_cc_typelist *tlist;
98     char *pfx, *cp;
99     char *resid;
100     int pfxlen;
101 
102     cp = strchr (name, ':');
103     if (!cp) {
104 	if (krb5_cc_dfl_ops)
105 	    return (*krb5_cc_dfl_ops->resolve)(context, cache, (char *)name);
106 	else
107 	    return KRB5_CC_BADNAME;
108     }
109 
110     pfxlen = cp - name;
111     resid = (char *)name + pfxlen + 1;
112 
113     pfx = malloc (pfxlen+1);
114     if (!pfx)
115 	return ENOMEM;
116 
117     memcpy (pfx, name, pfxlen);
118     pfx[pfxlen] = '\0';
119 
120     *cache = (krb5_ccache) 0;
121 
122     for (tlist = cc_typehead; tlist; tlist = tlist->next) {
123 	if (strcmp (tlist->ops->prefix, pfx) == 0) {
124 	    free(pfx);
125 	    return (*tlist->ops->resolve)(context, cache, resid);
126 	}
127     }
128     if (krb5_cc_dfl_ops && !strcmp (pfx, krb5_cc_dfl_ops->prefix)) {
129 	free (pfx);
130 	return (*krb5_cc_dfl_ops->resolve)(context, cache, resid);
131     }
132     free(pfx);
133     return KRB5_CC_UNKNOWN_TYPE;
134 }
135