f7f9e44de5b376653dc94863bb1920d6cfa76750
[krb5.git] / src / lib / krb5 / rcache / rc_base.c
1 /*
2  * lib/krb5/rcache/rc_base.c
3  *
4  * This file of the Kerberos V5 software is derived from public-domain code
5  * contributed by Daniel J. Bernstein, <brnstnd@acf10.nyu.edu>.
6  *
7  */
8
9
10 /*
11  * Base "glue" functions for the replay cache.
12  */
13
14 #ifdef SEMAPHORE
15 #include <semaphore.h>
16 #endif
17 #include "rc_base.h"
18
19 #define FREE(x) ((void) free((char *) (x)))
20
21 struct krb5_rc_typelist
22  {
23   krb5_rc_ops *ops;
24   struct krb5_rc_typelist *next;
25  };
26 static struct krb5_rc_typelist krb5_rc_typelist_dfl = { &krb5_rc_dfl_ops, 0 };
27 static struct krb5_rc_typelist *typehead = &krb5_rc_typelist_dfl;
28
29 #ifdef SEMAPHORE
30 semaphore ex_typelist = 1;
31 #endif
32
33 krb5_error_code INTERFACE krb5_rc_register_type(context, ops)
34     krb5_context context;
35     krb5_rc_ops *ops;
36 {
37  struct krb5_rc_typelist *t;
38 #ifdef SEMAPHORE
39  down(&ex_typelist);
40 #endif
41  for (t = typehead;t && strcmp(t->ops->type,ops->type);t = t->next)
42    ;
43 #ifdef SEMAPHORE
44  up(&ex_typelist);
45 #endif
46  if (t)
47    return KRB5_RC_TYPE_EXISTS;
48  if (!(t = (struct krb5_rc_typelist *) malloc(sizeof(struct krb5_rc_typelist))))
49    return KRB5_RC_MALLOC;
50 #ifdef SEMAPHORE
51  down(&ex_typelist);
52 #endif
53  t->next = typehead;
54  t->ops = ops;
55  typehead = t;
56 #ifdef SEMAPHORE
57  up(&ex_typelist);
58 #endif
59  return 0;
60 }
61
62 krb5_error_code INTERFACE krb5_rc_resolve_type(context, id, type)
63     krb5_context context;
64     krb5_rcache *id;
65     char *type;
66 {
67  struct krb5_rc_typelist *t;
68 #ifdef SEMAPHORE
69  down(&ex_typelist);
70 #endif
71  for (t = typehead;t && strcmp(t->ops->type,type);t = t->next)
72    ;
73 #ifdef SEMAPHORE
74  up(&ex_typelist);
75 #endif
76  if (!t)
77    return KRB5_RC_TYPE_NOTFOUND;
78  /* allocate *id? nah */
79  (*id)->ops = t->ops;
80  return 0;
81 }
82
83 char * INTERFACE krb5_rc_get_type(context, id)
84     krb5_context context;
85     krb5_rcache id;
86 {
87  return id->ops->type;
88 }
89
90 char * INTERFACE krb5_rc_default_type(context)
91     krb5_context context;
92 {
93  char *s;
94  if (s = getenv("KRB5RCACHETYPE"))
95    return s;
96  else
97    return "dfl";
98 }
99
100 char * INTERFACE krb5_rc_default_name(context)
101     krb5_context context;
102 {
103  char *s;
104  if (s = getenv("KRB5RCACHENAME"))
105    return s;
106  else
107    return (char *) 0;
108 }
109
110 krb5_error_code INTERFACE
111 krb5_rc_default(context, id)
112     krb5_context context;
113     krb5_rcache *id;
114 {
115     krb5_error_code retval;
116
117     if (!(*id = (krb5_rcache )malloc(sizeof(**id))))
118         return KRB5_RC_MALLOC;
119
120     if (retval = krb5_rc_resolve_type(context, id, 
121                                       krb5_rc_default_type(context))) {
122         FREE(*id);
123         return retval;
124     }
125     if (retval = krb5_rc_resolve(context, *id, 
126                                  krb5_rc_default_name(context)))
127         FREE(*id);
128     return retval;
129 }
130
131
132 krb5_error_code INTERFACE krb5_rc_resolve_full(context, id, string_name)
133     krb5_context context;
134     krb5_rcache *id;
135     char *string_name;
136 {
137     char *type;
138     char *residual;
139     krb5_error_code retval;
140
141     if (!(residual = strchr(string_name,':')))
142         return KRB5_RC_PARSE;
143  
144     if (!(type = malloc(residual - string_name + 1)))
145         return KRB5_RC_MALLOC;
146     (void) strncpy(type,string_name,residual - string_name);
147     type[residual - string_name] = '\0';
148
149     if (!(*id = (krb5_rcache) malloc(sizeof(**id)))) {
150         FREE(type);
151         return KRB5_RC_MALLOC;
152     }
153
154     if (retval = krb5_rc_resolve_type(context, id,type)) {
155         FREE(type);
156         FREE(*id);
157         return retval;
158     }
159     FREE(type);
160     if (retval = krb5_rc_resolve(context, *id,residual + 1))
161         FREE(*id);
162     return retval;
163 }
164