1 /* $NetBSD: isakmp_xauth.c,v 1.11.6.1 2007/08/07 04:49:24 manu Exp $ */
3 /* Id: isakmp_xauth.c,v 1.38 2006/08/22 18:17:17 manubsd Exp */
6 * Copyright (C) 2004-2005 Emmanuel Dreyfus
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project 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 PROJECT 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 PROJECT 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
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/socket.h>
39 #include <sys/queue.h>
41 #include <netinet/in.h>
49 #if TIME_WITH_SYS_TIME
50 # include <sys/time.h>
54 # include <sys/time.h>
78 #include "crypto_openssl.h"
79 #include "isakmp_var.h"
83 #include "remoteconf.h"
84 #include "isakmp_inf.h"
85 #include "isakmp_xauth.h"
86 #include "isakmp_unity.h"
87 #include "isakmp_cfg.h"
89 #include "ipsec_doi.h"
90 #include "remoteconf.h"
91 #include "localconf.h"
92 #include "vpn_control.h"
93 #include "vpn_control_var.h"
97 phase1_handle_t
*iph1
;
100 struct isakmp_pl_attr
*attr
;
101 struct isakmp_data
*typeattr
;
102 struct isakmp_data
*usrattr
;
103 struct isakmp_data
*pwdattr
;
104 struct xauth_state
*xst
= &iph1
->mode_cfg
->xauth
;
108 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
110 "Xauth request while phase 1 is not completed\n");
114 if (xst
->status
!= XAUTHST_NOTYET
) {
116 "Xauth request whith Xauth state %d\n", xst
->status
);
120 plog(ASL_LEVEL_NOTICE
, "Sending Xauth request\n");
122 tlen
= sizeof(*attr
) +
123 + sizeof(*typeattr
) +
127 if ((buffer
= vmalloc(tlen
)) == NULL
) {
128 plog(ASL_LEVEL_ERR
, "Cannot allocate buffer\n");
132 attr
= (struct isakmp_pl_attr
*)buffer
->v
;
133 memset(attr
, 0, tlen
);
135 attr
->h
.len
= htons(tlen
);
136 attr
->type
= ISAKMP_CFG_REQUEST
;
137 attr
->id
= htons(eay_random());
139 typeattr
= (struct isakmp_data
*)(attr
+ 1);
140 typeattr
->type
= htons(XAUTH_TYPE
| ISAKMP_GEN_TV
);
141 typeattr
->lorv
= htons(XAUTH_TYPE_GENERIC
);
143 usrattr
= (struct isakmp_data
*)(typeattr
+ 1);
144 usrattr
->type
= htons(XAUTH_USER_NAME
| ISAKMP_GEN_TLV
);
145 usrattr
->lorv
= htons(0);
147 pwdattr
= (struct isakmp_data
*)(usrattr
+ 1);
148 pwdattr
->type
= htons(XAUTH_USER_PASSWORD
| ISAKMP_GEN_TLV
);
149 pwdattr
->lorv
= htons(0);
151 isakmp_cfg_send(iph1
, buffer
,
152 ISAKMP_NPTYPE_ATTR
, ISAKMP_FLAG_E
, 1, 0, NULL
);
156 xst
->status
= XAUTHST_REQSENT
;
162 xauth_attr_reply(iph1
, attr
, id
)
163 phase1_handle_t
*iph1
;
164 struct isakmp_data
*attr
;
167 char **outlet
= NULL
;
170 struct xauth_state
*xst
= &iph1
->mode_cfg
->xauth
;
172 if ((iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_XAUTH
) == 0) {
174 "Xauth reply but peer did not declare "
175 "itself as Xauth capable\n");
179 if (xst
->status
!= XAUTHST_REQSENT
) {
181 "Xauth reply while Xauth state is %d\n", xst
->status
);
185 type
= ntohs(attr
->type
) & ~ISAKMP_GEN_MASK
;
188 switch (ntohs(attr
->lorv
)) {
189 case XAUTH_TYPE_GENERIC
:
190 xst
->authtype
= XAUTH_TYPE_GENERIC
;
193 plog(ASL_LEVEL_WARNING
,
194 "Unexpected authentication type %d\n",
200 case XAUTH_USER_NAME
:
201 outlet
= &xst
->authdata
.generic
.usr
;
204 case XAUTH_USER_PASSWORD
:
205 outlet
= &xst
->authdata
.generic
.pwd
;
209 plog(ASL_LEVEL_WARNING
,
210 "ignored Xauth attribute %d\n", type
);
214 if (outlet
!= NULL
) {
215 alen
= ntohs(attr
->lorv
);
217 if ((*outlet
= racoon_realloc(*outlet
, alen
+ 1)) == NULL
) {
219 "Cannot allocate memory for Xauth Data\n");
223 memcpy(*outlet
, attr
+ 1, alen
);
224 (*outlet
)[alen
] = '\0';
229 if ((xst
->authdata
.generic
.usr
!= NULL
) &&
230 (xst
->authdata
.generic
.pwd
!= NULL
)) {
233 char *usr
= xst
->authdata
.generic
.usr
;
234 char *pwd
= xst
->authdata
.generic
.pwd
;
235 time_t throttle_delay
= 0;
237 #if 0 /* Real debug, don't do that at home */
238 plog(ASL_LEVEL_DEBUG
,
239 "Got username \"%s\", password \"%s\"\n", usr
, pwd
);
241 strlcpy(iph1
->mode_cfg
->login
, usr
, sizeof(iph1
->mode_cfg
->login
));
244 if ((port
= isakmp_cfg_getport(iph1
)) == -1) {
246 "Port pool depleted\n");
250 switch (isakmp_cfg_config
.authsource
) {
251 case ISAKMP_CFG_AUTH_SYSTEM
:
252 res
= xauth_login_system(usr
, pwd
);
257 "Unexpected authentication source\n");
263 * Optional group authentication
265 if (!res
&& (isakmp_cfg_config
.groupcount
))
266 res
= group_check(iph1
,
267 isakmp_cfg_config
.grouplist
,
268 isakmp_cfg_config
.groupcount
);
271 * On failure, throttle the connexion for the remote host
272 * in order to make password attacks more difficult.
274 throttle_delay
= throttle_host(iph1
->remote
, res
) - time(NULL
);
275 if (throttle_delay
> 0) {
278 str
= saddrwop2str((struct sockaddr
*)iph1
->remote
);
281 "Throttling in action for %s: delay %lds\n",
282 str
, (unsigned long)throttle_delay
);
289 if (throttle_delay
!= 0) {
290 struct xauth_reply_arg
*xra
;
292 if ((xra
= racoon_malloc(sizeof(*xra
))) == NULL
) {
294 "malloc failed, bypass throttling\n");
295 return xauth_reply(iph1
, port
, id
, res
);
299 * We need to store the ph1, but it might have
300 * disapeared when xauth_reply is called, so
301 * store the index instead.
303 xra
->index
= iph1
->index
;
307 sched_new(throttle_delay
, xauth_reply_stub
, xra
);
309 return xauth_reply(iph1
, port
, id
, res
);
317 xauth_reply_stub(args
)
320 struct xauth_reply_arg
*xra
= (struct xauth_reply_arg
*)args
;
321 phase1_handle_t
*iph1
;
323 if ((iph1
= ike_session_getph1byindex(NULL
, &xra
->index
)) != NULL
)
324 (void)xauth_reply(iph1
, xra
->port
, xra
->id
, xra
->res
);
327 "Delayed Xauth reply: phase 1 no longer exists.\n");
334 xauth_reply(iph1
, port
, id
, res
)
335 phase1_handle_t
*iph1
;
339 struct xauth_state
*xst
= &iph1
->mode_cfg
->xauth
;
340 char *usr
= xst
->authdata
.generic
.usr
;
342 if (iph1
->is_dying
) {
343 plog(ASL_LEVEL_NOTICE
,
344 "dropped login for user \"%s\"\n", usr
);
350 isakmp_cfg_putport(iph1
, port
);
352 plog(ASL_LEVEL_NOTICE
,
353 "login failed for user \"%s\"\n", usr
);
355 xauth_sendstatus(iph1
, XAUTH_STATUS_FAIL
, id
);
356 xst
->status
= XAUTHST_NOTYET
;
358 /* Delete Phase 1 SA */
359 if (FSM_STATE_IS_ESTABLISHED(iph1
->status
))
360 isakmp_info_send_d1(iph1
);
361 isakmp_ph1expire(iph1
);
366 xst
->status
= XAUTHST_OK
;
367 plog(ASL_LEVEL_NOTICE
,
368 "login succeeded for user \"%s\"\n", usr
);
370 xauth_sendstatus(iph1
, XAUTH_STATUS_OK
, id
);
376 xauth_sendstatus(iph1
, status
, id
)
377 phase1_handle_t
*iph1
;
382 struct isakmp_pl_attr
*attr
;
383 struct isakmp_data
*stattr
;
386 tlen
= sizeof(*attr
) +
389 if ((buffer
= vmalloc(tlen
)) == NULL
) {
390 plog(ASL_LEVEL_ERR
, "Cannot allocate buffer\n");
394 attr
= (struct isakmp_pl_attr
*)buffer
->v
;
395 memset(attr
, 0, tlen
);
397 attr
->h
.len
= htons(tlen
);
398 attr
->type
= ISAKMP_CFG_SET
;
399 attr
->id
= htons(id
);
401 stattr
= (struct isakmp_data
*)(attr
+ 1);
402 stattr
->type
= htons(XAUTH_STATUS
| ISAKMP_GEN_TV
);
403 stattr
->lorv
= htons(status
);
405 isakmp_cfg_send(iph1
, buffer
,
406 ISAKMP_NPTYPE_ATTR
, ISAKMP_FLAG_E
, 1, 0, NULL
);
415 xauth_login_system(usr
, pwd
)
425 if ((spw
= getspnam(usr
)) == NULL
)
428 syscryptpwd
= spw
->sp_pwdp
;
431 if ((pw
= getpwnam(usr
)) == NULL
)
434 #ifndef HAVE_SHADOW_H
435 syscryptpwd
= pw
->pw_passwd
;
438 /* No root login. Ever. */
442 if ((cryptpwd
= crypt(pwd
, syscryptpwd
)) == NULL
)
445 if (strcmp(cryptpwd
, syscryptpwd
) == 0)
452 xauth_group_system(usr
, grp
)
463 "the system group name \'%s\' is unknown\n",
468 while ((member
= gr
->gr_mem
[index
++])!=NULL
) {
469 if (!strcmp(member
,usr
)) {
470 plog(ASL_LEVEL_NOTICE
,
471 "membership validated\n");
481 phase1_handle_t
*iph1
;
483 struct xauth_state
*xst
= &iph1
->mode_cfg
->xauth
;
486 * Only the server side (edge device) really check for Xauth
487 * status. It does it if the chose authmethod is using Xauth.
488 * On the client side (roadwarrior), we don't check anything.
490 switch (AUTHMETHOD(iph1
)) {
491 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
492 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
493 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
494 /* The following are not yet implemented */
495 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
496 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
497 if ((iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_XAUTH
) == 0) {
499 "Hybrid auth negotiated but peer did not "
500 "announced as Xauth capable\n");
504 if (xst
->status
!= XAUTHST_OK
) {
506 "Hybrid auth negotiated but peer did not "
507 "succeed Xauth exchange\n");
522 group_check(iph1
, grp_list
, grp_count
)
523 phase1_handle_t
*iph1
;
531 /* check for presence of modecfg data */
533 if(iph1
->mode_cfg
== NULL
) {
535 "xauth group specified but modecfg not found\n");
539 /* loop through our group list */
541 for(; grp_index
< grp_count
; grp_index
++) {
543 /* check for presence of xauth data */
545 usr
= iph1
->mode_cfg
->xauth
.authdata
.generic
.usr
;
549 "xauth group specified but xauth not found\n");
553 /* call appropriate group validation funtion */
555 switch (isakmp_cfg_config
.groupsource
) {
557 case ISAKMP_CFG_GROUP_SYSTEM
:
558 res
= xauth_group_system(
560 grp_list
[grp_index
]);
564 /* we should never get here */
566 "Unknown group auth source\n");
571 plog(ASL_LEVEL_NOTICE
,
572 "user \"%s\" is a member of group \"%s\"\n",
574 grp_list
[grp_index
]);
577 plog(ASL_LEVEL_NOTICE
,
578 "user \"%s\" is not a member of group \"%s\"\n",
580 grp_list
[grp_index
]);
588 isakmp_xauth_req(iph1
, attr
)
589 phase1_handle_t
*iph1
;
590 struct isakmp_data
*attr
;
596 vchar_t
*buffer
= NULL
;
598 vchar_t
*mdata
= NULL
;
605 if ((iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_XAUTH
) == 0) {
607 "Xauth mode config request but peer "
608 "did not declare itself as Xauth capable\n");
612 type
= ntohs(attr
->type
) & ~ISAKMP_GEN_MASK
;
617 if ((ntohs(attr
->type
) & ISAKMP_GEN_TV
) == 0) {
619 "Unexpected long XAUTH_TYPE attribute\n");
622 if (ntohs(attr
->lorv
) != XAUTH_TYPE_GENERIC
) {
624 "Unsupported Xauth authentication %d\n",
630 value
= XAUTH_TYPE_GENERIC
;
633 case XAUTH_USER_NAME
:
634 if (!iph1
->rmconf
->xauth
|| !iph1
->rmconf
->xauth
->login
) {
635 plog(ASL_LEVEL_ERR
, "Xauth performed "
636 "with no login supplied\n");
640 dlen
= iph1
->rmconf
->xauth
->login
->l
- 1;
641 iph1
->rmconf
->xauth
->state
|= XAUTH_SENT_USERNAME
;
644 case XAUTH_USER_PASSWORD
:
646 if (!iph1
->rmconf
->xauth
|| !iph1
->rmconf
->xauth
->login
)
649 skip
= sizeof(struct ipsecdoi_id_b
);
650 usr
= vmalloc(iph1
->rmconf
->xauth
->login
->l
- 1 + skip
);
653 "Cannot allocate memory\n");
656 memset(usr
->v
, 0, skip
);
657 memcpy(usr
->v
+ skip
,
658 iph1
->rmconf
->xauth
->login
->v
,
659 iph1
->rmconf
->xauth
->login
->l
- 1);
661 if (iph1
->rmconf
->xauth
->pass
) {
662 /* A key given through racoonctl */
663 pwd
= iph1
->rmconf
->xauth
->pass
;
665 if ((pwd
= getpskbyname(usr
)) == NULL
) {
667 "No password was found for login %s\n",
668 iph1
->rmconf
->xauth
->login
->v
);
672 /* We have to free it before returning */
677 iph1
->rmconf
->xauth
->state
|= XAUTH_SENT_PASSWORD
;
683 if ((ntohs(attr
->type
) & ISAKMP_GEN_TV
) == 0) {
684 dlen
= ntohs(attr
->lorv
);
686 mraw
= (char*)(attr
+ 1);
687 if ((mdata
= vmalloc(dlen
)) == NULL
) {
689 "Cannot allocate memory\n");
692 memcpy(mdata
->v
, mraw
, mdata
->l
);
693 plog(ASL_LEVEL_NOTICE
, "XAUTH Message: '%s'.\n",
694 binsanitize(mdata
->v
, mdata
->l
));
700 plog(ASL_LEVEL_WARNING
,
701 "Ignored attribute %s\n", s_isakmp_cfg_type(type
));
706 if ((buffer
= vmalloc(sizeof(*attr
) + dlen
)) == NULL
) {
708 "Cannot allocate memory\n");
712 attr
= (struct isakmp_data
*)buffer
->v
;
714 attr
->type
= htons(type
| ISAKMP_GEN_TV
);
715 attr
->lorv
= htons(value
);
719 attr
->type
= htons(type
| ISAKMP_GEN_TLV
);
720 attr
->lorv
= htons(dlen
);
721 data
= (char *)(attr
+ 1);
724 case XAUTH_USER_NAME
:
726 * iph1->rmconf->xauth->login->v is valid,
727 * we just checked it in the previous switch case
729 memcpy(data
, iph1
->rmconf
->xauth
->login
->v
, dlen
);
731 case XAUTH_USER_PASSWORD
:
733 memcpy(data
, pwd
->v
, dlen
);
747 isakmp_xauth_set(iph1
, attr
)
748 phase1_handle_t
*iph1
;
749 struct isakmp_data
*attr
;
752 vchar_t
*buffer
= NULL
;
753 struct xauth_state
*xst
;
756 vchar_t
*mdata
= NULL
;
758 if ((iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_XAUTH
) == 0) {
760 "Xauth mode config set but peer "
761 "did not declare itself as Xauth capable\n");
765 type
= ntohs(attr
->type
) & ~ISAKMP_GEN_MASK
;
770 * We should only receive ISAKMP mode_cfg SET XAUTH_STATUS
771 * when running as a client (initiator).
773 xst
= &iph1
->mode_cfg
->xauth
;
774 switch(AUTHMETHOD(iph1
)) {
775 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
776 if (!iph1
->is_rekey
) {
778 "Unexpected XAUTH_STATUS_OK... not a Phase 1 rekey\n");
781 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
782 case FICTIVE_AUTH_METHOD_XAUTH_PSKEY_I
:
783 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
:
784 /* Not implemented ... */
785 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_I
:
786 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_I
:
790 "Unexpected XAUTH_STATUS_OK\n");
795 /* If we got a failure, delete iph1 */
796 if (ntohs(attr
->lorv
) != XAUTH_STATUS_OK
) {
798 "Xauth authentication failed\n");
800 vpncontrol_notify_ike_failed(VPNCTL_NTYPE_AUTHENTICATION_FAILED
, FROM_LOCAL
,
801 iph1_get_remote_v4_address(iph1
), 0, NULL
);
803 iph1
->mode_cfg
->flags
|= ISAKMP_CFG_DELETE_PH1
;
805 IPSECLOGASLMSG("IPSec Extended Authentication Failed.\n");
807 if (iph1
->is_rekey
) {
808 xst
->status
= XAUTHST_OK
;
811 IPSECLOGASLMSG("IPSec Extended Authentication Passed.\n");
815 /* We acknowledge it */
818 if ((ntohs(attr
->type
) & ISAKMP_GEN_TV
) == 0) {
819 dlen
= ntohs(attr
->lorv
);
821 mraw
= (char*)(attr
+ 1);
822 if ((mdata
= vmalloc(dlen
)) == NULL
) {
824 "Cannot allocate memory\n");
827 memcpy(mdata
->v
, mraw
, mdata
->l
);
828 plog(ASL_LEVEL_NOTICE
, "XAUTH Message: '%s'.\n",
829 binsanitize(mdata
->v
, mdata
->l
));
835 plog(ASL_LEVEL_WARNING
,
836 "Ignored attribute %s\n", s_isakmp_cfg_type(type
));
841 if ((buffer
= vmalloc(sizeof(*attr
))) == NULL
) {
843 "Cannot allocate memory\n");
847 attr
= (struct isakmp_data
*)buffer
->v
;
848 attr
->type
= htons(type
| ISAKMP_GEN_TV
);
849 attr
->lorv
= htons(0);
857 struct xauth_state
*xst
;
859 switch (xst
->authtype
) {
860 case XAUTH_TYPE_GENERIC
:
861 if (xst
->authdata
.generic
.usr
)
862 racoon_free(xst
->authdata
.generic
.usr
);
864 if (xst
->authdata
.generic
.pwd
)
865 racoon_free(xst
->authdata
.generic
.pwd
);
869 case XAUTH_TYPE_CHAP
:
871 case XAUTH_TYPE_SKEY
:
872 plog(ASL_LEVEL_WARNING
,
873 "Unsupported authtype %d\n", xst
->authtype
);
877 plog(ASL_LEVEL_WARNING
,
878 "Unexpected authtype %d\n", xst
->authtype
);
886 xauth_rmconf_used(xauth_rmconf
)
887 struct xauth_rmconf
**xauth_rmconf
;
889 if (*xauth_rmconf
== NULL
) {
890 *xauth_rmconf
= racoon_malloc(sizeof(**xauth_rmconf
));
891 if (*xauth_rmconf
== NULL
) {
893 "xauth_rmconf_used: malloc failed\n");
897 (*xauth_rmconf
)->login
= NULL
;
898 (*xauth_rmconf
)->pass
= NULL
;
899 (*xauth_rmconf
)->state
= 0;
901 if ((*xauth_rmconf
)->login
) {
902 vfree((*xauth_rmconf
)->login
);
903 (*xauth_rmconf
)->login
= NULL
;
905 if ((*xauth_rmconf
)->pass
!= NULL
) {
906 vfree((*xauth_rmconf
)->pass
);
907 (*xauth_rmconf
)->pass
= NULL
;
909 (*xauth_rmconf
)->state
= 0;
916 xauth_rmconf_delete(xauth_rmconf
)
917 struct xauth_rmconf
**xauth_rmconf
;
919 if (*xauth_rmconf
!= NULL
) {
920 if ((*xauth_rmconf
)->login
!= NULL
)
921 vfree((*xauth_rmconf
)->login
);
922 if ((*xauth_rmconf
)->pass
!= NULL
)
923 vfree((*xauth_rmconf
)->pass
);
925 racoon_free(*xauth_rmconf
);
926 *xauth_rmconf
= NULL
;