summaryrefslogtreecommitdiff
path: root/src/anylike.c
blob: b154c458590089b5491af45cd9cfc1e64ba7807e (plain) (blame)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 *  anylike
 *
 *  anylike is an IKEv2 Implementation written in Lua and C. It's main 
 *  design goal is to provide anytun and uanytun or any other SATP 
 *  implementation with a key exchange mechanism but it should also be 
 *  possible to use anylike as key exchange daemon for IPSec security 
 *  associations. The use of Lua guarantees that anylike is easily 
 *  portable to many platforms including very small ones like wireless 
 *  routers.
 *  
 *
 *  Copyright (C) 2009-2010 Markus Grueneis <gimpf@anylike.org>
 *                          Christian Pointner <equinox@anylike.org>
 *
 *  This file is part of anylike.
 *
 *  anylike 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 3 of the License, or
 *  any later version.
 *
 *  anylike 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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with anylike. If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

#include "datatypes.h"
#include "options.h"
#include "string_list.h"
#include "log.h"
#include "l_log.h"
#include "l_crypt.h"
#include "l_sig_handler.h"

#ifndef USE_OPENSSL
#include <gnutls/gnutls.h>
#endif

#include "daemon.h"

#ifndef USE_OPENSSL

#define MIN_GNUTLS_VERSION "2.6.4"

void anylike_gnutls_log_func(int level, const char* msg)
{
  log_printf(DEBUG, msg); // TODO: convert level to proper log priority
}

int init_gnutls()
{
  int ret = gnutls_global_init();
  if(ret != GNUTLS_E_SUCCESS) {
    log_printf(ERROR, "gnutls_global_init() returned with error 0x%04X", ret);
    return -1;
  }

  if(!gnutls_check_version(MIN_GNUTLS_VERSION)) {
    log_printf(NOTICE, "invalid Version of gnutls, should be >= %s but is %s", MIN_GNUTLS_VERSION, gnutls_check_version(NULL));
    gnutls_global_deinit();
    return -1;
  }

  gnutls_global_set_log_function(anylike_gnutls_log_func);
  gnutls_global_set_log_level(11);  // TODO: find a better level (this is very talky)

  log_printf(NOTICE, "gnutls init finished");
  return 0;
}
#endif

void cleanup_crypt()
{
#ifndef USE_OPENSSL
  gnutls_global_deinit();
#endif
}

#include "anylike_lua_bytecode.h"

#define LUA_MAIN_LOOP_FUNC "main_loop"

static const luaL_Reg anylike_lualibs[] = {
  {"", luaopen_base},
  {LUA_LOADLIBNAME, luaopen_package},
  {LUA_TABLIBNAME, luaopen_table},
  {LUA_STRLIBNAME, luaopen_string},
  {LUA_MATHLIBNAME, luaopen_math},
  {LUA_LOGLIBNAME, luaopen_log},
  {LUA_SIGNALLIBNAME, luaopen_signal},
  {LUA_CRYPTLIBNAME, luaopen_crypt},
  {NULL, NULL}
};

int init_main_loop(lua_State *L)
{
  const luaL_Reg *lib = anylike_lualibs;
  for (; lib->func; lib++) {
    lua_pushcfunction(L, lib->func);
    lua_pushstring(L, lib->name);
    lua_call(L, 1, 0);
  }

  int ret = luaL_loadbuffer(L, anylike_lua_bytecode, sizeof(anylike_lua_bytecode), "anylike");
  if(ret) {
    const char* err_str = luaL_checkstring(L, -1);
    switch(ret) {
    case LUA_ERRSYNTAX: log_printf(ERROR, "luaL_loadbuffer() syntax error: %s", err_str); break;
    case LUA_ERRMEM: log_printf(ERROR, "luaL_loadbuffer() malloc error: %s", err_str); break;
    default: log_printf(ERROR, "luaL_loadbuffer() unknown error: %s", err_str); break;
    }
    return -1;
  }

  ret = lua_pcall(L, 0, 0, 0);
  if(ret) {
    const char* err_str = luaL_checkstring(L, -1);
    switch(ret) {
    case LUA_ERRRUN: log_printf(ERROR, "lua_pcall() runtime error: %s", err_str); break;
    case LUA_ERRMEM: log_printf(ERROR, "lua_pcall() malloc error: %s", err_str); break;
    case LUA_ERRERR: log_printf(ERROR, "lua_pcall() error at error handler function: %s", err_str); break;
    }
    return -1;
  }

  return 0;
}

int call_main_loop(lua_State* L, options_t* opt)
{
  lua_getglobal(L, LUA_MAIN_LOOP_FUNC);
  if(!lua_isfunction(L, -1)) {
    log_printf(ERROR, "there is no function '%s' inside anylike bytecode", LUA_MAIN_LOOP_FUNC);
    return -1;
  };

  options_lua_push(opt, L);

  int ret = lua_pcall(L, 1, LUA_MULTRET, 0);
  if(ret) {
    const char* err_str = luaL_checkstring(L, -1);
    switch(ret) {
    case LUA_ERRRUN: log_printf(ERROR, "lua_pcall(%s) runtime error: %s", LUA_MAIN_LOOP_FUNC, err_str); break;
    case LUA_ERRMEM: log_printf(ERROR, "lua_pcall(%s) malloc error: %s", LUA_MAIN_LOOP_FUNC, err_str); break;
    case LUA_ERRERR: log_printf(ERROR, "lua_pcall(%s) error at error handler function: %s", LUA_MAIN_LOOP_FUNC, err_str); break;
    }
    return -1;
  }

  int n = lua_gettop(L);
  log_printf(DEBUG, "%s returned %d values", LUA_MAIN_LOOP_FUNC, n);
  int i;
  for (i = 1; i <= n; i++)
    log_printf(DEBUG, "return value [%d] = '%s'", i, luaL_checkstring(L, i));

  ret = lua_tointeger(L, 1);
  return ret;
}

int main_loop(options_t* opt)
{
  lua_State *L;
  L = luaL_newstate();
  if(!L) {
    log_printf(ERROR, "error creating lua state");
    return -1;
  }

  int ret = init_main_loop(L);
  if(!ret)
    ret = call_main_loop(L, opt);
  
  lua_close(L);
  return ret;
}

int main(int argc, char* argv[])
{
  log_init();

  options_t opt;
  int ret = options_parse(&opt, argc, argv);
  if(ret) {
    if(ret > 0) {
      fprintf(stderr, "syntax error near: %s\n\n", argv[ret]);
    }
    if(ret == -2) {
      fprintf(stderr, "memory error on options_parse, exitting\n");
    }
    if(ret == -3) {
      options_print_version();
    }

    if(ret != -2 && ret != -3) 
      options_print_usage();

    if(ret == -1 || ret == -3)
      ret = 0;

    options_clear(&opt);
    log_close();
    exit(ret);
  }
  string_list_element_t* tmp = opt.log_targets_.first_;
  if(!tmp) {
    log_add_target("syslog:3,anylike,daemon");
  }
  else {
    while(tmp) {
      ret = log_add_target(tmp->string_);
      if(ret) {
        switch(ret) {
        case -2: fprintf(stderr, "memory error on log_add_target, exitting\n"); break;
        case -3: fprintf(stderr, "unknown log target: '%s', exitting\n", tmp->string_); break;
        case -4: fprintf(stderr, "this log target is only allowed once: '%s', exitting\n", tmp->string_); break;
        default: fprintf(stderr, "syntax error near: '%s', exitting\n", tmp->string_); break;
        }
        
        options_clear(&opt);
        log_close();
        exit(ret);
      }
      tmp = tmp->next_;
    }
  }

  log_printf(NOTICE, "just started...");
  options_parse_post(&opt);

  priv_info_t priv;
  if(opt.username_)
    if(priv_init(&priv, opt.username_, opt.groupname_)) {
      options_clear(&opt);
      log_close();
      exit(-1);
    }

#ifndef USE_OPENSSL
  ret = init_gnutls();
  if(ret) {
    log_printf(ERROR, "error on gnutls initialization, exitting");
    options_clear(&opt);
    log_close();
    exit(ret);
  }
#endif

  FILE* pid_file = NULL;
  if(opt.pid_file_) {
    pid_file = fopen(opt.pid_file_, "w");
    if(!pid_file) {
      log_printf(WARNING, "unable to open pid file: %s", strerror(errno));
    }
  }

  if(opt.chroot_dir_)
    if(do_chroot(opt.chroot_dir_)) {
      cleanup_crypt();
      options_clear(&opt);
      log_close();
      exit(-1);
    }
  if(opt.username_)
    if(priv_drop(&priv)) {
      cleanup_crypt();
      options_clear(&opt);
      log_close();
      exit(-1);
    }  

  if(opt.daemonize_) {
    pid_t oldpid = getpid();
    daemonize();
    log_printf(INFO, "running in background now (old pid: %d)", oldpid);
  }

  if(pid_file) {
    pid_t pid = getpid();
    fprintf(pid_file, "%d", pid);
    fclose(pid_file);
  }

  ret = main_loop(&opt);

  cleanup_crypt();
  options_clear(&opt);

  if(!ret)
    log_printf(NOTICE, "normal shutdown");
  else if(ret < 0)
    log_printf(NOTICE, "shutdown after error");
  else
    log_printf(NOTICE, "shutdown after signal");

  log_close();

	return ret;
}