net_iptables.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 <ncd/modules/command_template.h>
  32. #include <generated/blog_channel_ncd_net_iptables.h>
  33. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  34. #define IPTABLES_PATH "/sbin/iptables"
  35. static int build_append_cmdline (NCDModuleInst *i, int remove, char **exec, CmdLine *cl)
  36. {
  37. // read arguments
  38. NCDValue *table_arg;
  39. NCDValue *chain_arg;
  40. if (!NCDValue_ListReadHead(i->args, 2, &table_arg, &chain_arg)) {
  41. ModuleLog(i, BLOG_ERROR, "wrong arity");
  42. goto fail0;
  43. }
  44. if (NCDValue_Type(table_arg) != NCDVALUE_STRING || NCDValue_Type(chain_arg) != NCDVALUE_STRING) {
  45. ModuleLog(i, BLOG_ERROR, "wrong type");
  46. goto fail0;
  47. }
  48. char *table = NCDValue_StringValue(table_arg);
  49. char *chain = NCDValue_StringValue(chain_arg);
  50. // alloc exec
  51. if (!(*exec = strdup(IPTABLES_PATH))) {
  52. ModuleLog(i, BLOG_ERROR, "strdup failed");
  53. goto fail0;
  54. }
  55. // start cmdline
  56. if (!CmdLine_Init(cl)) {
  57. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  58. goto fail1;
  59. }
  60. // add header
  61. if (!CmdLine_Append(cl, IPTABLES_PATH) || !CmdLine_Append(cl, "-t") || !CmdLine_Append(cl, table) || !CmdLine_Append(cl, (remove ? "-D" : "-A")) || !CmdLine_Append(cl, chain)) {
  62. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  63. goto fail2;
  64. }
  65. // add additional arguments
  66. NCDValue *arg = NCDValue_ListNext(i->args, chain_arg);
  67. while (arg) {
  68. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  69. ModuleLog(i, BLOG_ERROR, "wrong type");
  70. goto fail2;
  71. }
  72. if (!CmdLine_Append(cl, NCDValue_StringValue(arg))) {
  73. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  74. goto fail2;
  75. }
  76. arg = NCDValue_ListNext(i->args, arg);
  77. }
  78. // finish
  79. if (!CmdLine_Finish(cl)) {
  80. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  81. goto fail2;
  82. }
  83. return 1;
  84. fail2:
  85. CmdLine_Free(cl);
  86. fail1:
  87. free(*exec);
  88. fail0:
  89. return 0;
  90. }
  91. static int build_policy_cmdline (NCDModuleInst *i, int remove, char **exec, CmdLine *cl)
  92. {
  93. // read arguments
  94. NCDValue *table_arg;
  95. NCDValue *chain_arg;
  96. NCDValue *target_arg;
  97. NCDValue *revert_target_arg;
  98. if (!NCDValue_ListRead(i->args, 4, &table_arg, &chain_arg, &target_arg, &revert_target_arg)) {
  99. ModuleLog(i, BLOG_ERROR, "wrong arity");
  100. goto fail0;
  101. }
  102. if (NCDValue_Type(table_arg) != NCDVALUE_STRING || NCDValue_Type(chain_arg) != NCDVALUE_STRING ||
  103. NCDValue_Type(target_arg) != NCDVALUE_STRING || NCDValue_Type(revert_target_arg) != NCDVALUE_STRING
  104. ) {
  105. ModuleLog(i, BLOG_ERROR, "wrong type");
  106. goto fail0;
  107. }
  108. char *table = NCDValue_StringValue(table_arg);
  109. char *chain = NCDValue_StringValue(chain_arg);
  110. char *target = NCDValue_StringValue(target_arg);
  111. char *revert_target = NCDValue_StringValue(revert_target_arg);
  112. // alloc exec
  113. if (!(*exec = strdup(IPTABLES_PATH))) {
  114. ModuleLog(i, BLOG_ERROR, "strdup failed");
  115. goto fail0;
  116. }
  117. // start cmdline
  118. if (!CmdLine_Init(cl)) {
  119. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  120. goto fail1;
  121. }
  122. // add arguments
  123. if (!CmdLine_Append(cl, IPTABLES_PATH) || !CmdLine_Append(cl, "-t") || !CmdLine_Append(cl, table) ||
  124. !CmdLine_Append(cl, "-P") || !CmdLine_Append(cl, chain) || !CmdLine_Append(cl, (remove ? revert_target : target))) {
  125. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  126. goto fail2;
  127. }
  128. // finish
  129. if (!CmdLine_Finish(cl)) {
  130. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  131. goto fail2;
  132. }
  133. return 1;
  134. fail2:
  135. CmdLine_Free(cl);
  136. fail1:
  137. free(*exec);
  138. fail0:
  139. return 0;
  140. }
  141. static void * append_func_new (NCDModuleInst *i)
  142. {
  143. return command_template_new(i, build_append_cmdline, BLOG_CURRENT_CHANNEL);
  144. }
  145. static void * policy_func_new (NCDModuleInst *i)
  146. {
  147. return command_template_new(i, build_policy_cmdline, BLOG_CURRENT_CHANNEL);
  148. }
  149. static const struct NCDModule modules[] = {
  150. {
  151. .type = "net.iptables.append",
  152. .func_new = append_func_new,
  153. .func_free = command_template_func_free,
  154. .func_die = command_template_func_die
  155. }, {
  156. .type = "net.iptables.policy",
  157. .func_new = policy_func_new,
  158. .func_free = command_template_func_free,
  159. .func_die = command_template_func_die
  160. }, {
  161. .type = NULL
  162. }
  163. };
  164. const struct NCDModuleGroup ncdmodule_net_iptables = {
  165. .modules = modules
  166. };