2 * Copyright (c) 1997 - 2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "krb5_locl.h"
36 krb5_error_code KRB5_LIB_FUNCTION
37 krb5_auth_con_init(krb5_context context
,
38 krb5_auth_context
*auth_context
)
44 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
47 memset(p
, 0, sizeof(*p
));
48 ALLOC(p
->authenticator
, 1);
49 if (!p
->authenticator
) {
50 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
54 memset (p
->authenticator
, 0, sizeof(*p
->authenticator
));
55 p
->flags
= KRB5_AUTH_CONTEXT_DO_TIME
;
57 p
->local_address
= NULL
;
58 p
->remote_address
= NULL
;
61 p
->keytype
= KEYTYPE_NULL
;
62 p
->cksumtype
= CKSUMTYPE_NONE
;
67 krb5_error_code KRB5_LIB_FUNCTION
68 krb5_auth_con_free(krb5_context context
,
69 krb5_auth_context auth_context
)
71 if (auth_context
!= NULL
) {
72 krb5_free_authenticator(context
, &auth_context
->authenticator
);
73 if(auth_context
->local_address
){
74 free_HostAddress(auth_context
->local_address
);
75 free(auth_context
->local_address
);
77 if(auth_context
->remote_address
){
78 free_HostAddress(auth_context
->remote_address
);
79 free(auth_context
->remote_address
);
81 krb5_free_keyblock(context
, auth_context
->keyblock
);
82 krb5_free_keyblock(context
, auth_context
->remote_subkey
);
83 krb5_free_keyblock(context
, auth_context
->local_subkey
);
89 krb5_error_code KRB5_LIB_FUNCTION
90 krb5_auth_con_setflags(krb5_context context
,
91 krb5_auth_context auth_context
,
94 auth_context
->flags
= flags
;
99 krb5_error_code KRB5_LIB_FUNCTION
100 krb5_auth_con_getflags(krb5_context context
,
101 krb5_auth_context auth_context
,
104 *flags
= auth_context
->flags
;
108 krb5_error_code KRB5_LIB_FUNCTION
109 krb5_auth_con_addflags(krb5_context context
,
110 krb5_auth_context auth_context
,
115 *flags
= auth_context
->flags
;
116 auth_context
->flags
|= addflags
;
120 krb5_error_code KRB5_LIB_FUNCTION
121 krb5_auth_con_removeflags(krb5_context context
,
122 krb5_auth_context auth_context
,
127 *flags
= auth_context
->flags
;
128 auth_context
->flags
&= ~removeflags
;
132 krb5_error_code KRB5_LIB_FUNCTION
133 krb5_auth_con_setaddrs(krb5_context context
,
134 krb5_auth_context auth_context
,
135 krb5_address
*local_addr
,
136 krb5_address
*remote_addr
)
139 if (auth_context
->local_address
)
140 krb5_free_address (context
, auth_context
->local_address
);
142 if ((auth_context
->local_address
= malloc(sizeof(krb5_address
))) == NULL
)
144 krb5_copy_address(context
, local_addr
, auth_context
->local_address
);
147 if (auth_context
->remote_address
)
148 krb5_free_address (context
, auth_context
->remote_address
);
150 if ((auth_context
->remote_address
= malloc(sizeof(krb5_address
))) == NULL
)
152 krb5_copy_address(context
, remote_addr
, auth_context
->remote_address
);
157 krb5_error_code KRB5_LIB_FUNCTION
158 krb5_auth_con_genaddrs(krb5_context context
,
159 krb5_auth_context auth_context
,
163 krb5_address local_k_address
, remote_k_address
;
164 krb5_address
*lptr
= NULL
, *rptr
= NULL
;
165 struct sockaddr_storage ss_local
, ss_remote
;
166 struct sockaddr
*local
= (struct sockaddr
*)&ss_local
;
167 struct sockaddr
*remote
= (struct sockaddr
*)&ss_remote
;
170 if(flags
& KRB5_AUTH_CONTEXT_GENERATE_LOCAL_ADDR
) {
171 if (auth_context
->local_address
== NULL
) {
172 len
= sizeof(ss_local
);
173 if(getsockname(fd
, local
, &len
) < 0) {
176 strerror_r(ret
, buf
, sizeof(buf
));
177 krb5_set_error_message(context
, ret
, "getsockname: %s", buf
);
180 ret
= krb5_sockaddr2address (context
, local
, &local_k_address
);
182 if(flags
& KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR
) {
183 krb5_sockaddr2port (context
, local
, &auth_context
->local_port
);
185 auth_context
->local_port
= 0;
186 lptr
= &local_k_address
;
189 if(flags
& KRB5_AUTH_CONTEXT_GENERATE_REMOTE_ADDR
) {
190 len
= sizeof(ss_remote
);
191 if(getpeername(fd
, remote
, &len
) < 0) {
194 strerror_r(ret
, buf
, sizeof(buf
));
195 krb5_set_error_message(context
, ret
, "getpeername: %s", buf
);
198 ret
= krb5_sockaddr2address (context
, remote
, &remote_k_address
);
200 if(flags
& KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR
) {
201 krb5_sockaddr2port (context
, remote
, &auth_context
->remote_port
);
203 auth_context
->remote_port
= 0;
204 rptr
= &remote_k_address
;
206 ret
= krb5_auth_con_setaddrs (context
,
212 krb5_free_address (context
, lptr
);
214 krb5_free_address (context
, rptr
);
219 krb5_error_code KRB5_LIB_FUNCTION
220 krb5_auth_con_setaddrs_from_fd (krb5_context context
,
221 krb5_auth_context auth_context
,
224 int fd
= *(int*)p_fd
;
226 if(auth_context
->local_address
== NULL
)
227 flags
|= KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR
;
228 if(auth_context
->remote_address
== NULL
)
229 flags
|= KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR
;
230 return krb5_auth_con_genaddrs(context
, auth_context
, fd
, flags
);
233 krb5_error_code KRB5_LIB_FUNCTION
234 krb5_auth_con_getaddrs(krb5_context context
,
235 krb5_auth_context auth_context
,
236 krb5_address
**local_addr
,
237 krb5_address
**remote_addr
)
240 krb5_free_address (context
, *local_addr
);
241 *local_addr
= malloc (sizeof(**local_addr
));
242 if (*local_addr
== NULL
) {
243 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
246 krb5_copy_address(context
,
247 auth_context
->local_address
,
251 krb5_free_address (context
, *remote_addr
);
252 *remote_addr
= malloc (sizeof(**remote_addr
));
253 if (*remote_addr
== NULL
) {
254 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
255 krb5_free_address (context
, *local_addr
);
259 krb5_copy_address(context
,
260 auth_context
->remote_address
,
265 static krb5_error_code
266 copy_key(krb5_context context
,
271 return krb5_copy_keyblock(context
, in
, out
);
272 *out
= NULL
; /* is this right? */
276 krb5_error_code KRB5_LIB_FUNCTION
277 krb5_auth_con_getkey(krb5_context context
,
278 krb5_auth_context auth_context
,
279 krb5_keyblock
**keyblock
)
281 return copy_key(context
, auth_context
->keyblock
, keyblock
);
284 krb5_error_code KRB5_LIB_FUNCTION
285 krb5_auth_con_getlocalsubkey(krb5_context context
,
286 krb5_auth_context auth_context
,
287 krb5_keyblock
**keyblock
)
289 return copy_key(context
, auth_context
->local_subkey
, keyblock
);
292 krb5_error_code KRB5_LIB_FUNCTION
293 krb5_auth_con_getremotesubkey(krb5_context context
,
294 krb5_auth_context auth_context
,
295 krb5_keyblock
**keyblock
)
297 return copy_key(context
, auth_context
->remote_subkey
, keyblock
);
300 krb5_error_code KRB5_LIB_FUNCTION
301 krb5_auth_con_setkey(krb5_context context
,
302 krb5_auth_context auth_context
,
303 krb5_keyblock
*keyblock
)
305 if(auth_context
->keyblock
)
306 krb5_free_keyblock(context
, auth_context
->keyblock
);
307 return copy_key(context
, keyblock
, &auth_context
->keyblock
);
310 krb5_error_code KRB5_LIB_FUNCTION
311 krb5_auth_con_setlocalsubkey(krb5_context context
,
312 krb5_auth_context auth_context
,
313 krb5_keyblock
*keyblock
)
315 if(auth_context
->local_subkey
)
316 krb5_free_keyblock(context
, auth_context
->local_subkey
);
317 return copy_key(context
, keyblock
, &auth_context
->local_subkey
);
320 krb5_error_code KRB5_LIB_FUNCTION
321 krb5_auth_con_generatelocalsubkey(krb5_context context
,
322 krb5_auth_context auth_context
,
326 krb5_keyblock
*subkey
;
328 ret
= krb5_generate_subkey_extended (context
, key
,
329 auth_context
->keytype
,
333 if(auth_context
->local_subkey
)
334 krb5_free_keyblock(context
, auth_context
->local_subkey
);
335 auth_context
->local_subkey
= subkey
;
340 krb5_error_code KRB5_LIB_FUNCTION
341 krb5_auth_con_setremotesubkey(krb5_context context
,
342 krb5_auth_context auth_context
,
343 krb5_keyblock
*keyblock
)
345 if(auth_context
->remote_subkey
)
346 krb5_free_keyblock(context
, auth_context
->remote_subkey
);
347 return copy_key(context
, keyblock
, &auth_context
->remote_subkey
);
350 krb5_error_code KRB5_LIB_FUNCTION
351 krb5_auth_con_setcksumtype(krb5_context context
,
352 krb5_auth_context auth_context
,
353 krb5_cksumtype cksumtype
)
355 auth_context
->cksumtype
= cksumtype
;
359 krb5_error_code KRB5_LIB_FUNCTION
360 krb5_auth_con_getcksumtype(krb5_context context
,
361 krb5_auth_context auth_context
,
362 krb5_cksumtype
*cksumtype
)
364 *cksumtype
= auth_context
->cksumtype
;
368 krb5_error_code KRB5_LIB_FUNCTION
369 krb5_auth_con_setkeytype (krb5_context context
,
370 krb5_auth_context auth_context
,
371 krb5_keytype keytype
)
373 auth_context
->keytype
= keytype
;
377 krb5_error_code KRB5_LIB_FUNCTION
378 krb5_auth_con_getkeytype (krb5_context context
,
379 krb5_auth_context auth_context
,
380 krb5_keytype
*keytype
)
382 *keytype
= auth_context
->keytype
;
387 krb5_error_code KRB5_LIB_FUNCTION
388 krb5_auth_con_setenctype(krb5_context context
,
389 krb5_auth_context auth_context
,
392 if(auth_context
->keyblock
)
393 krb5_free_keyblock(context
, auth_context
->keyblock
);
394 ALLOC(auth_context
->keyblock
, 1);
395 if(auth_context
->keyblock
== NULL
)
397 auth_context
->keyblock
->keytype
= etype
;
401 krb5_error_code KRB5_LIB_FUNCTION
402 krb5_auth_con_getenctype(krb5_context context
,
403 krb5_auth_context auth_context
,
406 krb5_abortx(context
, "unimplemented krb5_auth_getenctype called");
410 krb5_error_code KRB5_LIB_FUNCTION
411 krb5_auth_con_getlocalseqnumber(krb5_context context
,
412 krb5_auth_context auth_context
,
415 *seqnumber
= auth_context
->local_seqnumber
;
419 krb5_error_code KRB5_LIB_FUNCTION
420 krb5_auth_con_setlocalseqnumber (krb5_context context
,
421 krb5_auth_context auth_context
,
424 auth_context
->local_seqnumber
= seqnumber
;
428 krb5_error_code KRB5_LIB_FUNCTION
429 krb5_auth_getremoteseqnumber(krb5_context context
,
430 krb5_auth_context auth_context
,
433 *seqnumber
= auth_context
->remote_seqnumber
;
437 krb5_error_code KRB5_LIB_FUNCTION
438 krb5_auth_con_setremoteseqnumber (krb5_context context
,
439 krb5_auth_context auth_context
,
442 auth_context
->remote_seqnumber
= seqnumber
;
447 krb5_error_code KRB5_LIB_FUNCTION
448 krb5_auth_con_getauthenticator(krb5_context context
,
449 krb5_auth_context auth_context
,
450 krb5_authenticator
*authenticator
)
452 *authenticator
= malloc(sizeof(**authenticator
));
453 if (*authenticator
== NULL
) {
454 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
458 copy_Authenticator(auth_context
->authenticator
,
464 void KRB5_LIB_FUNCTION
465 krb5_free_authenticator(krb5_context context
,
466 krb5_authenticator
*authenticator
)
468 free_Authenticator (*authenticator
);
469 free (*authenticator
);
470 *authenticator
= NULL
;
474 krb5_error_code KRB5_LIB_FUNCTION
475 krb5_auth_con_setuserkey(krb5_context context
,
476 krb5_auth_context auth_context
,
477 krb5_keyblock
*keyblock
)
479 if(auth_context
->keyblock
)
480 krb5_free_keyblock(context
, auth_context
->keyblock
);
481 return krb5_copy_keyblock(context
, keyblock
, &auth_context
->keyblock
);
484 krb5_error_code KRB5_LIB_FUNCTION
485 krb5_auth_con_getrcache(krb5_context context
,
486 krb5_auth_context auth_context
,
489 *rcache
= auth_context
->rcache
;
493 krb5_error_code KRB5_LIB_FUNCTION
494 krb5_auth_con_setrcache(krb5_context context
,
495 krb5_auth_context auth_context
,
498 auth_context
->rcache
= rcache
;
502 #if 0 /* not implemented */
504 krb5_error_code KRB5_LIB_FUNCTION
505 krb5_auth_con_initivector(krb5_context context
,
506 krb5_auth_context auth_context
)
508 krb5_abortx(context
, "unimplemented krb5_auth_con_initivector called");
512 krb5_error_code KRB5_LIB_FUNCTION
513 krb5_auth_con_setivector(krb5_context context
,
514 krb5_auth_context auth_context
,
515 krb5_pointer ivector
)
517 krb5_abortx(context
, "unimplemented krb5_auth_con_setivector called");
520 #endif /* not implemented */