interface_badvpn.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**
  2. * @file interface_badvpn.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * BadVPN interface backend.
  25. */
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <misc/cmdline.h>
  29. #include <system/BProcess.h>
  30. #include <ncd/NCDInterfaceModule.h>
  31. #include <ncd/NCDIfConfig.h>
  32. struct instance {
  33. NCDInterfaceModuleInst *i;
  34. BProcess process;
  35. int need_terminate;
  36. };
  37. static int build_cmdline (struct instance *o, CmdLine *c)
  38. {
  39. if (!CmdLine_Init(c)) {
  40. goto fail0;
  41. }
  42. // find exec statement
  43. struct NCDConfig_statements *exec_st = NCDConfig_find_statement(o->i->conf->statements, "badvpn.exec");
  44. if (!exec_st) {
  45. NCDInterfaceModuleInst_Backend_Log(o->i, BLOG_ERROR, "badvpn.exec missing");
  46. goto fail1;
  47. }
  48. // check arity
  49. char *exec_arg;
  50. if (!NCDConfig_statement_has_one_arg(exec_st, &exec_arg)) {
  51. NCDInterfaceModuleInst_Backend_Log(o->i, BLOG_ERROR, "badvpn.exec: wrong arity");
  52. goto fail1;
  53. }
  54. // append to cmdline
  55. if (!CmdLine_Append(c, exec_arg)) {
  56. goto fail1;
  57. }
  58. // append tapdev
  59. if (!CmdLine_Append(c, "--tapdev") || !CmdLine_Append(c, o->i->conf->name)) {
  60. goto fail1;
  61. }
  62. // iterate arg statements
  63. struct NCDConfig_statements *st = o->i->conf->statements;
  64. while (st = NCDConfig_find_statement(st, "badvpn.arg")) {
  65. // iterate arguments
  66. struct NCDConfig_strings *arg = st->args;
  67. while (arg) {
  68. if (!CmdLine_Append(c, arg->value)) {
  69. goto fail1;
  70. }
  71. arg = arg->next;
  72. }
  73. st = st->next;
  74. }
  75. // terminate
  76. if (!CmdLine_Finish(c)) {
  77. goto fail1;
  78. }
  79. return 1;
  80. fail1:
  81. CmdLine_Free(c);
  82. fail0:
  83. return 0;
  84. }
  85. static const char * read_user (struct instance *o)
  86. {
  87. // find statement
  88. struct NCDConfig_statements *user_st = NCDConfig_find_statement(o->i->conf->statements, "badvpn.user");
  89. if (!user_st) {
  90. NCDInterfaceModuleInst_Backend_Log(o->i, BLOG_ERROR, "badvpn.user missing");
  91. return NULL;
  92. }
  93. // check arity
  94. char *user_arg;
  95. if (!NCDConfig_statement_has_one_arg(user_st, &user_arg)) {
  96. NCDInterfaceModuleInst_Backend_Log(o->i, BLOG_ERROR, "badvpn.user: wrong arity");
  97. return NULL;
  98. }
  99. return user_arg;
  100. }
  101. static void process_handler (struct instance *o, int normally, uint8_t normally_exit_status)
  102. {
  103. NCDInterfaceModuleInst_Backend_Log(o->i, BLOG_INFO, "process terminated");
  104. // set not need terminate
  105. o->need_terminate = 0;
  106. // report error
  107. NCDInterfaceModuleInst_Backend_Error(o->i);
  108. return;
  109. }
  110. static void * func_new (NCDInterfaceModuleInst *i)
  111. {
  112. // allocate instance
  113. struct instance *o = malloc(sizeof(*o));
  114. if (!o) {
  115. NCDInterfaceModuleInst_Backend_Log(i, BLOG_ERROR, "failed to allocate instance");
  116. goto fail0;
  117. }
  118. // init arguments
  119. o->i = i;
  120. // create TAP device
  121. if (!NCDIfConfig_make_tuntap(o->i->conf->name, NULL, 0)) {
  122. NCDInterfaceModuleInst_Backend_Log(o->i, BLOG_ERROR, "failed to create TAP device");
  123. goto fail1;
  124. }
  125. // set device up
  126. if (!NCDIfConfig_set_up(o->i->conf->name)) {
  127. NCDInterfaceModuleInst_Backend_Log(o->i, BLOG_ERROR, "failed to set device up");
  128. goto fail2;
  129. }
  130. // read username
  131. const char *username = read_user(o);
  132. if (!username) {
  133. goto fail2;
  134. }
  135. // build cmdline
  136. CmdLine cl;
  137. if (!build_cmdline(o, &cl)) {
  138. NCDInterfaceModuleInst_Backend_Log(o->i, BLOG_ERROR, "failed to build cmdline");
  139. goto fail2;
  140. }
  141. // start process
  142. if (!BProcess_Init(&o->process, o->i->manager, (BProcess_handler)process_handler, o, ((char **)cl.arr.v)[0], (char **)cl.arr.v, username)) {
  143. NCDInterfaceModuleInst_Backend_Log(o->i, BLOG_ERROR, "BProcess_Init failed");
  144. CmdLine_Free(&cl);
  145. goto fail2;
  146. }
  147. CmdLine_Free(&cl);
  148. // set need terminate
  149. o->need_terminate = 1;
  150. // report up
  151. NCDInterfaceModuleInst_Backend_Event(o->i, NCDINTERFACEMODULE_EVENT_UP);
  152. return o;
  153. fail2:
  154. NCDIfConfig_remove_tuntap(o->i->conf->name, 0);
  155. fail1:
  156. free(o);
  157. fail0:
  158. return NULL;
  159. }
  160. static void func_free (void *vo)
  161. {
  162. struct instance *o = vo;
  163. // order process to terminate
  164. if (o->need_terminate) {
  165. BProcess_Terminate(&o->process);
  166. }
  167. // free process
  168. BProcess_Free(&o->process);
  169. // remove TAP device
  170. if (!NCDIfConfig_remove_tuntap(o->i->conf->name, 0)) {
  171. NCDInterfaceModuleInst_Backend_Log(o->i, BLOG_ERROR, "failed to remove TAP device");
  172. }
  173. // free instance
  174. free(o);
  175. }
  176. static void func_finish (void *vo)
  177. {
  178. struct instance *o = vo;
  179. ASSERT(o->need_terminate)
  180. // order process to terminate
  181. BProcess_Terminate(&o->process);
  182. // set not need terminate
  183. o->need_terminate = 0;
  184. }
  185. const struct NCDInterfaceModule ncd_interface_badvpn = {
  186. .type = "badvpn",
  187. .func_new = func_new,
  188. .func_free = func_free,
  189. .func_finish = func_finish
  190. };