add winpcap 4.0.2 from url http://www.winpcap.org/
[natblaster.git] / winpcap / wpcap / libpcap / rpcapd / fileconf.c
blob5c0422cf687a0d955b3969e72fe59275fa78bdcb
1 /*
2 * Copyright (c) 1987, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
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.
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.
35 #include <stdio.h>
36 #include <string.h>
37 #include <signal.h>
38 #include <pcap.h> // for PCAP_ERRBUF_SIZE
39 #include "rpcapd.h"
40 #include "pcap-remote.h"
41 #include "sockutils.h" // for SOCK_ASSERT
44 extern char hostlist[MAX_HOST_LIST + 1]; //!< Keeps the list of the hosts that are allowed to connect to this server
45 extern struct active_pars activelist[MAX_ACTIVE_LIST]; //!< Keeps the list of the hosts (host, port) on which I want to connect to (active mode)
46 extern int nullAuthAllowed; //!< '1' if we permit NULL authentication, '0' otherwise
47 extern char loadfile[MAX_LINE + 1]; //!< Name of the file from which we have to load the configuration
51 int strrem(char *string, char chr);
55 void fileconf_read(int sign)
57 FILE *fp;
58 char msg[PCAP_ERRBUF_SIZE + 1];
59 int i;
61 #ifndef WIN32
62 signal(SIGHUP, fileconf_read);
63 #endif
65 if ((fp= fopen(loadfile, "r") ) != NULL)
67 char line[MAX_LINE + 1];
68 char *ptr;
70 hostlist[0]= 0;
71 i= 0;
73 while ( fgets(line, MAX_LINE, fp) != NULL )
75 if (line[0] == '\n') continue; // Blank line
76 if (line[0] == '\r') continue; // Blank line
77 if (line[0] == '#') continue; // Comment
79 if ( (ptr= strstr(line, "ActiveClient")) )
81 char *address, *port;
83 ptr= strchr(ptr, '=') + 1;
84 address= strtok(ptr, RPCAP_HOSTLIST_SEP);
86 if ( (address != NULL) && (i < MAX_ACTIVE_LIST) )
88 port = strtok(NULL, RPCAP_HOSTLIST_SEP);
89 snprintf(activelist[i].address, MAX_LINE, address);
91 if (strcmp(port, "DEFAULT") == 0) // the user choose a custom port
92 snprintf(activelist[i].port, MAX_LINE, RPCAP_DEFAULT_NETPORT_ACTIVE);
93 else
94 snprintf(activelist[i].port, MAX_LINE, port);
96 activelist[i].address[MAX_LINE] = 0;
97 activelist[i].port[MAX_LINE] = 0;
99 else
100 SOCK_ASSERT("Only MAX_ACTIVE_LIST active connections are currently supported.", 1);
102 i++;
103 continue;
106 if ( (ptr= strstr(line, "PassiveClient")) )
108 ptr= strchr(ptr, '=') + 1;
109 strncat(hostlist, ptr, MAX_HOST_LIST);
110 strncat(hostlist, ",", MAX_HOST_LIST);
111 continue;
114 if ( (ptr= strstr(line, "NullAuthPermit")) )
116 ptr= strstr(ptr, "YES");
117 if (ptr)
118 nullAuthAllowed= 1;
119 else
120 nullAuthAllowed= 0;
121 continue;
125 // clear the remaining fields of the active list
126 while (i < MAX_ACTIVE_LIST)
128 activelist[i].address[0] = 0;
129 activelist[i].port[0] = 0;
130 i++;
133 // Remove all '\n' and '\r' from the strings
134 strrem(hostlist, '\r');
135 strrem(hostlist, '\n');
137 snprintf(msg, PCAP_ERRBUF_SIZE, "New passive host list: %s\n\n", hostlist);
138 SOCK_ASSERT(msg, 1);
139 fclose(fp);
145 int fileconf_save(const char *savefile)
147 FILE *fp;
149 if ((fp= fopen(savefile, "w") ) != NULL)
151 char *token; /*, *port;*/ // temp, needed to separate items into the hostlist
152 char temphostlist[MAX_HOST_LIST + 1];
153 int i= 0;
155 fprintf(fp, "# Configuration file help.\n\n");
157 // Save list of clients which are allowed to connect to us in passive mode
158 fprintf(fp, "# Hosts which are allowed to connect to this server (passive mode)\n");
159 fprintf(fp, "# Format: PassiveClient = <name or address>\n\n");
161 strncpy(temphostlist, hostlist, MAX_HOST_LIST);
162 temphostlist[MAX_HOST_LIST]= 0;
164 token= strtok(temphostlist, RPCAP_HOSTLIST_SEP);
165 while( token != NULL )
167 fprintf(fp, "PassiveClient = %s\n", token);
168 token = strtok(NULL, RPCAP_HOSTLIST_SEP);
172 // Save list of clients which are allowed to connect to us in active mode
173 fprintf(fp, "\n\n");
174 fprintf(fp, "# Hosts to which this server is trying to connect to (active mode)\n");
175 fprintf(fp, "# Format: ActiveClient = <name or address>, <port | DEFAULT>\n\n");
178 while ( (activelist[i].address[0] != 0) && (i < MAX_ACTIVE_LIST) )
180 fprintf(fp, "ActiveClient = %s, %s\n", activelist[i].address, activelist[i].port);
181 i++;
184 // Save if we want to permit NULL authentication
185 fprintf(fp, "\n\n");
186 fprintf(fp, "# Permit NULL authentication: YES or NOT\n\n");
188 if (nullAuthAllowed)
189 fprintf(fp, "NullAuthPermit = YES\n");
190 else
191 fprintf(fp, "NullAuthPermit = NO\n");
193 fclose(fp);
194 return 0;
196 else
198 return -1;
205 int strrem(char *string, char chr)
207 char *pos;
208 int num= 0;
209 int len, i;
211 while ( (pos= strchr(string, chr) ) != NULL)
213 num++;
214 len= strlen(pos);
215 for (i=0; i<len; i++)
216 pos[i]= pos[i+1];
219 return num;