summaryrefslogtreecommitdiff
path: root/src/libimcv/imv/imv_session_manager.c
blob: c9760299849b5dfa499e6756763342ff80c86345 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Copyright (C) 2014-2015 Andreas Steffen
 * HSR Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "imv_session_manager.h"

#include <tncif_names.h>
#include <tncif_identity.h>

#include <threading/mutex.h>

typedef struct private_imv_session_manager_t private_imv_session_manager_t;

/**
 * Private data of a imv_session_manager_t object.
 */
struct private_imv_session_manager_t {

	/**
	 * Public imv_session_manager_t interface.
	 */
	imv_session_manager_t public;

	/**
	 * Session list
	 */
	linked_list_t *sessions;

	/**
	 * mutex used to lock session list
	 */
	mutex_t *mutex;

};

METHOD(imv_session_manager_t, add_session, imv_session_t*,
	private_imv_session_manager_t *this, TNC_ConnectionID conn_id,
	linked_list_t *ar_identities)
{
	enumerator_t *enumerator;
	tncif_identity_t *tnc_id;
	imv_session_t *current, *session = NULL;
	time_t created;

	this->mutex->lock(this->mutex);

	/* check if a session has already been assigned */
	enumerator = this->sessions->create_enumerator(this->sessions);
	while (enumerator->enumerate(enumerator, &current))
	{
		if (conn_id == current->get_connection_id(current))
		{
			session = current;
			break;
		}
	}
	enumerator->destroy(enumerator);

	/* session already exists */
	if (session)
	{
		ar_identities->destroy_offset(ar_identities,
							   offsetof(tncif_identity_t, destroy));
		this->mutex->unlock(this->mutex);
		return session->get_ref(session);
	}

	/* Output list of Access Requestor identities */
	enumerator = ar_identities->create_enumerator(ar_identities);
	while (enumerator->enumerate(enumerator, &tnc_id))
	{
		pen_type_t id_type, subject_type, auth_type;
		uint32_t tcg_id_type, tcg_subject_type, tcg_auth_type;
		chunk_t id_value;

		id_type = tnc_id->get_identity_type(tnc_id);
		id_value = tnc_id->get_identity_value(tnc_id);
		subject_type = tnc_id->get_subject_type(tnc_id);
		auth_type = tnc_id->get_auth_type(tnc_id);

		tcg_id_type = (subject_type.vendor_id == PEN_TCG) ?
							id_type.type : TNC_SUBJECT_UNKNOWN;
		tcg_subject_type = (subject_type.vendor_id == PEN_TCG) ?
							subject_type.type : TNC_SUBJECT_UNKNOWN;
		tcg_auth_type =    (auth_type.vendor_id == PEN_TCG) ?
							auth_type.type : TNC_AUTH_UNKNOWN;

		DBG2(DBG_IMV, "  %N AR identity '%.*s' of type %N authenticated by %N",
			 TNC_Subject_names, tcg_subject_type,
			 id_value.len, id_value.ptr,
			 TNC_Identity_names, tcg_id_type,
			 TNC_Authentication_names, tcg_auth_type);
	}
	enumerator->destroy(enumerator);

	/* create a new session entry */
	created = time(NULL);
	session = imv_session_create(conn_id, created, ar_identities);
	this->sessions->insert_last(this->sessions, session);

	this->mutex->unlock(this->mutex);

	return session;
}

METHOD(imv_session_manager_t, remove_session, void,
	private_imv_session_manager_t *this, imv_session_t *session)
{
	enumerator_t *enumerator;
	imv_session_t *current;

	this->mutex->lock(this->mutex);
	enumerator = this->sessions->create_enumerator(this->sessions);
	while (enumerator->enumerate(enumerator, &current))
	{
		if (current == session)
		{
			this->sessions->remove_at(this->sessions, enumerator);
			break;
		}
	}
	enumerator->destroy(enumerator);
	this->mutex->unlock(this->mutex);
}

METHOD(imv_session_manager_t, destroy, void,
	private_imv_session_manager_t *this)
{
	this->sessions->destroy_offset(this->sessions,
							offsetof(imv_session_t, destroy));
	this->mutex->destroy(this->mutex);
	free(this);
}

/**
 * See header
 */
imv_session_manager_t *imv_session_manager_create(void)
{
	private_imv_session_manager_t *this;

	INIT(this,
		.public = {
			.add_session = _add_session,
			.remove_session = _remove_session,
			.destroy = _destroy,
		},
		.sessions = linked_list_create(),
		.mutex = mutex_create(MUTEX_TYPE_DEFAULT),
	);

	return &this->public;
}