srv_rcache.c (krb5_get_server_rcache):
[krb5.git] / src / lib / krb5 / krb / auth_con.c
1
2 #include "k5-int.h"
3 #include "auth_con.h"
4
5 static krb5_error_code
6 actx_copy_addr(context, inad, outad)
7     krb5_context        context;
8     const krb5_address  *inad;
9     krb5_address        **outad;
10 {
11     krb5_address *tmpad;
12
13     if (!(tmpad = (krb5_address *)malloc(sizeof(*tmpad))))
14         return ENOMEM;
15 #ifdef HAVE_C_STRUCTURE_ASSIGNMENT
16     *tmpad = *inad;
17 #else
18     memcpy(tmpad, inad, sizeof(krb5_address));
19 #endif
20     if (!(tmpad->contents = (krb5_octet *)malloc(inad->length))) {
21         krb5_xfree(tmpad);
22         return ENOMEM;
23     }
24     memcpy((char *)tmpad->contents, (char *)inad->contents, inad->length);
25     *outad = tmpad;
26     return 0;
27 }
28
29 KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
30 krb5_auth_con_init(context, auth_context)
31     krb5_context          context;
32     krb5_auth_context  FAR * auth_context;
33 {
34     *auth_context =
35             (krb5_auth_context)malloc(sizeof(struct _krb5_auth_context));
36     if (!*auth_context)
37             return ENOMEM;
38     
39     memset(*auth_context, 0, sizeof(struct _krb5_auth_context));
40
41     /* Default flags, do time not seq */
42     (*auth_context)->auth_context_flags = 
43             KRB5_AUTH_CONTEXT_DO_TIME |  KRB5_AUTH_CONN_INITIALIZED;
44
45     (*auth_context)->req_cksumtype = context->default_ap_req_sumtype;
46     (*auth_context)->safe_cksumtype = context->default_safe_sumtype;
47     (*auth_context)->magic = KV5M_AUTH_CONTEXT;
48     return 0;
49 }
50
51 KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
52 krb5_auth_con_free(context, auth_context)
53     krb5_context          context;
54     krb5_auth_context     auth_context;
55 {
56     if (auth_context->local_addr) 
57         krb5_free_address(context, auth_context->local_addr);
58     if (auth_context->remote_addr) 
59         krb5_free_address(context, auth_context->remote_addr);
60     if (auth_context->local_port) 
61         krb5_free_address(context, auth_context->local_port);
62     if (auth_context->remote_port) 
63         krb5_free_address(context, auth_context->remote_port);
64     if (auth_context->authentp) 
65         krb5_free_authenticator(context, auth_context->authentp);
66     if (auth_context->keyblock) 
67         krb5_free_keyblock(context, auth_context->keyblock);
68     if (auth_context->local_subkey) 
69         krb5_free_keyblock(context, auth_context->local_subkey);
70     if (auth_context->remote_subkey) 
71         krb5_free_keyblock(context, auth_context->remote_subkey);
72     if (auth_context->rcache)
73         krb5_rc_close(context, auth_context->rcache);
74     free(auth_context);
75     return 0;
76 }
77
78 krb5_error_code
79 krb5_auth_con_setaddrs(context, auth_context, local_addr, remote_addr)
80     krb5_context          context;
81     krb5_auth_context     auth_context;
82     krb5_address        * local_addr;
83     krb5_address        * remote_addr;
84 {
85     krb5_error_code     retval;
86
87     /* Free old addresses */
88     if (auth_context->local_addr)
89         (void) krb5_free_address(context, auth_context->local_addr);
90     if (auth_context->remote_addr)
91         (void) krb5_free_address(context, auth_context->remote_addr);
92
93     retval = 0;
94     if (local_addr)
95         retval = actx_copy_addr(context,
96                                 local_addr,
97                                 &auth_context->local_addr);
98     else
99         auth_context->local_addr = NULL;
100
101     if (!retval && remote_addr)
102         retval = actx_copy_addr(context,
103                                 remote_addr,
104                                 &auth_context->remote_addr);
105     else
106         auth_context->remote_addr = NULL;
107
108     return retval;
109 }
110
111 krb5_error_code
112 krb5_auth_con_getaddrs(context, auth_context, local_addr, remote_addr)
113     krb5_context          context;
114     krb5_auth_context     auth_context;
115     krb5_address       ** local_addr;
116     krb5_address       ** remote_addr;
117 {
118     krb5_error_code     retval;
119
120     retval = 0;
121     if (local_addr && auth_context->local_addr) {
122         retval = actx_copy_addr(context,
123                                 auth_context->local_addr,
124                                 local_addr);
125     }
126     if (!retval && (remote_addr) && auth_context->remote_addr) {
127         retval = actx_copy_addr(context,
128                                 auth_context->remote_addr,
129                                 remote_addr);
130     }
131     return retval;
132 }
133
134 krb5_error_code
135 krb5_auth_con_setports(context, auth_context, local_port, remote_port)
136     krb5_context          context;
137     krb5_auth_context     auth_context;
138     krb5_address        * local_port;
139     krb5_address        * remote_port;
140 {
141     krb5_error_code     retval;
142
143     /* Free old addresses */
144     if (auth_context->local_port)
145         (void) krb5_free_address(context, auth_context->local_port);
146     if (auth_context->remote_port)
147         (void) krb5_free_address(context, auth_context->remote_port);
148
149     retval = 0;
150     if (local_port)
151         retval = actx_copy_addr(context,
152                                 local_port,
153                                 &auth_context->local_port);
154     else
155         auth_context->local_port = NULL;
156
157     if (!retval && remote_port)
158         retval = actx_copy_addr(context,
159                                 remote_port,
160                                 &auth_context->remote_port);
161     else
162         auth_context->remote_port = NULL;
163
164     return retval;
165 }
166
167
168 /*
169  * This function overloads the keyblock field. It is only useful prior to
170  * a krb5_rd_req_decode() call for user to user authentication where the
171  * server has the key and needs to use it to decrypt the incoming request.
172  * Once decrypted this key is no longer necessary and is then overwritten
173  * with the session key sent by the client.
174  */
175 KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
176 krb5_auth_con_setuseruserkey(context, auth_context, keyblock)
177     krb5_context          context;
178     krb5_auth_context     auth_context;
179     krb5_keyblock   FAR * keyblock;             
180 {
181     if (auth_context->keyblock)
182         krb5_free_keyblock(context, auth_context->keyblock);
183     return(krb5_copy_keyblock(context, keyblock, &(auth_context->keyblock)));
184 }
185
186 KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
187 krb5_auth_con_getkey(context, auth_context, keyblock)
188     krb5_context          context;
189     krb5_auth_context     auth_context;
190     krb5_keyblock      ** keyblock;             
191 {
192     if (auth_context->keyblock)
193         return krb5_copy_keyblock(context, auth_context->keyblock, keyblock);
194     *keyblock = NULL;
195     return 0;
196 }
197
198 KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
199 krb5_auth_con_getlocalsubkey(context, auth_context, keyblock)
200     krb5_context          context;
201     krb5_auth_context     auth_context;
202     krb5_keyblock FAR * FAR * keyblock;         
203 {
204     if (auth_context->local_subkey)
205         return krb5_copy_keyblock(context,auth_context->local_subkey,keyblock);
206     *keyblock = NULL;
207     return 0;
208 }
209
210 KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
211 krb5_auth_con_getremotesubkey(context, auth_context, keyblock)
212     krb5_context          context;
213     krb5_auth_context     auth_context;
214     krb5_keyblock FAR * FAR * keyblock;         
215 {
216     if (auth_context->remote_subkey)
217         return krb5_copy_keyblock(context,auth_context->remote_subkey,keyblock);
218     *keyblock = NULL;
219     return 0;
220 }
221
222 KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
223 krb5_auth_con_set_req_cksumtype(context, auth_context, cksumtype)
224     krb5_context          context;
225     krb5_auth_context     auth_context;
226     krb5_cksumtype        cksumtype;            
227 {
228     auth_context->req_cksumtype = cksumtype;
229     return 0;
230 }
231
232 krb5_error_code
233 krb5_auth_con_set_safe_cksumtype(context, auth_context, cksumtype)
234     krb5_context          context;
235     krb5_auth_context     auth_context;
236     krb5_cksumtype        cksumtype;            
237 {
238     auth_context->safe_cksumtype = cksumtype;
239     return 0;
240 }
241
242 KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
243 krb5_auth_con_getlocalseqnumber(context, auth_context, seqnumber)
244     krb5_context          context;
245     krb5_auth_context     auth_context;
246     krb5_int32          * seqnumber;            
247 {
248     *seqnumber = auth_context->local_seq_number;
249     return 0;
250 }
251
252 KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
253 krb5_auth_con_getauthenticator(context, auth_context, authenticator)
254     krb5_context          context;
255     krb5_auth_context     auth_context;
256     krb5_authenticator FAR * FAR * authenticator;               
257 {
258     return (krb5_copy_authenticator(context, auth_context->authentp,
259                                     authenticator));
260 }
261
262 KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
263 krb5_auth_con_getremoteseqnumber(context, auth_context, seqnumber)
264     krb5_context          context;
265     krb5_auth_context     auth_context;
266     krb5_int32      FAR * seqnumber;            
267 {
268     *seqnumber = auth_context->remote_seq_number;
269     return 0;
270 }
271
272 krb5_error_code
273 krb5_auth_con_initivector(context, auth_context)
274     krb5_context          context;
275     krb5_auth_context     auth_context;
276 {
277     if (auth_context->keyblock) {
278         int size = krb5_enctype_array[auth_context->keyblock->enctype]->
279                       system->block_length;
280
281         if ((auth_context->i_vector = (krb5_pointer)malloc(size))) {
282             memset(auth_context->i_vector, 0, size);
283             return 0;
284         }
285         return ENOMEM;
286     }
287     return EINVAL; /* XXX need an error for no keyblock */
288 }
289
290 krb5_error_code
291 krb5_auth_con_setivector(context, auth_context, ivector)
292     krb5_context          context;
293     krb5_auth_context     auth_context;
294     krb5_pointer          ivector;
295 {
296     auth_context->i_vector = ivector;
297     return 0;
298 }
299
300 krb5_error_code
301 krb5_auth_con_getivector(context, auth_context, ivector)
302     krb5_context          context;
303     krb5_auth_context     auth_context;
304     krb5_pointer        * ivector;
305 {
306     *ivector = auth_context->i_vector;
307     return 0;
308 }
309
310 KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
311 krb5_auth_con_setflags(context, auth_context, flags)
312     krb5_context          context;
313     krb5_auth_context     auth_context;
314     krb5_int32            flags;
315 {
316     auth_context->auth_context_flags = flags;
317     return 0;
318 }
319
320 KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
321 krb5_auth_con_getflags(context, auth_context, flags)
322     krb5_context          context;
323     krb5_auth_context     auth_context;
324     krb5_int32      FAR * flags;
325 {
326     *flags = auth_context->auth_context_flags;
327     return 0;
328 }
329
330 KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
331 krb5_auth_con_setrcache(context, auth_context, rcache)
332     krb5_context          context;
333     krb5_auth_context     auth_context;
334     krb5_rcache           rcache;
335 {
336     auth_context->rcache = rcache;
337     return 0;
338 }
339     
340 krb5_error_code
341 krb5_auth_con_getrcache(context, auth_context, rcache)
342     krb5_context          context;
343     krb5_auth_context     auth_context;
344     krb5_rcache         * rcache;
345 {
346     *rcache = auth_context->rcache;
347     return 0;
348 }
349