net_iptables.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. */
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <ncd/modules/command_template.h>
  31. #include <generated/blog_channel_ncd_net_iptables.h>
  32. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  33. #define IPTABLES_PATH "/sbin/iptables"
  34. static int build_cmdline (NCDModuleInst *i, int remove, char **exec, CmdLine *cl)
  35. {
  36. // read arguments
  37. NCDValue *table_arg;
  38. NCDValue *chain_arg;
  39. if (!NCDValue_ListReadHead(i->args, 2, &table_arg, &chain_arg)) {
  40. ModuleLog(i, BLOG_ERROR, "wrong arity");
  41. goto fail0;
  42. }
  43. if (NCDValue_Type(table_arg) != NCDVALUE_STRING || NCDValue_Type(chain_arg) != NCDVALUE_STRING) {
  44. ModuleLog(i, BLOG_ERROR, "wrong type");
  45. goto fail0;
  46. }
  47. char *table = NCDValue_StringValue(table_arg);
  48. char *chain = NCDValue_StringValue(chain_arg);
  49. // alloc exec
  50. if (!(*exec = strdup(IPTABLES_PATH))) {
  51. ModuleLog(i, BLOG_ERROR, "strdup failed");
  52. goto fail0;
  53. }
  54. // start cmdline
  55. if (!CmdLine_Init(cl)) {
  56. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  57. goto fail1;
  58. }
  59. // add header
  60. if (!CmdLine_Append(cl, IPTABLES_PATH) || !CmdLine_Append(cl, "-t") || !CmdLine_Append(cl, table) || !CmdLine_Append(cl, (remove ? "-D" : "-A")) || !CmdLine_Append(cl, chain)) {
  61. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  62. goto fail2;
  63. }
  64. // add additional arguments
  65. NCDValue *arg = NCDValue_ListNext(i->args, chain_arg);
  66. while (arg) {
  67. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  68. ModuleLog(i, BLOG_ERROR, "wrong type");
  69. goto fail2;
  70. }
  71. if (!CmdLine_Append(cl, NCDValue_StringValue(arg))) {
  72. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  73. goto fail2;
  74. }
  75. arg = NCDValue_ListNext(i->args, arg);
  76. }
  77. // finish
  78. if (!CmdLine_Finish(cl)) {
  79. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  80. goto fail2;
  81. }
  82. return 1;
  83. fail2:
  84. CmdLine_Free(cl);
  85. fail1:
  86. free(*exec);
  87. fail0:
  88. return 0;
  89. }
  90. static void * func_new (NCDModuleInst *i)
  91. {
  92. return command_template_new(i, build_cmdline, BLOG_CURRENT_CHANNEL);
  93. }
  94. static const struct NCDModule modules[] = {
  95. {
  96. .type = "net.iptables.append",
  97. .func_new = func_new,
  98. .func_free = command_template_func_free,
  99. .func_die = command_template_func_die
  100. }, {
  101. .type = NULL
  102. }
  103. };
  104. const struct NCDModuleGroup ncdmodule_net_iptables = {
  105. .modules = modules
  106. };