fileconf.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 1987, 1993, 1994
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgement:
  15. * This product includes software developed by the University of
  16. * California, Berkeley and its contributors.
  17. * 4. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #ifdef HAVE_CONFIG_H
  34. #include <config.h>
  35. #endif
  36. #include "ftmacros.h"
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <signal.h>
  40. #include <pcap.h> // for PCAP_ERRBUF_SIZE
  41. #include "sockutils.h" // for SOCK_DEBUG_MESSAGE
  42. #include "portability.h"
  43. #include "rpcapd.h"
  44. #include "config_params.h" // configuration file parameters
  45. #include "fileconf.h"
  46. #include "rpcap-protocol.h"
  47. static int strrem(char *string, char chr);
  48. void fileconf_read(void)
  49. {
  50. FILE *fp;
  51. char msg[PCAP_ERRBUF_SIZE + 1];
  52. int i;
  53. if ((fp = fopen(loadfile, "r")) != NULL)
  54. {
  55. char line[MAX_LINE + 1];
  56. char *ptr;
  57. hostlist[0] = 0;
  58. i = 0;
  59. while (fgets(line, MAX_LINE, fp) != NULL)
  60. {
  61. if (line[0] == '\n') continue; // Blank line
  62. if (line[0] == '\r') continue; // Blank line
  63. if (line[0] == '#') continue; // Comment
  64. ptr = strstr(line, "ActiveClient");
  65. if (ptr)
  66. {
  67. char *address, *port;
  68. char *lasts;
  69. ptr = strchr(ptr, '=') + 1;
  70. address = pcap_strtok_r(ptr, RPCAP_HOSTLIST_SEP, &lasts);
  71. if ((address != NULL) && (i < MAX_ACTIVE_LIST))
  72. {
  73. port = pcap_strtok_r(NULL, RPCAP_HOSTLIST_SEP, &lasts);
  74. strlcpy(activelist[i].address, address, MAX_LINE);
  75. if (strcmp(port, "DEFAULT") == 0) // the user choose a custom port
  76. strlcpy(activelist[i].port, RPCAP_DEFAULT_NETPORT_ACTIVE, MAX_LINE);
  77. else
  78. strlcpy(activelist[i].port, port, MAX_LINE);
  79. activelist[i].address[MAX_LINE] = 0;
  80. activelist[i].port[MAX_LINE] = 0;
  81. }
  82. else
  83. SOCK_DEBUG_MESSAGE("Only MAX_ACTIVE_LIST active connections are currently supported.");
  84. i++;
  85. continue;
  86. }
  87. ptr = strstr(line, "PassiveClient");
  88. if (ptr)
  89. {
  90. ptr = strchr(ptr, '=') + 1;
  91. strlcat(hostlist, ptr, MAX_HOST_LIST);
  92. strlcat(hostlist, ",", MAX_HOST_LIST);
  93. continue;
  94. }
  95. ptr = strstr(line, "NullAuthPermit");
  96. if (ptr)
  97. {
  98. ptr = strstr(ptr, "YES");
  99. if (ptr)
  100. nullAuthAllowed = 1;
  101. else
  102. nullAuthAllowed = 0;
  103. continue;
  104. }
  105. }
  106. // clear the remaining fields of the active list
  107. while (i < MAX_ACTIVE_LIST)
  108. {
  109. activelist[i].address[0] = 0;
  110. activelist[i].port[0] = 0;
  111. i++;
  112. }
  113. // Remove all '\n' and '\r' from the strings
  114. strrem(hostlist, '\r');
  115. strrem(hostlist, '\n');
  116. pcap_snprintf(msg, PCAP_ERRBUF_SIZE, "New passive host list: %s\n\n", hostlist);
  117. SOCK_DEBUG_MESSAGE(msg);
  118. fclose(fp);
  119. }
  120. }
  121. int fileconf_save(const char *savefile)
  122. {
  123. FILE *fp;
  124. if ((fp = fopen(savefile, "w")) != NULL)
  125. {
  126. char *token; /*, *port;*/ // temp, needed to separate items into the hostlist
  127. char temphostlist[MAX_HOST_LIST + 1];
  128. int i = 0;
  129. char *lasts;
  130. fprintf(fp, "# Configuration file help.\n\n");
  131. // Save list of clients which are allowed to connect to us in passive mode
  132. fprintf(fp, "# Hosts which are allowed to connect to this server (passive mode)\n");
  133. fprintf(fp, "# Format: PassiveClient = <name or address>\n\n");
  134. strncpy(temphostlist, hostlist, MAX_HOST_LIST);
  135. temphostlist[MAX_HOST_LIST] = 0;
  136. token = pcap_strtok_r(temphostlist, RPCAP_HOSTLIST_SEP, &lasts);
  137. while(token != NULL)
  138. {
  139. fprintf(fp, "PassiveClient = %s\n", token);
  140. token = pcap_strtok_r(NULL, RPCAP_HOSTLIST_SEP, &lasts);
  141. }
  142. // Save list of clients which are allowed to connect to us in active mode
  143. fprintf(fp, "\n\n");
  144. fprintf(fp, "# Hosts to which this server is trying to connect to (active mode)\n");
  145. fprintf(fp, "# Format: ActiveClient = <name or address>, <port | DEFAULT>\n\n");
  146. while ((i < MAX_ACTIVE_LIST) && (activelist[i].address[0] != 0))
  147. {
  148. fprintf(fp, "ActiveClient = %s, %s\n", activelist[i].address, activelist[i].port);
  149. i++;
  150. }
  151. // Save if we want to permit NULL authentication
  152. fprintf(fp, "\n\n");
  153. fprintf(fp, "# Permit NULL authentication: YES or NOT\n\n");
  154. if (nullAuthAllowed)
  155. fprintf(fp, "NullAuthPermit = YES\n");
  156. else
  157. fprintf(fp, "NullAuthPermit = NO\n");
  158. fclose(fp);
  159. return 0;
  160. }
  161. else
  162. {
  163. return -1;
  164. }
  165. }
  166. static int strrem(char *string, char chr)
  167. {
  168. char *pos;
  169. int num = 0;
  170. int len, i;
  171. while ((pos = strchr(string, chr)) != NULL)
  172. {
  173. num++;
  174. len = strlen(pos);
  175. for (i = 0; i < len; i++)
  176. pos[i] = pos[i+1];
  177. }
  178. return num;
  179. }