]> git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libimcv/imv/imv_session_manager.c
Update copyright headers after acquisition by secunet
[thirdparty/strongswan.git] / src / libimcv / imv / imv_session_manager.c
1 /*
2 * Copyright (C) 2014-2015 Andreas Steffen
3 *
4 * Copyright (C) secunet Security Networks AG
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "imv_session_manager.h"
18
19 #include <tncif_names.h>
20 #include <tncif_identity.h>
21
22 #include <threading/mutex.h>
23
24 typedef struct private_imv_session_manager_t private_imv_session_manager_t;
25
26 /**
27 * Private data of a imv_session_manager_t object.
28 */
29 struct private_imv_session_manager_t {
30
31 /**
32 * Public imv_session_manager_t interface.
33 */
34 imv_session_manager_t public;
35
36 /**
37 * Session list
38 */
39 linked_list_t *sessions;
40
41 /**
42 * mutex used to lock session list
43 */
44 mutex_t *mutex;
45
46 };
47
48 METHOD(imv_session_manager_t, add_session, imv_session_t*,
49 private_imv_session_manager_t *this, TNC_ConnectionID conn_id,
50 linked_list_t *ar_identities)
51 {
52 enumerator_t *enumerator;
53 tncif_identity_t *tnc_id;
54 imv_session_t *current, *session = NULL;
55
56 this->mutex->lock(this->mutex);
57
58 /* check if a session has already been assigned */
59 enumerator = this->sessions->create_enumerator(this->sessions);
60 while (enumerator->enumerate(enumerator, &current))
61 {
62 if (conn_id == current->get_connection_id(current))
63 {
64 session = current;
65 break;
66 }
67 }
68 enumerator->destroy(enumerator);
69
70 /* session already exists */
71 if (session)
72 {
73 ar_identities->destroy_offset(ar_identities,
74 offsetof(tncif_identity_t, destroy));
75 this->mutex->unlock(this->mutex);
76 return session->get_ref(session);
77 }
78
79 /* Output list of Access Requestor identities */
80 enumerator = ar_identities->create_enumerator(ar_identities);
81 while (enumerator->enumerate(enumerator, &tnc_id))
82 {
83 pen_type_t id_type, subject_type, auth_type;
84 uint32_t tcg_id_type, tcg_subject_type, tcg_auth_type;
85 chunk_t id_value;
86
87 id_type = tnc_id->get_identity_type(tnc_id);
88 id_value = tnc_id->get_identity_value(tnc_id);
89 subject_type = tnc_id->get_subject_type(tnc_id);
90 auth_type = tnc_id->get_auth_type(tnc_id);
91
92 tcg_id_type = (subject_type.vendor_id == PEN_TCG) ?
93 id_type.type : TNC_SUBJECT_UNKNOWN;
94 tcg_subject_type = (subject_type.vendor_id == PEN_TCG) ?
95 subject_type.type : TNC_SUBJECT_UNKNOWN;
96 tcg_auth_type = (auth_type.vendor_id == PEN_TCG) ?
97 auth_type.type : TNC_AUTH_UNKNOWN;
98
99 DBG2(DBG_IMV, " %N AR identity '%.*s' of type %N authenticated by %N",
100 TNC_Subject_names, tcg_subject_type,
101 id_value.len, id_value.ptr,
102 TNC_Identity_names, tcg_id_type,
103 TNC_Authentication_names, tcg_auth_type);
104 }
105 enumerator->destroy(enumerator);
106
107 /* create a new session entry */
108 session = imv_session_create(conn_id, ar_identities);
109 this->sessions->insert_last(this->sessions, session);
110
111 this->mutex->unlock(this->mutex);
112
113 return session;
114 }
115
116 METHOD(imv_session_manager_t, remove_session, void,
117 private_imv_session_manager_t *this, imv_session_t *session)
118 {
119 enumerator_t *enumerator;
120 imv_session_t *current;
121
122 this->mutex->lock(this->mutex);
123 enumerator = this->sessions->create_enumerator(this->sessions);
124 while (enumerator->enumerate(enumerator, &current))
125 {
126 if (current == session)
127 {
128 this->sessions->remove_at(this->sessions, enumerator);
129 break;
130 }
131 }
132 enumerator->destroy(enumerator);
133 this->mutex->unlock(this->mutex);
134 }
135
136 METHOD(imv_session_manager_t, destroy, void,
137 private_imv_session_manager_t *this)
138 {
139 this->sessions->destroy_offset(this->sessions,
140 offsetof(imv_session_t, destroy));
141 this->mutex->destroy(this->mutex);
142 free(this);
143 }
144
145 /**
146 * See header
147 */
148 imv_session_manager_t *imv_session_manager_create(void)
149 {
150 private_imv_session_manager_t *this;
151
152 INIT(this,
153 .public = {
154 .add_session = _add_session,
155 .remove_session = _remove_session,
156 .destroy = _destroy,
157 },
158 .sessions = linked_list_create(),
159 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
160 );
161
162 return &this->public;
163 }
164