Migrated auth_controller_t to INIT/METHOD macros.
[strongswan.git] / src / manager / manager.c
1 /*
2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "manager.h"
17
18 #include "gateway.h"
19
20 #include <utils/linked_list.h>
21
22 typedef struct private_manager_t private_manager_t;
23
24 /**
25 * private data of manager
26 */
27 struct private_manager_t {
28
29 /**
30 * public functions
31 */
32 manager_t public;
33
34 /**
35 * underlying storage database
36 */
37 storage_t *store;
38
39 /**
40 * user id, if we are logged in
41 */
42 int user;
43
44 /**
45 * selected gateway
46 */
47 gateway_t *gateway;
48 };
49
50 METHOD(manager_t, create_gateway_enumerator, enumerator_t*,
51 private_manager_t *this)
52 {
53 return this->store->create_gateway_enumerator(this->store, this->user);
54 }
55
56 METHOD(manager_t, select_gateway, gateway_t*,
57 private_manager_t *this, int select_id)
58 {
59 if (select_id != 0)
60 {
61 enumerator_t *enumerator;
62 int id, port;
63 char *name, *address;
64 host_t *host;
65
66 if (this->gateway) this->gateway->destroy(this->gateway);
67 this->gateway = NULL;
68
69 enumerator = this->store->create_gateway_enumerator(this->store, this->user);
70 while (enumerator->enumerate(enumerator, &id, &name, &port, &address))
71 {
72 if (select_id == id)
73 {
74 if (port == 0)
75 {
76 this->gateway = gateway_create_unix(name);
77 }
78 else
79 {
80 host = host_create_from_string(address, port);
81 if (host)
82 {
83 this->gateway = gateway_create_tcp(name, host);
84 }
85 }
86 break;
87 }
88 }
89 enumerator->destroy(enumerator);
90 }
91 return this->gateway;
92 }
93
94 METHOD(manager_t, logged_in, bool,
95 private_manager_t *this)
96 {
97 return this->user != 0;
98 }
99
100 METHOD(manager_t, login, bool,
101 private_manager_t *this, char *username, char *password)
102 {
103 if (!this->user)
104 {
105 this->user = this->store->login(this->store, username, password);
106 }
107 return this->user != 0;
108 }
109
110 METHOD(manager_t, logout, void,
111 private_manager_t *this)
112 {
113 if (this->gateway)
114 {
115 this->gateway->destroy(this->gateway);
116 this->gateway = NULL;
117 }
118 this->user = 0;
119 }
120
121 METHOD(context_t, destroy, void,
122 private_manager_t *this)
123 {
124 if (this->gateway) this->gateway->destroy(this->gateway);
125 free(this);
126 }
127
128 /*
129 * see header file
130 */
131 manager_t *manager_create(storage_t *storage)
132 {
133 private_manager_t *this;
134
135 INIT(this,
136 .public = {
137 .login = _login,
138 .logged_in = _logged_in,
139 .logout = _logout,
140 .create_gateway_enumerator = _create_gateway_enumerator,
141 .select_gateway = _select_gateway,
142 .context = {
143 .destroy = _destroy,
144 },
145 },
146 .store = storage,
147 );
148
149 return &this->public;
150 }
151