]> The Tcpdump Group git mirrors - libpcap/blob - rpcapd/fileconf.c
Clean up the ether_hostton() stuff.
[libpcap] / rpcapd / fileconf.c
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
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include "ftmacros.h"
39
40 #include <stdio.h>
41 #include <string.h>
42 #include <signal.h>
43 #include <pcap.h> // for PCAP_ERRBUF_SIZE
44
45 #include "sockutils.h" // for SOCK_ASSERT
46 #include "portability.h"
47 #include "rpcapd.h"
48 #include "fileconf.h"
49 #include "rpcap-protocol.h"
50
51 static int strrem(char *string, char chr);
52
53 void fileconf_read(int sign)
54 {
55 FILE *fp;
56 char msg[PCAP_ERRBUF_SIZE + 1];
57 int i;
58
59 #ifndef _WIN32
60 signal(SIGHUP, fileconf_read);
61 #endif
62
63 if ((fp = fopen(loadfile, "r")) != NULL)
64 {
65 char line[MAX_LINE + 1];
66 char *ptr;
67
68 hostlist[0] = 0;
69 i = 0;
70
71 while (fgets(line, MAX_LINE, fp) != NULL)
72 {
73 if (line[0] == '\n') continue; // Blank line
74 if (line[0] == '\r') continue; // Blank line
75 if (line[0] == '#') continue; // Comment
76
77 if ((ptr = strstr(line, "ActiveClient")))
78 {
79 char *address, *port;
80 char *lasts;
81
82 ptr = strchr(ptr, '=') + 1;
83 address = pcap_strtok_r(ptr, RPCAP_HOSTLIST_SEP, &lasts);
84
85 if ((address != NULL) && (i < MAX_ACTIVE_LIST))
86 {
87 port = pcap_strtok_r(NULL, RPCAP_HOSTLIST_SEP, &lasts);
88 strlcpy(activelist[i].address, address, MAX_LINE);
89
90 if (strcmp(port, "DEFAULT") == 0) // the user choose a custom port
91 strlcpy(activelist[i].port, RPCAP_DEFAULT_NETPORT_ACTIVE, MAX_LINE);
92 else
93 strlcpy(activelist[i].port, port, MAX_LINE);
94
95 activelist[i].address[MAX_LINE] = 0;
96 activelist[i].port[MAX_LINE] = 0;
97 }
98 else
99 SOCK_ASSERT("Only MAX_ACTIVE_LIST active connections are currently supported.", 1);
100
101 i++;
102 continue;
103 }
104
105 if ((ptr = strstr(line, "PassiveClient")))
106 {
107 ptr = strchr(ptr, '=') + 1;
108 strlcat(hostlist, ptr, MAX_HOST_LIST);
109 strlcat(hostlist, ",", MAX_HOST_LIST);
110 continue;
111 }
112
113 if ((ptr = strstr(line, "NullAuthPermit")))
114 {
115 ptr = strstr(ptr, "YES");
116 if (ptr)
117 nullAuthAllowed = 1;
118 else
119 nullAuthAllowed = 0;
120 continue;
121 }
122 }
123
124 // clear the remaining fields of the active list
125 while (i < MAX_ACTIVE_LIST)
126 {
127 activelist[i].address[0] = 0;
128 activelist[i].port[0] = 0;
129 i++;
130 }
131
132 // Remove all '\n' and '\r' from the strings
133 strrem(hostlist, '\r');
134 strrem(hostlist, '\n');
135
136 pcap_snprintf(msg, PCAP_ERRBUF_SIZE, "New passive host list: %s\n\n", hostlist);
137 SOCK_ASSERT(msg, 1);
138 fclose(fp);
139 }
140 }
141
142 int fileconf_save(const char *savefile)
143 {
144 FILE *fp;
145
146 if ((fp = fopen(savefile, "w")) != NULL)
147 {
148 char *token; /*, *port;*/ // temp, needed to separate items into the hostlist
149 char temphostlist[MAX_HOST_LIST + 1];
150 int i = 0;
151 char *lasts;
152
153 fprintf(fp, "# Configuration file help.\n\n");
154
155 // Save list of clients which are allowed to connect to us in passive mode
156 fprintf(fp, "# Hosts which are allowed to connect to this server (passive mode)\n");
157 fprintf(fp, "# Format: PassiveClient = <name or address>\n\n");
158
159 strncpy(temphostlist, hostlist, MAX_HOST_LIST);
160 temphostlist[MAX_HOST_LIST] = 0;
161
162 token = pcap_strtok_r(temphostlist, RPCAP_HOSTLIST_SEP, &lasts);
163 while(token != NULL)
164 {
165 fprintf(fp, "PassiveClient = %s\n", token);
166 token = pcap_strtok_r(NULL, RPCAP_HOSTLIST_SEP, &lasts);
167 }
168
169
170 // Save list of clients which are allowed to connect to us in active mode
171 fprintf(fp, "\n\n");
172 fprintf(fp, "# Hosts to which this server is trying to connect to (active mode)\n");
173 fprintf(fp, "# Format: ActiveClient = <name or address>, <port | DEFAULT>\n\n");
174
175
176 while ((activelist[i].address[0] != 0) && (i < MAX_ACTIVE_LIST))
177 {
178 fprintf(fp, "ActiveClient = %s, %s\n", activelist[i].address, activelist[i].port);
179 i++;
180 }
181
182 // Save if we want to permit NULL authentication
183 fprintf(fp, "\n\n");
184 fprintf(fp, "# Permit NULL authentication: YES or NOT\n\n");
185
186 if (nullAuthAllowed)
187 fprintf(fp, "NullAuthPermit = YES\n");
188 else
189 fprintf(fp, "NullAuthPermit = NO\n");
190
191 fclose(fp);
192 return 0;
193 }
194 else
195 {
196 return -1;
197 }
198
199 }
200
201 static int strrem(char *string, char chr)
202 {
203 char *pos;
204 int num = 0;
205 int len, i;
206
207 while ((pos = strchr(string, chr)) != NULL)
208 {
209 num++;
210 len = strlen(pos);
211 for (i = 0; i < len; i++)
212 pos[i] = pos[i+1];
213 }
214
215 return num;
216 }