net_iptables.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /**
  2. * @file net_iptables.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. * iptables module.
  25. *
  26. * Synopsis: net.iptables.append(string table, string chain, string arg1, ...)
  27. * Synopsis: net.iptables.policy(string table, string chain, string target, string revert_target)
  28. */
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include <ncd/BEventLock.h>
  33. #include <ncd/modules/command_template.h>
  34. #include <generated/blog_channel_ncd_net_iptables.h>
  35. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  36. #define IPTABLES_PATH "/sbin/iptables"
  37. #define IPTABLES_PATH2 "/usr/sbin/iptables"
  38. static void template_free_func (void *vo, int is_error);
  39. BEventLock iptables_lock;
  40. struct instance {
  41. NCDModuleInst *i;
  42. command_template_instance cti;
  43. };
  44. static const char *find_iptables (NCDModuleInst *i)
  45. {
  46. if (access(IPTABLES_PATH, X_OK) == 0) {
  47. return IPTABLES_PATH;
  48. }
  49. if (access(IPTABLES_PATH2, X_OK) == 0) {
  50. return IPTABLES_PATH2;
  51. }
  52. ModuleLog(i, BLOG_ERROR, "failed to find iptables (tried "IPTABLES_PATH" and "IPTABLES_PATH2")");
  53. return NULL;
  54. }
  55. static int build_append_cmdline (NCDModuleInst *i, int remove, char **exec, CmdLine *cl)
  56. {
  57. // read arguments
  58. NCDValue *table_arg;
  59. NCDValue *chain_arg;
  60. if (!NCDValue_ListReadHead(i->args, 2, &table_arg, &chain_arg)) {
  61. ModuleLog(i, BLOG_ERROR, "wrong arity");
  62. goto fail0;
  63. }
  64. if (NCDValue_Type(table_arg) != NCDVALUE_STRING || NCDValue_Type(chain_arg) != NCDVALUE_STRING) {
  65. ModuleLog(i, BLOG_ERROR, "wrong type");
  66. goto fail0;
  67. }
  68. char *table = NCDValue_StringValue(table_arg);
  69. char *chain = NCDValue_StringValue(chain_arg);
  70. // find iptables
  71. const char *iptables_path = find_iptables(i);
  72. if (!iptables_path) {
  73. goto fail0;
  74. }
  75. // alloc exec
  76. if (!(*exec = strdup(iptables_path))) {
  77. ModuleLog(i, BLOG_ERROR, "strdup failed");
  78. goto fail0;
  79. }
  80. // start cmdline
  81. if (!CmdLine_Init(cl)) {
  82. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  83. goto fail1;
  84. }
  85. // add header
  86. if (!CmdLine_Append(cl, iptables_path) || !CmdLine_Append(cl, "-t") || !CmdLine_Append(cl, table) || !CmdLine_Append(cl, (remove ? "-D" : "-A")) || !CmdLine_Append(cl, chain)) {
  87. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  88. goto fail2;
  89. }
  90. // add additional arguments
  91. NCDValue *arg = NCDValue_ListNext(i->args, chain_arg);
  92. while (arg) {
  93. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  94. ModuleLog(i, BLOG_ERROR, "wrong type");
  95. goto fail2;
  96. }
  97. if (!CmdLine_Append(cl, NCDValue_StringValue(arg))) {
  98. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  99. goto fail2;
  100. }
  101. arg = NCDValue_ListNext(i->args, arg);
  102. }
  103. // finish
  104. if (!CmdLine_Finish(cl)) {
  105. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  106. goto fail2;
  107. }
  108. return 1;
  109. fail2:
  110. CmdLine_Free(cl);
  111. fail1:
  112. free(*exec);
  113. fail0:
  114. return 0;
  115. }
  116. static int build_policy_cmdline (NCDModuleInst *i, int remove, char **exec, CmdLine *cl)
  117. {
  118. // read arguments
  119. NCDValue *table_arg;
  120. NCDValue *chain_arg;
  121. NCDValue *target_arg;
  122. NCDValue *revert_target_arg;
  123. if (!NCDValue_ListRead(i->args, 4, &table_arg, &chain_arg, &target_arg, &revert_target_arg)) {
  124. ModuleLog(i, BLOG_ERROR, "wrong arity");
  125. goto fail0;
  126. }
  127. if (NCDValue_Type(table_arg) != NCDVALUE_STRING || NCDValue_Type(chain_arg) != NCDVALUE_STRING ||
  128. NCDValue_Type(target_arg) != NCDVALUE_STRING || NCDValue_Type(revert_target_arg) != NCDVALUE_STRING
  129. ) {
  130. ModuleLog(i, BLOG_ERROR, "wrong type");
  131. goto fail0;
  132. }
  133. char *table = NCDValue_StringValue(table_arg);
  134. char *chain = NCDValue_StringValue(chain_arg);
  135. char *target = NCDValue_StringValue(target_arg);
  136. char *revert_target = NCDValue_StringValue(revert_target_arg);
  137. // find iptables
  138. const char *iptables_path = find_iptables(i);
  139. if (!iptables_path) {
  140. goto fail0;
  141. }
  142. // alloc exec
  143. if (!(*exec = strdup(iptables_path))) {
  144. ModuleLog(i, BLOG_ERROR, "strdup failed");
  145. goto fail0;
  146. }
  147. // start cmdline
  148. if (!CmdLine_Init(cl)) {
  149. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  150. goto fail1;
  151. }
  152. // add arguments
  153. if (!CmdLine_Append(cl, iptables_path) || !CmdLine_Append(cl, "-t") || !CmdLine_Append(cl, table) ||
  154. !CmdLine_Append(cl, "-P") || !CmdLine_Append(cl, chain) || !CmdLine_Append(cl, (remove ? revert_target : target))) {
  155. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  156. goto fail2;
  157. }
  158. // finish
  159. if (!CmdLine_Finish(cl)) {
  160. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  161. goto fail2;
  162. }
  163. return 1;
  164. fail2:
  165. CmdLine_Free(cl);
  166. fail1:
  167. free(*exec);
  168. fail0:
  169. return 0;
  170. }
  171. static int func_globalinit (struct NCDModuleInitParams params)
  172. {
  173. // init iptables lock
  174. BEventLock_Init(&iptables_lock, BReactor_PendingGroup(params.reactor));
  175. return 1;
  176. }
  177. static void func_globalfree (void)
  178. {
  179. // free iptables lock
  180. BEventLock_Free(&iptables_lock);
  181. }
  182. static void func_new (NCDModuleInst *i, command_template_build_cmdline build_cmdline)
  183. {
  184. // allocate instance
  185. struct instance *o = malloc(sizeof(*o));
  186. if (!o) {
  187. BLog(BLOG_ERROR, "malloc failed");
  188. goto fail0;
  189. }
  190. NCDModuleInst_Backend_SetUser(i, o);
  191. // init arguments
  192. o->i = i;
  193. command_template_new(&o->cti, i, build_cmdline, template_free_func, o, BLOG_CURRENT_CHANNEL, &iptables_lock);
  194. return;
  195. fail0:
  196. NCDModuleInst_Backend_SetError(i);
  197. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  198. }
  199. void template_free_func (void *vo, int is_error)
  200. {
  201. struct instance *o = vo;
  202. NCDModuleInst *i = o->i;
  203. // free instance
  204. free(o);
  205. if (is_error) {
  206. NCDModuleInst_Backend_SetError(i);
  207. }
  208. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  209. }
  210. static void append_func_new (NCDModuleInst *i)
  211. {
  212. func_new(i, build_append_cmdline);
  213. }
  214. static void policy_func_new (NCDModuleInst *i)
  215. {
  216. func_new(i, build_policy_cmdline);
  217. }
  218. static void func_die (void *vo)
  219. {
  220. struct instance *o = vo;
  221. command_template_die(&o->cti);
  222. }
  223. static const struct NCDModule modules[] = {
  224. {
  225. .type = "net.iptables.append",
  226. .func_new = append_func_new,
  227. .func_die = func_die
  228. }, {
  229. .type = "net.iptables.policy",
  230. .func_new = policy_func_new,
  231. .func_die = func_die
  232. }, {
  233. .type = NULL
  234. }
  235. };
  236. const struct NCDModuleGroup ncdmodule_net_iptables = {
  237. .modules = modules,
  238. .func_globalinit = func_globalinit,
  239. .func_globalfree = func_globalfree
  240. };