File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / miniupnpd / options.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue Feb 21 23:16:02 2012 UTC (12 years, 4 months ago) by misho
Branches: miniupnpd, elwix, MAIN
CVS tags: v1_6elwix, v1_5, HEAD
miniupnpd

    1: /* $Id: options.c,v 1.1.1.1 2012/02/21 23:16:02 misho Exp $ */
    2: /* MiniUPnP project
    3:  * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
    4:  * author: Ryan Wagoner
    5:  * (c) 2006 Thomas Bernard 
    6:  * This software is subject to the conditions detailed
    7:  * in the LICENCE file provided within the distribution */
    8: 
    9: #include <stdio.h>
   10: #include <string.h>
   11: #include <stdlib.h>
   12: #include <ctype.h>
   13: #include <syslog.h>
   14: #include "options.h"
   15: #include "upnppermissions.h"
   16: #include "upnpglobalvars.h"
   17: 
   18: struct option * ary_options = NULL;
   19: int num_options = 0;
   20: 
   21: static const struct {
   22: 	enum upnpconfigoptions id;
   23: 	const char * name;
   24: } optionids[] = {
   25: 	{ UPNPEXT_IFNAME, "ext_ifname" },
   26: 	{ UPNPEXT_IP,	"ext_ip" },
   27: 	{ UPNPLISTENING_IP, "listening_ip" },
   28: 	{ UPNPPORT, "port" },
   29: 	{ UPNPBITRATE_UP, "bitrate_up" },
   30: 	{ UPNPBITRATE_DOWN, "bitrate_down" },
   31: 	{ UPNPPRESENTATIONURL, "presentation_url" },
   32: 	{ UPNPNOTIFY_INTERVAL, "notify_interval" },
   33: 	{ UPNPSYSTEM_UPTIME, "system_uptime" },
   34: 	{ UPNPPACKET_LOG, "packet_log" },
   35: 	{ UPNPUUID, "uuid"},
   36: 	{ UPNPSERIAL, "serial"},
   37: 	{ UPNPMODEL_NUMBER, "model_number"},
   38: 	{ UPNPCLEANTHRESHOLD, "clean_ruleset_threshold"},
   39: 	{ UPNPCLEANINTERVAL, "clean_ruleset_interval"},
   40: #ifdef USE_NETFILTER
   41: 	{ UPNPFORWARDCHAIN, "upnp_forward_chain"},
   42: 	{ UPNPNATCHAIN, "upnp_nat_chain"},
   43: #endif
   44: #ifdef ENABLE_NATPMP
   45: 	{ UPNPENABLENATPMP, "enable_natpmp"},
   46: #endif
   47: 	{ UPNPENABLE, "enable_upnp"},
   48: #ifdef USE_PF
   49: 	{ UPNPQUEUE, "queue"},
   50: 	{ UPNPTAG, "tag"},
   51: #endif
   52: #ifdef PF_ENABLE_FILTER_RULES
   53: 	{ UPNPQUICKRULES, "quickrules" },
   54: #endif
   55: #ifdef ENABLE_LEASEFILE
   56: 	{ UPNPLEASEFILE, "lease_file"},
   57: #endif
   58: 	{ UPNPMINISSDPDSOCKET, "minissdpdsocket"},
   59: 	{ UPNPSECUREMODE, "secure_mode"}
   60: };
   61: 
   62: int
   63: readoptionsfile(const char * fname)
   64: {
   65: 	FILE *hfile = NULL;
   66: 	char buffer[1024];
   67: 	char *equals;
   68: 	char *name;
   69: 	char *value;
   70: 	char *t;
   71: 	int linenum = 0;
   72: 	int i;
   73: 	enum upnpconfigoptions id;
   74: 
   75: 	if(!fname || (strlen(fname) == 0))
   76: 		return -1;
   77: 
   78: 	memset(buffer, 0, sizeof(buffer));
   79: 
   80: #ifdef DEBUG
   81: 	printf("Reading configuration from file %s\n", fname);
   82: #endif
   83: 
   84: 	if(!(hfile = fopen(fname, "r")))
   85: 		return -1;
   86: 
   87: 	if(ary_options != NULL)
   88: 	{
   89: 		free(ary_options);
   90: 		num_options = 0;
   91: 	}
   92: 
   93: 	while(fgets(buffer, sizeof(buffer), hfile))
   94: 	{
   95: 		linenum++;
   96: 		t = strchr(buffer, '\n'); 
   97: 		if(t)
   98: 		{
   99: 			*t = '\0';
  100: 			t--;
  101: 			while((t >= buffer) && isspace(*t))
  102: 			{
  103: 				*t = '\0';
  104: 				t--;
  105: 			}
  106: 		}
  107:        
  108: 		/* skip leading whitespaces */
  109: 		name = buffer;
  110: 		while(isspace(*name))
  111: 			name++;
  112: 
  113: 		/* check for comments or empty lines */
  114: 		if(name[0] == '#' || name[0] == '\0') continue;
  115: 
  116: 		/* check for UPnP permissions rule */
  117: 		if(0 == memcmp(name, "allow", 5) || 0 == memcmp(name, "deny", 4))
  118: 		{
  119: 			upnppermlist = realloc(upnppermlist,
  120: 			                       sizeof(struct upnpperm) * (num_upnpperm+1));
  121: 			/* parse the rule */
  122: 			if(read_permission_line(upnppermlist + num_upnpperm, name) >= 0)
  123: 			{
  124: 				num_upnpperm++;
  125: 			}
  126: 			else
  127: 			{
  128: 				fprintf(stderr, "parsing error file %s line %d : %s\n",
  129: 				        fname, linenum, name);
  130: 			}
  131: 			continue;
  132: 		}
  133: 		if(!(equals = strchr(name, '=')))
  134: 		{
  135: 			fprintf(stderr, "parsing error file %s line %d : %s\n",
  136: 			        fname, linenum, name);
  137: 			continue;
  138: 		}
  139: 
  140: 		/* remove ending whitespaces */
  141: 		for(t=equals-1; t>name && isspace(*t); t--)
  142: 			*t = '\0';
  143: 
  144: 		*equals = '\0';
  145: 		value = equals+1;
  146: 
  147: 		/* skip leading whitespaces */
  148: 		while(isspace(*value))
  149: 			value++;
  150: 
  151: 		id = UPNP_INVALID;
  152: 		for(i=0; i<sizeof(optionids)/sizeof(optionids[0]); i++)
  153: 		{
  154: 			/*printf("%2d %2d %s %s\n", i, optionids[i].id, name,
  155: 			       optionids[i].name); */
  156: 
  157: 			if(0 == strcmp(name, optionids[i].name))
  158: 			{
  159: 				id = optionids[i].id;
  160: 				break;
  161: 			}
  162: 		}
  163: 
  164: 		if(id == UPNP_INVALID)
  165: 		{
  166: 			fprintf(stderr, "parsing error file %s line %d : %s=%s\n",
  167: 			        fname, linenum, name, value);
  168: 		}
  169: 		else
  170: 		{
  171: 			num_options += 1;
  172: 			ary_options = (struct option *) realloc(ary_options, num_options * sizeof(struct option));
  173: 
  174: 			ary_options[num_options-1].id = id;
  175: 			strncpy(ary_options[num_options-1].value, value, MAX_OPTION_VALUE_LEN);
  176: 		}
  177: 
  178: 	}
  179: 	
  180: 	fclose(hfile);
  181: 	
  182: 	return 0;
  183: }
  184: 
  185: void
  186: freeoptions(void)
  187: {
  188: 	if(ary_options)
  189: 	{
  190: 		free(ary_options);
  191: 		ary_options = NULL;
  192: 		num_options = 0;
  193: 	}
  194: }
  195: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>