… | |
… | |
28 | #include <openssl/rand.h> |
28 | #include <openssl/rand.h> |
29 | #include <openssl/evp.h> |
29 | #include <openssl/evp.h> |
30 | #include <openssl/rsa.h> |
30 | #include <openssl/rsa.h> |
31 | #include <openssl/err.h> |
31 | #include <openssl/err.h> |
32 | |
32 | |
33 | #include "gettext.h" |
|
|
34 | |
|
|
35 | #include "conf.h" |
33 | #include "conf.h" |
36 | #include "slog.h" |
34 | #include "slog.h" |
37 | #include "device.h" |
35 | #include "device.h" |
38 | #include "vpn.h" |
36 | #include "vpn.h" |
39 | #include "connection.h" |
37 | #include "connection.h" |
… | |
… | |
97 | struct rsa_cache : list<rsa_entry> |
95 | struct rsa_cache : list<rsa_entry> |
98 | { |
96 | { |
99 | void cleaner_cb (time_watcher &w); time_watcher cleaner; |
97 | void cleaner_cb (time_watcher &w); time_watcher cleaner; |
100 | |
98 | |
101 | bool find (const rsaid &id, rsachallenge &chg) |
99 | bool find (const rsaid &id, rsachallenge &chg) |
102 | { |
100 | { |
103 | for (iterator i = begin (); i != end (); ++i) |
101 | for (iterator i = begin (); i != end (); ++i) |
104 | { |
102 | { |
105 | if (!memcmp (&id, &i->id, sizeof id) && i->expire > NOW) |
103 | if (!memcmp (&id, &i->id, sizeof id) && i->expire > NOW) |
106 | { |
104 | { |
107 | memcpy (&chg, &i->chg, sizeof chg); |
105 | memcpy (&chg, &i->chg, sizeof chg); |
108 | |
106 | |
109 | erase (i); |
107 | erase (i); |
110 | return true; |
108 | return true; |
111 | } |
109 | } |
112 | } |
110 | } |
113 | |
111 | |
114 | if (cleaner.at < NOW) |
112 | if (cleaner.at < NOW) |
115 | cleaner.start (NOW + RSA_TTL); |
113 | cleaner.start (NOW + RSA_TTL); |
116 | |
114 | |
117 | return false; |
115 | return false; |
118 | } |
116 | } |
119 | |
117 | |
120 | void gen (rsaid &id, rsachallenge &chg) |
118 | void gen (rsaid &id, rsachallenge &chg) |
121 | { |
119 | { |
122 | rsa_entry e; |
120 | rsa_entry e; |
123 | |
121 | |
124 | RAND_bytes ((unsigned char *)&id, sizeof id); |
122 | RAND_bytes ((unsigned char *)&id, sizeof id); |
125 | RAND_bytes ((unsigned char *)&chg, sizeof chg); |
123 | RAND_bytes ((unsigned char *)&chg, sizeof chg); |
126 | |
124 | |
127 | e.expire = NOW + RSA_TTL; |
125 | e.expire = NOW + RSA_TTL; |
128 | e.id = id; |
126 | e.id = id; |
129 | memcpy (&e.chg, &chg, sizeof chg); |
127 | memcpy (&e.chg, &chg, sizeof chg); |
130 | |
128 | |
131 | push_back (e); |
129 | push_back (e); |
132 | |
130 | |
133 | if (cleaner.at < NOW) |
131 | if (cleaner.at < NOW) |
134 | cleaner.start (NOW + RSA_TTL); |
132 | cleaner.start (NOW + RSA_TTL); |
135 | } |
133 | } |
136 | |
134 | |
137 | rsa_cache () |
135 | rsa_cache () |
138 | : cleaner (this, &rsa_cache::cleaner_cb) |
136 | : cleaner (this, &rsa_cache::cleaner_cb) |
139 | { } |
137 | { } |
140 | |
138 | |
141 | } rsa_cache; |
139 | } rsa_cache; |
142 | |
140 | |
143 | void rsa_cache::cleaner_cb (time_watcher &w) |
141 | void rsa_cache::cleaner_cb (time_watcher &w) |
144 | { |
142 | { |
… | |
… | |
788 | { |
786 | { |
789 | slog (L_INFO, _("%s(%s): connection lost"), |
787 | slog (L_INFO, _("%s(%s): connection lost"), |
790 | conf->nodename, (const char *)si); |
788 | conf->nodename, (const char *)si); |
791 | |
789 | |
792 | if (::conf.script_node_down) |
790 | if (::conf.script_node_down) |
793 | run_script (run_script_cb (this, &connection::script_node_down), false); |
791 | if (!run_script (run_script_cb (this, &connection::script_node_down), false)) |
|
|
792 | slog (L_WARN, _("node-down command execution failed, continuing.")); |
794 | } |
793 | } |
795 | |
794 | |
796 | delete ictx; ictx = 0; |
795 | delete ictx; ictx = 0; |
797 | delete octx; octx = 0; |
796 | delete octx; octx = 0; |
798 | #if ENABLE_DNS |
797 | #if ENABLE_DNS |
… | |
… | |
1019 | slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), |
1018 | slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), |
1020 | conf->nodename, (const char *)rsi, |
1019 | conf->nodename, (const char *)rsi, |
1021 | p->prot_major, p->prot_minor); |
1020 | p->prot_major, p->prot_minor); |
1022 | |
1021 | |
1023 | if (::conf.script_node_up) |
1022 | if (::conf.script_node_up) |
1024 | run_script (run_script_cb (this, &connection::script_node_up), false); |
1023 | if (!run_script (run_script_cb (this, &connection::script_node_up), false)) |
|
|
1024 | slog (L_WARN, _("node-up command execution failed, continuing.")); |
1025 | |
1025 | |
1026 | break; |
1026 | break; |
1027 | } |
1027 | } |
1028 | else |
1028 | else |
1029 | slog (L_ERR, _("%s(%s): sent and received challenge do not match"), |
1029 | slog (L_ERR, _("%s(%s): sent and received challenge do not match"), |
… | |
… | |
1169 | send_vpn_packet (p, si); |
1169 | send_vpn_packet (p, si); |
1170 | |
1170 | |
1171 | delete p; |
1171 | delete p; |
1172 | } |
1172 | } |
1173 | |
1173 | |
1174 | void connection::script_node () |
1174 | void connection::script_init_env () |
1175 | { |
1175 | { |
1176 | vpn->script_if_up (); |
1176 | vpn->script_init_env (); |
1177 | |
1177 | |
1178 | char *env; |
1178 | char *env; |
1179 | asprintf (&env, "DESTID=%d", conf->id); putenv (env); |
1179 | asprintf (&env, "DESTID=%d", conf->id); putenv (env); |
1180 | asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); |
1180 | asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); |
1181 | asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); |
1181 | asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); |
1182 | asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); |
1182 | asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); |
1183 | } |
1183 | } |
1184 | |
1184 | |
1185 | const char *connection::script_node_up () |
1185 | const char *connection::script_node_up () |
1186 | { |
1186 | { |
1187 | script_node (); |
1187 | script_init_env (); |
1188 | |
1188 | |
1189 | putenv ("STATE=up"); |
1189 | putenv ("STATE=up"); |
1190 | |
1190 | |
|
|
1191 | char *filename; |
|
|
1192 | asprintf (&filename, |
|
|
1193 | "%s/%s", |
|
|
1194 | confbase, |
1191 | return ::conf.script_node_up ? ::conf.script_node_up : "node-up"; |
1195 | ::conf.script_node_up ? ::conf.script_node_up : "node-up"); |
1192 | } |
1196 | } |
1193 | |
1197 | |
1194 | const char *connection::script_node_down () |
1198 | const char *connection::script_node_down () |
1195 | { |
1199 | { |
1196 | script_node (); |
1200 | script_init_env (); |
1197 | |
1201 | |
1198 | putenv ("STATE=down"); |
1202 | putenv ("STATE=down"); |
1199 | |
1203 | |
1200 | return ::conf.script_node_up ? ::conf.script_node_down : "node-down"; |
1204 | char *filename; |
|
|
1205 | asprintf (&filename, |
|
|
1206 | "%s/%s", |
|
|
1207 | confbase, |
|
|
1208 | ::conf.script_node_down ? ::conf.script_node_down : "node-down"); |
1201 | } |
1209 | } |
1202 | |
1210 | |
1203 | connection::connection (struct vpn *vpn, conf_node *conf) |
1211 | connection::connection (struct vpn *vpn, conf_node *conf) |
1204 | : vpn(vpn), conf(conf) |
1212 | : vpn(vpn), conf(conf) |
1205 | , rekey (this, &connection::rekey_cb) |
1213 | , rekey (this, &connection::rekey_cb) |