net_iptables.c 9.0 KB

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