1 /**********************************************************************
2  * Copyright (C) (2004) (Jack Louis) <jack@dyadsecurity.com>          *
3  *                                                                    *
4  * This program is free software; you can redistribute it and/or      *
5  * modify it under the terms of the GNU General Public License        *
6  * as published by the Free Software Foundation; either               *
7  * version 2 of the License, or (at your option) any later            *
8  * version.                                                           *
9  *                                                                    *
10  * This program is distributed in the hope that it will be useful,    *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      *
13  * GNU General Public License for more details.                       *
14  *                                                                    *
15  * You should have received a copy of the GNU General Public License  *
16  * along with this program; if not, write to the Free Software        *
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.          *
18  **********************************************************************/
19 #include <config.h>
20 
21 #include <netdb.h>
22 
23 #include <settings.h>
24 #include <unilib/output.h>
25 #include <unilib/xmalloc.h>
26 #include <drone.h>
27 
parse_drone_list(const char * input)28 int parse_drone_list(const char *input) {
29 	char *data=NULL, *start=NULL;
30 	size_t slen=0;
31 	int j=0;
32 	char hostbuf[128];
33 	uint32_t sport;
34 
35 	assert(s->dlh == NULL);
36 
37 	s->dlh=(drone_list_head_t *)xmalloc(sizeof(drone_list_head_t));
38 
39 	s->dlh->head=NULL;
40 	s->dlh->bottom=NULL;
41 	s->dlh->size=0;
42 
43 	data=xstrdup(input); start=data;
44 
45 	slen=strlen(data);
46 	if (slen == 0) {
47 		MSG(M_ERR, "Drone list is too small or blank.");
48 		return -1;
49 	}
50 
51 	if (s->verbose > 5) {
52 		MSG(M_DBG2, "Drone list `%s' is %d long, starting to parse it", s->drone_str, slen);
53 	}
54 
55 	for (j=0 ; (size_t)j < slen ; j++) {
56 		if (data[j] == ',') {
57 			data[j]='\0';
58 		}
59 	}
60 
61 	if (strlen(data)) {
62 		memset(hostbuf, 0, sizeof(hostbuf));
63 		if ((j=sscanf(data, "%127[a-zA-Z0-9_.-]:%u", hostbuf, &sport)) != 2) {
64 			MSG(M_ERR, "Corrupt drone address `%s' got %d parts host `%s' port %d, use 1.2.3.4:123 for host 1.2.3.4 port 123", data, j, hostbuf, sport);
65 			return -1;
66 		}
67 		else {
68 			struct hostent *dhe=NULL;
69 
70 			dhe=gethostbyname(hostbuf);
71 			if (dhe == NULL) {
72 				MSG(M_ERR, "Unknown host `%s' in drone list", hostbuf);
73 				return -1;
74 			}
75 			else {
76 				struct in_addr *ia=NULL;
77 
78 				drone_t *d=NULL;
79 
80 				d=(drone_t *)xmalloc(sizeof(drone_t));
81 				memset(d, 0, sizeof(drone_t));
82 
83 				d->status=0;
84 				d->type=0;
85 				ia=(struct in_addr *)dhe->h_addr_list[0];
86 				d->dsa.sin_addr.s_addr=ia->s_addr;
87 				d->dsa.sin_port=htons(sport);
88 				d->s=-1;
89 				d->next=NULL;
90 
91 				s->dlh->head=d;
92 				s->dlh->bottom=d;
93 				s->dlh->size=1;
94 
95 				if (s->verbose > 4) MSG(M_DBG1, "Added drone `%s:%d'", inet_ntoa(d->dsa.sin_addr), ntohs(d->dsa.sin_port));
96 			}
97 		}
98 	}
99 
100 	for (j=0 ; (size_t)j < slen ; j++) {
101 		if (data[j] == '\0' && (unsigned int)(j + 1) < slen) {
102 			char *dptr=NULL;
103 			++j;
104 			dptr=(data + j);
105 			if (strlen(dptr)) {
106 				memset(hostbuf, 0, sizeof(hostbuf));
107 				if (sscanf(dptr, "%127[a-zA-Z0-9_.-]:%u", hostbuf, &sport) != 2) {
108 					MSG(M_ERR, "Corrupt drone address `%s' got host `%s' port %d, use 1.2.3.4:123 for host 1.2.3.4 port 123", data, hostbuf, sport);
109 					return -1;
110 				}
111 				else {
112 					struct hostent *dhe=NULL;
113 
114 					dhe=gethostbyname(hostbuf);
115 					if (dhe == NULL) {
116 						MSG(M_ERR, "Unknown host `%s' in drone list", hostbuf);
117 						return -1;
118 					} /* hostname lookup failed */
119 					else {
120 						struct in_addr *ia=NULL;
121 
122 						drone_t *d=NULL,*l=NULL;
123 
124 						d=(drone_t *)xmalloc(sizeof(drone_t));
125 						memset(d, 0, sizeof(drone_t));
126 
127 						d->status=0;
128 						d->type=0;
129 						ia=(struct in_addr *)dhe->h_addr_list[0];
130 						d->dsa.sin_addr.s_addr=ia->s_addr;
131 						d->dsa.sin_port=htons(sport);
132 						d->s=-1;
133 						d->next=NULL;
134 
135 						l=s->dlh->bottom;
136 						s->dlh->bottom=d;
137 						l->next=d;
138 						s->dlh->size++;
139 
140 						if (s->verbose > 4) MSG(M_DBG1, "Added drone `%s:%d'", inet_ntoa(d->dsa.sin_addr), ntohs(d->dsa.sin_port));
141 					} /* hostname lookup worked */
142 				} /* sscanf worked */
143 			} /* dptr strlen > 0 */
144 		} /* there is `stuff' after this '\0' */
145 	} /* the length of the string */
146 
147 	data=start;
148 	xfree(data);
149 
150 	return 1;
151 }
152