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