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