fixed open failure debug message in load_secrets
[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 /**
51 * Implementation of manager_t.create_gateway_enumerator.
52 */
53 static enumerator_t* create_gateway_enumerator(private_manager_t *this)
54 {
55 return this->store->create_gateway_enumerator(this->store, this->user);
56 }
57
58 /**
59 * Implementation of manager_t.select_gateway.
60 */
61 static gateway_t* select_gateway(private_manager_t *this, int select_id)
62 {
63 if (select_id != 0)
64 {
65 enumerator_t *enumerator;
66 int id, port;
67 char *name, *address;
68 host_t *host;
69
70 if (this->gateway) this->gateway->destroy(this->gateway);
71 this->gateway = NULL;
72
73 enumerator = this->store->create_gateway_enumerator(this->store, this->user);
74 while (enumerator->enumerate(enumerator, &id, &name, &port, &address))
75 {
76 if (select_id == id)
77 {
78 if (port == 0)
79 {
80 this->gateway = gateway_create_unix(name);
81 }
82 else
83 {
84 host = host_create_from_string(address, port);
85 if (host)
86 {
87 this->gateway = gateway_create_tcp(name, host);
88 }
89 }
90 break;
91 }
92 }
93 enumerator->destroy(enumerator);
94 }
95 return this->gateway;
96 }
97
98 /**
99 * Implementation of manager_t.logged_in.
100 */
101 static bool logged_in(private_manager_t *this)
102 {
103 return this->user != 0;
104 }
105
106 /**
107 * Implementation of manager_t.login.
108 */
109 static bool login(private_manager_t *this, char *username, char *password)
110 {
111 if (!this->user)
112 {
113 this->user = this->store->login(this->store, username, password);
114 }
115 return this->user != 0;
116 }
117
118 /**
119 * Implementation of manager_t.logout.
120 */
121 static void logout(private_manager_t *this)
122 {
123 if (this->gateway)
124 {
125 this->gateway->destroy(this->gateway);
126 this->gateway = NULL;
127 }
128 this->user = 0;
129 }
130
131 /**
132 * Implementation of manager_t.destroy
133 */
134 static void destroy(private_manager_t *this)
135 {
136 if (this->gateway) this->gateway->destroy(this->gateway);
137 free(this);
138 }
139
140 /*
141 * see header file
142 */
143 manager_t *manager_create(storage_t *storage)
144 {
145 private_manager_t *this = malloc_thing(private_manager_t);
146
147 this->public.login = (bool(*)(manager_t*, char *username, char *password))login;
148 this->public.logged_in = (bool(*)(manager_t*))logged_in;
149 this->public.logout = (void(*)(manager_t*))logout;
150 this->public.create_gateway_enumerator = (enumerator_t*(*)(manager_t*))create_gateway_enumerator;
151 this->public.select_gateway = (gateway_t*(*)(manager_t*, int id))select_gateway;
152 this->public.context.destroy = (void(*)(context_t*))destroy;
153
154 this->user = 0;
155 this->store = storage;
156 this->gateway = NULL;
157
158 return &this->public;
159 }
160