parms.c revision 1.11 1 1.11 thorpej /* $NetBSD: parms.c,v 1.11 1998/06/02 18:02:55 thorpej Exp $ */
2 1.2 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 1983, 1993
5 1.1 thorpej * The Regents of the University of California. All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1 thorpej * modification, are permitted provided that the following conditions
9 1.1 thorpej * are met:
10 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1 thorpej * documentation and/or other materials provided with the distribution.
15 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
16 1.1 thorpej * must display the following acknowledgement:
17 1.1 thorpej * This product includes software developed by the University of
18 1.1 thorpej * California, Berkeley and its contributors.
19 1.1 thorpej * 4. Neither the name of the University nor the names of its contributors
20 1.1 thorpej * may be used to endorse or promote products derived from this software
21 1.1 thorpej * without specific prior written permission.
22 1.1 thorpej *
23 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 thorpej * SUCH DAMAGE.
34 1.1 thorpej */
35 1.1 thorpej
36 1.3 christos #if !defined(lint) && !defined(sgi) && !defined(__NetBSD__)
37 1.1 thorpej static char sccsid[] = "@(#)if.c 8.1 (Berkeley) 6/5/93";
38 1.3 christos #elif defined(__NetBSD__)
39 1.6 lukem #include <sys/cdefs.h>
40 1.11 thorpej __RCSID("$NetBSD: parms.c,v 1.11 1998/06/02 18:02:55 thorpej Exp $");
41 1.2 thorpej #endif
42 1.1 thorpej
43 1.1 thorpej #include "defs.h"
44 1.1 thorpej #include "pathnames.h"
45 1.5 christos #include <sys/stat.h>
46 1.1 thorpej
47 1.1 thorpej
48 1.1 thorpej struct parm *parms;
49 1.1 thorpej struct intnet *intnets;
50 1.11 thorpej struct r1net *r1nets;
51 1.5 christos struct tgate *tgates;
52 1.1 thorpej
53 1.1 thorpej
54 1.1 thorpej /* use configured parameters
55 1.1 thorpej */
56 1.1 thorpej void
57 1.1 thorpej get_parms(struct interface *ifp)
58 1.1 thorpej {
59 1.10 mrg static int warned_auth_in, warned_auth_out;
60 1.1 thorpej struct parm *parmp;
61 1.5 christos int i, num_passwds = 0;
62 1.1 thorpej
63 1.1 thorpej /* get all relevant parameters
64 1.1 thorpej */
65 1.1 thorpej for (parmp = parms; parmp != 0; parmp = parmp->parm_next) {
66 1.5 christos if (parmp->parm_name[0] == '\0'
67 1.5 christos || !strcmp(ifp->int_name, parmp->parm_name)
68 1.5 christos || (parmp->parm_name[0] == '\n'
69 1.5 christos && on_net(ifp->int_addr,
70 1.5 christos parmp->parm_net, parmp->parm_mask))) {
71 1.5 christos
72 1.5 christos /* This group of parameters is relevant,
73 1.1 thorpej * so get its settings
74 1.1 thorpej */
75 1.1 thorpej ifp->int_state |= parmp->parm_int_state;
76 1.5 christos for (i = 0; i < MAX_AUTH_KEYS; i++) {
77 1.5 christos if (parmp->parm_auth[0].type == RIP_AUTH_NONE
78 1.5 christos || num_passwds >= MAX_AUTH_KEYS)
79 1.5 christos break;
80 1.7 lukem memmove(&ifp->int_auth[num_passwds++],
81 1.7 lukem &parmp->parm_auth[i],
82 1.7 lukem sizeof(ifp->int_auth[0]));
83 1.5 christos }
84 1.1 thorpej if (parmp->parm_rdisc_pref != 0)
85 1.1 thorpej ifp->int_rdisc_pref = parmp->parm_rdisc_pref;
86 1.1 thorpej if (parmp->parm_rdisc_int != 0)
87 1.1 thorpej ifp->int_rdisc_int = parmp->parm_rdisc_int;
88 1.1 thorpej if (parmp->parm_d_metric != 0)
89 1.1 thorpej ifp->int_d_metric = parmp->parm_d_metric;
90 1.5 christos }
91 1.1 thorpej }
92 1.5 christos
93 1.5 christos /* Set general defaults.
94 1.5 christos *
95 1.5 christos * Default poor-man's router discovery to a metric that will
96 1.5 christos * be heard by old versions of `routed`. They ignored received
97 1.5 christos * routes with metric 15.
98 1.1 thorpej */
99 1.1 thorpej if ((ifp->int_state & IS_PM_RDISC)
100 1.1 thorpej && ifp->int_d_metric == 0)
101 1.5 christos ifp->int_d_metric = FAKE_METRIC;
102 1.1 thorpej
103 1.1 thorpej if (ifp->int_rdisc_int == 0)
104 1.1 thorpej ifp->int_rdisc_int = DefMaxAdvertiseInterval;
105 1.1 thorpej
106 1.1 thorpej if (!(ifp->int_if_flags & IFF_MULTICAST)
107 1.5 christos && !(ifp->int_state & IS_REMOTE))
108 1.1 thorpej ifp->int_state |= IS_BCAST_RDISC;
109 1.1 thorpej
110 1.1 thorpej if (ifp->int_if_flags & IFF_POINTOPOINT) {
111 1.1 thorpej ifp->int_state |= IS_BCAST_RDISC;
112 1.1 thorpej /* By default, point-to-point links should be passive
113 1.1 thorpej * about router-discovery for the sake of demand-dialing.
114 1.1 thorpej */
115 1.11 thorpej if (0 == (ifp->int_state & GROUP_IS_SOL_OUT))
116 1.1 thorpej ifp->int_state |= IS_NO_SOL_OUT;
117 1.11 thorpej if (0 == (ifp->int_state & GROUP_IS_ADV_OUT))
118 1.1 thorpej ifp->int_state |= IS_NO_ADV_OUT;
119 1.1 thorpej }
120 1.1 thorpej
121 1.1 thorpej if (0 != (ifp->int_state & (IS_PASSIVE | IS_REMOTE)))
122 1.1 thorpej ifp->int_state |= IS_NO_RDISC;
123 1.1 thorpej if (ifp->int_state & IS_PASSIVE)
124 1.5 christos ifp->int_state |= IS_NO_RIP;
125 1.5 christos
126 1.5 christos if (!IS_RIP_IN_OFF(ifp->int_state)
127 1.5 christos && ifp->int_auth[0].type != RIP_AUTH_NONE
128 1.5 christos && !(ifp->int_state & IS_NO_RIPV1_IN)
129 1.5 christos && !warned_auth_in) {
130 1.5 christos msglog("Warning: RIPv1 input via %s"
131 1.5 christos " will be accepted without authentication",
132 1.5 christos ifp->int_name);
133 1.5 christos warned_auth_in = 1;
134 1.5 christos }
135 1.5 christos if (!IS_RIP_OUT_OFF(ifp->int_state)
136 1.5 christos && ifp->int_auth[0].type != RIP_AUTH_NONE
137 1.5 christos && !(ifp->int_state & IS_NO_RIPV1_OUT)) {
138 1.5 christos if (!warned_auth_out) {
139 1.5 christos msglog("Warning: RIPv1 output via %s"
140 1.5 christos " will be sent without authentication",
141 1.5 christos ifp->int_name);
142 1.5 christos warned_auth_out = 1;
143 1.5 christos }
144 1.5 christos }
145 1.1 thorpej }
146 1.1 thorpej
147 1.1 thorpej
148 1.1 thorpej /* Read a list of gateways from /etc/gateways and add them to our tables.
149 1.1 thorpej *
150 1.1 thorpej * This file contains a list of "remote" gateways. That is usually
151 1.1 thorpej * a gateway which we cannot immediately determine if it is present or
152 1.1 thorpej * not as we can do for those provided by directly connected hardware.
153 1.1 thorpej *
154 1.1 thorpej * If a gateway is marked "passive" in the file, then we assume it
155 1.1 thorpej * does not understand RIP and assume it is always present. Those
156 1.1 thorpej * not marked passive are treated as if they were directly connected
157 1.1 thorpej * and assumed to be broken if they do not send us advertisements.
158 1.1 thorpej * All remote interfaces are added to our list, and those not marked
159 1.1 thorpej * passive are sent routing updates.
160 1.1 thorpej *
161 1.1 thorpej * A passive interface can also be local, hardware interface exempt
162 1.1 thorpej * from RIP.
163 1.1 thorpej */
164 1.1 thorpej void
165 1.1 thorpej gwkludge(void)
166 1.1 thorpej {
167 1.1 thorpej FILE *fp;
168 1.1 thorpej char *p, *lptr;
169 1.11 thorpej char lbuf[200], net_host[5], dname[64+1+64+1];
170 1.11 thorpej char gname[GNAME_LEN+1], qual[9];
171 1.1 thorpej struct interface *ifp;
172 1.1 thorpej naddr dst, netmask, gate;
173 1.11 thorpej int metric, n, lnum;
174 1.5 christos struct stat sb;
175 1.1 thorpej u_int state;
176 1.1 thorpej char *type;
177 1.1 thorpej
178 1.1 thorpej
179 1.1 thorpej fp = fopen(_PATH_GATEWAYS, "r");
180 1.1 thorpej if (fp == 0)
181 1.1 thorpej return;
182 1.1 thorpej
183 1.5 christos if (0 > fstat(fileno(fp), &sb)) {
184 1.5 christos msglog("could not stat() "_PATH_GATEWAYS);
185 1.5 christos (void)fclose(fp);
186 1.5 christos return;
187 1.5 christos }
188 1.5 christos
189 1.11 thorpej for (lnum = 1; ; lnum++) {
190 1.1 thorpej if (0 == fgets(lbuf, sizeof(lbuf)-1, fp))
191 1.1 thorpej break;
192 1.1 thorpej lptr = lbuf;
193 1.1 thorpej while (*lptr == ' ')
194 1.1 thorpej lptr++;
195 1.11 thorpej p = lptr+strlen(lptr)-1;
196 1.11 thorpej while (*p == '\n'
197 1.11 thorpej || (*p == ' ' && (p == lptr+1 || *(p-1) != '\\')))
198 1.11 thorpej *p-- = '\0';
199 1.11 thorpej if (*lptr == '\0' /* ignore null and comment lines */
200 1.1 thorpej || *lptr == '#')
201 1.1 thorpej continue;
202 1.1 thorpej
203 1.1 thorpej /* notice newfangled parameter lines
204 1.1 thorpej */
205 1.1 thorpej if (strncasecmp("net", lptr, 3)
206 1.1 thorpej && strncasecmp("host", lptr, 4)) {
207 1.5 christos p = parse_parms(lptr,
208 1.5 christos (sb.st_uid == 0
209 1.5 christos && !(sb.st_mode&(S_IRWXG|S_IRWXO))));
210 1.11 thorpej if (p != 0)
211 1.11 thorpej msglog("%s in line %d of "_PATH_GATEWAYS,
212 1.11 thorpej p, lnum);
213 1.1 thorpej continue;
214 1.1 thorpej }
215 1.1 thorpej
216 1.1 thorpej /* {net | host} XX[/M] XX gateway XX metric DD [passive | external]\n */
217 1.5 christos qual[0] = '\0';
218 1.11 thorpej /* the '64' here must be GNAME_LEN */
219 1.1 thorpej n = sscanf(lptr, "%4s %129[^ \t] gateway"
220 1.5 christos " %64[^ / \t] metric %u %8s\n",
221 1.1 thorpej net_host, dname, gname, &metric, qual);
222 1.5 christos if (n != 4 && n != 5) {
223 1.5 christos msglog("bad "_PATH_GATEWAYS" entry \"%s\"; %d values",
224 1.5 christos lptr, n);
225 1.1 thorpej continue;
226 1.1 thorpej }
227 1.5 christos if (metric >= HOPCNT_INFINITY) {
228 1.1 thorpej msglog("bad metric in "_PATH_GATEWAYS" entry \"%s\"",
229 1.1 thorpej lptr);
230 1.1 thorpej continue;
231 1.1 thorpej }
232 1.5 christos if (!strcasecmp(net_host, "host")) {
233 1.1 thorpej if (!gethost(dname, &dst)) {
234 1.1 thorpej msglog("bad host \"%s\" in "_PATH_GATEWAYS
235 1.1 thorpej " entry \"%s\"", dname, lptr);
236 1.1 thorpej continue;
237 1.1 thorpej }
238 1.1 thorpej netmask = HOST_MASK;
239 1.5 christos } else if (!strcasecmp(net_host, "net")) {
240 1.1 thorpej if (!getnet(dname, &dst, &netmask)) {
241 1.1 thorpej msglog("bad net \"%s\" in "_PATH_GATEWAYS
242 1.1 thorpej " entry \"%s\"", dname, lptr);
243 1.1 thorpej continue;
244 1.1 thorpej }
245 1.11 thorpej if (dst == RIP_DEFAULT) {
246 1.11 thorpej msglog("bad net \"%s\" in "_PATH_GATEWAYS
247 1.11 thorpej " entry \"%s\"--cannot be default",
248 1.11 thorpej dname, lptr);
249 1.11 thorpej continue;
250 1.11 thorpej }
251 1.5 christos HTONL(dst); /* make network # into IP address */
252 1.1 thorpej } else {
253 1.1 thorpej msglog("bad \"%s\" in "_PATH_GATEWAYS
254 1.1 thorpej " entry \"%s\"", lptr);
255 1.1 thorpej continue;
256 1.1 thorpej }
257 1.1 thorpej
258 1.1 thorpej if (!gethost(gname, &gate)) {
259 1.1 thorpej msglog("bad gateway \"%s\" in "_PATH_GATEWAYS
260 1.1 thorpej " entry \"%s\"", gname, lptr);
261 1.1 thorpej continue;
262 1.1 thorpej }
263 1.1 thorpej
264 1.5 christos if (!strcasecmp(qual, type = "passive")) {
265 1.1 thorpej /* Passive entries are not placed in our tables,
266 1.1 thorpej * only the kernel's, so we don't copy all of the
267 1.1 thorpej * external routing information within a net.
268 1.1 thorpej * Internal machines should use the default
269 1.1 thorpej * route to a suitable gateway (like us).
270 1.1 thorpej */
271 1.1 thorpej state = IS_REMOTE | IS_PASSIVE;
272 1.1 thorpej if (metric == 0)
273 1.1 thorpej metric = 1;
274 1.1 thorpej
275 1.5 christos } else if (!strcasecmp(qual, type = "external")) {
276 1.1 thorpej /* External entries are handled by other means
277 1.1 thorpej * such as EGP, and are placed only in the daemon
278 1.1 thorpej * tables to prevent overriding them with something
279 1.1 thorpej * else.
280 1.1 thorpej */
281 1.5 christos strcpy(qual,"external");
282 1.1 thorpej state = IS_REMOTE | IS_PASSIVE | IS_EXTERNAL;
283 1.1 thorpej if (metric == 0)
284 1.1 thorpej metric = 1;
285 1.1 thorpej
286 1.5 christos } else if (!strcasecmp(qual, "active")
287 1.5 christos || qual[0] == '\0') {
288 1.1 thorpej if (metric != 0) {
289 1.1 thorpej /* Entries that are neither "passive" nor
290 1.1 thorpej * "external" are "remote" and must behave
291 1.1 thorpej * like physical interfaces. If they are not
292 1.1 thorpej * heard from regularly, they are deleted.
293 1.1 thorpej */
294 1.1 thorpej state = IS_REMOTE;
295 1.1 thorpej type = "remote";
296 1.1 thorpej } else {
297 1.1 thorpej /* "remote" entries with a metric of 0
298 1.1 thorpej * are aliases for our own interfaces
299 1.1 thorpej */
300 1.5 christos state = IS_REMOTE | IS_PASSIVE | IS_ALIAS;
301 1.1 thorpej type = "alias";
302 1.1 thorpej }
303 1.1 thorpej
304 1.1 thorpej } else {
305 1.5 christos msglog("bad "_PATH_GATEWAYS" entry \"%s\";"
306 1.5 christos " unknown type %s", lptr, qual);
307 1.1 thorpej continue;
308 1.1 thorpej }
309 1.1 thorpej
310 1.1 thorpej if (0 != (state & (IS_PASSIVE | IS_REMOTE)))
311 1.1 thorpej state |= IS_NO_RDISC;
312 1.1 thorpej if (state & IS_PASSIVE)
313 1.5 christos state |= IS_NO_RIP;
314 1.1 thorpej
315 1.5 christos ifp = check_dup(gate,dst,netmask,0);
316 1.1 thorpej if (ifp != 0) {
317 1.5 christos msglog("duplicate "_PATH_GATEWAYS" entry \"%s\"",lptr);
318 1.1 thorpej continue;
319 1.1 thorpej }
320 1.1 thorpej
321 1.11 thorpej ifp = (struct interface *)rtmalloc(sizeof(*ifp), "gwkludge()");
322 1.6 lukem memset(ifp, 0, sizeof(*ifp));
323 1.1 thorpej
324 1.1 thorpej ifp->int_state = state;
325 1.1 thorpej if (netmask == HOST_MASK)
326 1.11 thorpej ifp->int_if_flags = IFF_POINTOPOINT | IFF_UP;
327 1.5 christos else
328 1.11 thorpej ifp->int_if_flags = IFF_UP;
329 1.5 christos ifp->int_act_time = NEVER;
330 1.5 christos ifp->int_addr = gate;
331 1.1 thorpej ifp->int_dstaddr = dst;
332 1.5 christos ifp->int_mask = netmask;
333 1.5 christos ifp->int_ripv1_mask = netmask;
334 1.5 christos ifp->int_std_mask = std_mask(gate);
335 1.5 christos ifp->int_net = ntohl(dst);
336 1.5 christos ifp->int_std_net = ifp->int_net & ifp->int_std_mask;
337 1.5 christos ifp->int_std_addr = htonl(ifp->int_std_net);
338 1.1 thorpej ifp->int_metric = metric;
339 1.5 christos if (!(state & IS_EXTERNAL)
340 1.5 christos && ifp->int_mask != ifp->int_std_mask)
341 1.5 christos ifp->int_state |= IS_SUBNET;
342 1.5 christos (void)sprintf(ifp->int_name, "%s(%s)", type, gname);
343 1.1 thorpej ifp->int_index = -1;
344 1.1 thorpej
345 1.5 christos if_link(ifp);
346 1.5 christos }
347 1.5 christos
348 1.5 christos /* After all of the parameter lines have been read,
349 1.5 christos * apply them to any remote interfaces.
350 1.5 christos */
351 1.5 christos for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next) {
352 1.1 thorpej get_parms(ifp);
353 1.1 thorpej
354 1.5 christos tot_interfaces++;
355 1.5 christos if (!IS_RIP_OFF(ifp->int_state))
356 1.5 christos rip_interfaces++;
357 1.5 christos
358 1.1 thorpej trace_if("Add", ifp);
359 1.1 thorpej }
360 1.5 christos
361 1.5 christos (void)fclose(fp);
362 1.1 thorpej }
363 1.1 thorpej
364 1.1 thorpej
365 1.11 thorpej /* like strtok(), but honoring backslash and not changing the source string
366 1.5 christos */
367 1.5 christos static int /* 0=ok, -1=bad */
368 1.11 thorpej parse_quote(char **linep, /* look here */
369 1.11 thorpej char *delims, /* for these delimiters */
370 1.11 thorpej char *delimp, /* 0 or put found delimiter here */
371 1.11 thorpej char *buf, /* copy token to here */
372 1.11 thorpej int lim) /* at most this many bytes */
373 1.5 christos {
374 1.8 christos char c = '\0';
375 1.8 christos char *pc, *p;
376 1.5 christos
377 1.5 christos
378 1.5 christos pc = *linep;
379 1.5 christos if (*pc == '\0')
380 1.5 christos return -1;
381 1.5 christos
382 1.5 christos while (lim != 0) {
383 1.5 christos c = *pc++;
384 1.5 christos if (c == '\0')
385 1.5 christos break;
386 1.5 christos
387 1.5 christos if (c == '\\' && pc != '\0') {
388 1.5 christos if ((c = *pc++) == 'n') {
389 1.5 christos c = '\n';
390 1.5 christos } else if (c == 'r') {
391 1.5 christos c = '\r';
392 1.5 christos } else if (c == 't') {
393 1.5 christos c = '\t';
394 1.5 christos } else if (c == 'b') {
395 1.5 christos c = '\b';
396 1.5 christos } else if (c >= '0' && c <= '7') {
397 1.5 christos c -= '0';
398 1.5 christos if (*pc >= '0' && *pc <= '7') {
399 1.5 christos c = (c<<3)+(*pc++ - '0');
400 1.5 christos if (*pc >= '0' && *pc <= '7')
401 1.5 christos c = (c<<3)+(*pc++ - '0');
402 1.5 christos }
403 1.5 christos }
404 1.5 christos
405 1.5 christos } else {
406 1.5 christos for (p = delims; *p != '\0'; ++p) {
407 1.5 christos if (*p == c)
408 1.5 christos goto exit;
409 1.5 christos }
410 1.5 christos }
411 1.5 christos
412 1.5 christos *buf++ = c;
413 1.5 christos --lim;
414 1.5 christos }
415 1.5 christos exit:
416 1.5 christos if (lim == 0)
417 1.5 christos return -1;
418 1.5 christos
419 1.11 thorpej *buf = '\0'; /* terminate copy of token */
420 1.5 christos if (delimp != 0)
421 1.11 thorpej *delimp = c; /* return delimiter */
422 1.11 thorpej *linep = pc-1; /* say where we ended */
423 1.5 christos return 0;
424 1.5 christos }
425 1.5 christos
426 1.5 christos
427 1.5 christos /* Parse password timestamp
428 1.5 christos */
429 1.5 christos static char *
430 1.5 christos parse_ts(time_t *tp,
431 1.5 christos char **valp,
432 1.5 christos char *val0,
433 1.5 christos char *delimp,
434 1.5 christos char *buf,
435 1.5 christos u_int bufsize)
436 1.5 christos {
437 1.5 christos struct tm tm;
438 1.9 christos char *ptr;
439 1.5 christos
440 1.5 christos if (0 > parse_quote(valp, "| ,\n\r", delimp,
441 1.5 christos buf,bufsize)
442 1.5 christos || buf[bufsize-1] != '\0'
443 1.5 christos || buf[bufsize-2] != '\0') {
444 1.5 christos sprintf(buf,"bad timestamp %.25s", val0);
445 1.5 christos return buf;
446 1.5 christos }
447 1.5 christos strcat(buf,"\n");
448 1.6 lukem memset(&tm, 0, sizeof(tm));
449 1.9 christos
450 1.9 christos ptr = strptime(buf, "%y/%m/%d@%H:%M\n", &tm);
451 1.9 christos if (ptr == NULL || *ptr != '\0') {
452 1.5 christos sprintf(buf,"bad timestamp %.25s", val0);
453 1.5 christos return buf;
454 1.5 christos }
455 1.5 christos
456 1.5 christos if ((*tp = mktime(&tm)) == -1) {
457 1.5 christos sprintf(buf,"bad timestamp %.25s", val0);
458 1.5 christos return buf;
459 1.5 christos }
460 1.5 christos
461 1.5 christos return 0;
462 1.5 christos }
463 1.5 christos
464 1.5 christos
465 1.5 christos /* Get a password, key ID, and expiration date in the format
466 1.5 christos * passwd|keyID|year/mon/day@hour:min|year/mon/day@hour:min
467 1.5 christos */
468 1.5 christos static char * /* 0 or error message */
469 1.5 christos get_passwd(char *tgt,
470 1.5 christos char *val,
471 1.5 christos struct parm *parmp,
472 1.5 christos u_char type,
473 1.5 christos int safe) /* 1=from secure file */
474 1.5 christos {
475 1.5 christos static char buf[80];
476 1.5 christos char *val0, *p, delim;
477 1.5 christos struct auth k, *ap, *ap2;
478 1.5 christos int i;
479 1.5 christos u_long l;
480 1.5 christos
481 1.5 christos
482 1.5 christos if (!safe)
483 1.5 christos return "ignore unsafe password";
484 1.5 christos
485 1.5 christos for (ap = parmp->parm_auth, i = 0;
486 1.5 christos ap->type != RIP_AUTH_NONE; i++, ap++) {
487 1.5 christos if (i >= MAX_AUTH_KEYS)
488 1.5 christos return "too many passwords";
489 1.5 christos }
490 1.5 christos
491 1.6 lukem memset(&k, 0, sizeof(k));
492 1.5 christos k.type = type;
493 1.5 christos k.end = -1-DAY;
494 1.5 christos
495 1.5 christos val0 = val;
496 1.5 christos if (0 > parse_quote(&val, "| ,\n\r", &delim,
497 1.5 christos (char *)k.key, sizeof(k.key)))
498 1.5 christos return tgt;
499 1.5 christos
500 1.5 christos if (delim != '|') {
501 1.5 christos if (type == RIP_AUTH_MD5)
502 1.5 christos return "missing Keyid";
503 1.5 christos } else {
504 1.5 christos val0 = ++val;
505 1.5 christos buf[sizeof(buf)-1] = '\0';
506 1.5 christos if (0 > parse_quote(&val, "| ,\n\r", &delim, buf,sizeof(buf))
507 1.5 christos || buf[sizeof(buf)-1] != '\0'
508 1.5 christos || (l = strtoul(buf,&p,0)) > 255
509 1.5 christos || *p != '\0') {
510 1.5 christos sprintf(buf,"bad KeyID \"%.20s\"", val0);
511 1.5 christos return buf;
512 1.5 christos }
513 1.5 christos for (ap2 = parmp->parm_auth; ap2 < ap; ap2++) {
514 1.5 christos if (ap2->keyid == l) {
515 1.5 christos sprintf(buf,"duplicate KeyID \"%.20s\"", val0);
516 1.5 christos return buf;
517 1.5 christos }
518 1.5 christos }
519 1.5 christos k.keyid = (int)l;
520 1.5 christos
521 1.5 christos if (delim == '|') {
522 1.5 christos val0 = ++val;
523 1.5 christos if (0 != (p = parse_ts(&k.start,&val,val0,&delim,
524 1.5 christos buf,sizeof(buf))))
525 1.5 christos return p;
526 1.5 christos if (delim != '|')
527 1.5 christos return "missing second timestamp";
528 1.5 christos val0 = ++val;
529 1.5 christos if (0 != (p = parse_ts(&k.end,&val,val0,&delim,
530 1.5 christos buf,sizeof(buf))))
531 1.5 christos return p;
532 1.5 christos if ((u_long)k.start > (u_long)k.end) {
533 1.5 christos sprintf(buf,"out of order timestamp %.30s",
534 1.5 christos val0);
535 1.5 christos return buf;
536 1.5 christos }
537 1.5 christos }
538 1.5 christos }
539 1.5 christos if (delim != '\0')
540 1.5 christos return tgt;
541 1.5 christos
542 1.7 lukem memmove(ap, &k, sizeof(*ap));
543 1.5 christos return 0;
544 1.5 christos }
545 1.5 christos
546 1.5 christos
547 1.11 thorpej static char *
548 1.11 thorpej bad_str(char *estr)
549 1.11 thorpej {
550 1.11 thorpej static char buf[100+8];
551 1.11 thorpej
552 1.11 thorpej sprintf(buf, "bad \"%.100s\"", estr);
553 1.11 thorpej return buf;
554 1.11 thorpej }
555 1.11 thorpej
556 1.11 thorpej
557 1.5 christos /* Parse a set of parameters for an interface.
558 1.1 thorpej */
559 1.1 thorpej char * /* 0 or error message */
560 1.5 christos parse_parms(char *line,
561 1.5 christos int safe) /* 1=from secure file */
562 1.1 thorpej {
563 1.5 christos #define PARS(str) (!strcasecmp(tgt, str))
564 1.5 christos #define PARSEQ(str) (!strncasecmp(tgt, str"=", sizeof(str)))
565 1.1 thorpej #define CKF(g,b) {if (0 != (parm.parm_int_state & ((g) & ~(b)))) break; \
566 1.1 thorpej parm.parm_int_state |= (b);}
567 1.1 thorpej struct parm parm;
568 1.1 thorpej struct intnet *intnetp;
569 1.11 thorpej struct r1net *r1netp;
570 1.5 christos struct tgate *tg;
571 1.5 christos naddr addr, mask;
572 1.11 thorpej char delim, *val0, *tgt, *val, *p;
573 1.11 thorpej char buf[BUFSIZ], buf2[BUFSIZ];
574 1.11 thorpej int i;
575 1.1 thorpej
576 1.11 thorpej val0 = NULL; /* XXX gcc -Wuninitialized */
577 1.1 thorpej
578 1.11 thorpej /* "subnet=x.y.z.u/mask[,metric]" must be alone on the line */
579 1.5 christos if (!strncasecmp(line, "subnet=", sizeof("subnet=")-1)
580 1.11 thorpej && *(val = &line[sizeof("subnet=")-1]) != '\0') {
581 1.11 thorpej if (0 > parse_quote(&val, ",", &delim, buf, sizeof(buf)))
582 1.11 thorpej return bad_str(line);
583 1.11 thorpej intnetp = (struct intnet*)rtmalloc(sizeof(*intnetp),
584 1.11 thorpej "parse_parms subnet");
585 1.1 thorpej intnetp->intnet_metric = 1;
586 1.11 thorpej if (delim == ',') {
587 1.11 thorpej intnetp->intnet_metric = (int)strtol(val+1,&p,0);
588 1.1 thorpej if (*p != '\0'
589 1.1 thorpej || intnetp->intnet_metric <= 0
590 1.1 thorpej || intnetp->intnet_metric >= HOPCNT_INFINITY)
591 1.11 thorpej return bad_str(line);
592 1.1 thorpej }
593 1.11 thorpej if (!getnet(buf, &intnetp->intnet_addr, &intnetp->intnet_mask)
594 1.1 thorpej || intnetp->intnet_mask == HOST_MASK
595 1.1 thorpej || intnetp->intnet_addr == RIP_DEFAULT) {
596 1.1 thorpej free(intnetp);
597 1.11 thorpej return bad_str(line);
598 1.1 thorpej }
599 1.5 christos HTONL(intnetp->intnet_addr);
600 1.1 thorpej intnetp->intnet_next = intnets;
601 1.1 thorpej intnets = intnetp;
602 1.1 thorpej return 0;
603 1.1 thorpej }
604 1.1 thorpej
605 1.11 thorpej /* "ripv1_mask=x.y.z.u/mask,mask" must be alone on the line */
606 1.11 thorpej if (!strncasecmp(line, "ripv1_mask=", sizeof("ripv1_mask=")-1)
607 1.11 thorpej && *(val = &line[sizeof("ripv1_mask=")-1]) != '\0') {
608 1.11 thorpej if (0 > parse_quote(&val, ",", &delim, buf, sizeof(buf))
609 1.11 thorpej || delim == '\0')
610 1.11 thorpej return bad_str(line);
611 1.11 thorpej if ((i = (int)strtol(val+1, &p, 0)) <= 0
612 1.11 thorpej || i > 32 || *p != '\0')
613 1.11 thorpej return bad_str(line);
614 1.11 thorpej r1netp = (struct r1net *)rtmalloc(sizeof(*r1netp),
615 1.11 thorpej "parse_parms ripv1_mask");
616 1.11 thorpej r1netp->r1net_mask = HOST_MASK << (32-i);
617 1.11 thorpej if (!getnet(buf, &r1netp->r1net_net, &r1netp->r1net_match)
618 1.11 thorpej || r1netp->r1net_net == RIP_DEFAULT
619 1.11 thorpej || r1netp->r1net_mask < r1netp->r1net_match) {
620 1.11 thorpej free(r1netp);
621 1.11 thorpej return bad_str(line);
622 1.11 thorpej }
623 1.11 thorpej r1netp->r1net_next = r1nets;
624 1.11 thorpej r1nets = r1netp;
625 1.11 thorpej return 0;
626 1.11 thorpej }
627 1.11 thorpej
628 1.6 lukem memset(&parm, 0, sizeof(parm));
629 1.1 thorpej
630 1.1 thorpej tgt = "null";
631 1.5 christos for (;;) {
632 1.5 christos tgt = line + strspn(line, " ,\n\r");
633 1.11 thorpej if (*tgt == '\0' || *tgt == '#')
634 1.5 christos break;
635 1.11 thorpej line = tgt+strcspn(tgt, "= #,\n\r");
636 1.5 christos delim = *line;
637 1.5 christos if (delim == '=') {
638 1.5 christos val0 = ++line;
639 1.11 thorpej if (0 > parse_quote(&line, " #,\n\r",&delim,
640 1.5 christos buf,sizeof(buf)))
641 1.11 thorpej return bad_str(tgt);
642 1.11 thorpej }
643 1.11 thorpej if (delim != '\0') {
644 1.11 thorpej for (;;) {
645 1.11 thorpej *line = '\0';
646 1.11 thorpej if (delim == '#')
647 1.11 thorpej break;
648 1.11 thorpej ++line;
649 1.11 thorpej if (delim != ' '
650 1.11 thorpej || (delim = *line) != ' ')
651 1.11 thorpej break;
652 1.11 thorpej }
653 1.5 christos }
654 1.5 christos
655 1.5 christos if (PARSEQ("if")) {
656 1.1 thorpej if (parm.parm_name[0] != '\0'
657 1.11 thorpej || strlen(buf) > IF_NAME_LEN)
658 1.11 thorpej return bad_str(tgt);
659 1.5 christos strcpy(parm.parm_name, buf);
660 1.5 christos
661 1.5 christos } else if (PARSEQ("addr")) {
662 1.5 christos /* This is a bad idea, because the address based
663 1.5 christos * sets of parameters cannot be checked for
664 1.5 christos * consistency with the interface name parameters.
665 1.5 christos * The parm_net stuff is needed to allow several
666 1.5 christos * -F settings.
667 1.5 christos */
668 1.5 christos if (!getnet(val0, &addr, &mask)
669 1.5 christos || parm.parm_name[0] != '\0')
670 1.11 thorpej return bad_str(tgt);
671 1.5 christos parm.parm_net = addr;
672 1.5 christos parm.parm_mask = mask;
673 1.5 christos parm.parm_name[0] = '\n';
674 1.5 christos
675 1.5 christos } else if (PARSEQ("passwd")) {
676 1.5 christos /* since cleartext passwords are so weak allow
677 1.5 christos * them anywhere
678 1.5 christos */
679 1.5 christos tgt = get_passwd(tgt,val0,&parm,RIP_AUTH_PW,1);
680 1.5 christos if (tgt) {
681 1.5 christos *val0 = '\0';
682 1.11 thorpej return bad_str(tgt);
683 1.5 christos }
684 1.5 christos
685 1.5 christos } else if (PARSEQ("md5_passwd")) {
686 1.5 christos tgt = get_passwd(tgt,val0,&parm,RIP_AUTH_MD5,safe);
687 1.5 christos if (tgt) {
688 1.5 christos *val0 = '\0';
689 1.11 thorpej return bad_str(tgt);
690 1.5 christos }
691 1.1 thorpej
692 1.1 thorpej } else if (PARS("no_ag")) {
693 1.1 thorpej parm.parm_int_state |= (IS_NO_AG | IS_NO_SUPER_AG);
694 1.1 thorpej
695 1.1 thorpej } else if (PARS("no_super_ag")) {
696 1.1 thorpej parm.parm_int_state |= IS_NO_SUPER_AG;
697 1.1 thorpej
698 1.1 thorpej } else if (PARS("no_ripv1_in")) {
699 1.1 thorpej parm.parm_int_state |= IS_NO_RIPV1_IN;
700 1.1 thorpej
701 1.1 thorpej } else if (PARS("no_ripv2_in")) {
702 1.1 thorpej parm.parm_int_state |= IS_NO_RIPV2_IN;
703 1.1 thorpej
704 1.1 thorpej } else if (PARS("ripv2_out")) {
705 1.1 thorpej if (parm.parm_int_state & IS_NO_RIPV2_OUT)
706 1.11 thorpej return bad_str(tgt);
707 1.1 thorpej parm.parm_int_state |= IS_NO_RIPV1_OUT;
708 1.1 thorpej
709 1.5 christos } else if (PARS("ripv2")) {
710 1.5 christos if ((parm.parm_int_state & IS_NO_RIPV2_OUT)
711 1.5 christos || (parm.parm_int_state & IS_NO_RIPV2_IN))
712 1.11 thorpej return bad_str(tgt);
713 1.5 christos parm.parm_int_state |= (IS_NO_RIPV1_IN
714 1.5 christos | IS_NO_RIPV1_OUT);
715 1.5 christos
716 1.1 thorpej } else if (PARS("no_rip")) {
717 1.5 christos CKF(IS_PM_RDISC, IS_NO_RIP);
718 1.1 thorpej
719 1.11 thorpej } else if (PARS("no_rip_mcast")) {
720 1.11 thorpej parm.parm_int_state |= IS_NO_RIP_MCAST;
721 1.11 thorpej
722 1.1 thorpej } else if (PARS("no_rdisc")) {
723 1.11 thorpej CKF((GROUP_IS_SOL_OUT|GROUP_IS_ADV_OUT), IS_NO_RDISC);
724 1.1 thorpej
725 1.1 thorpej } else if (PARS("no_solicit")) {
726 1.11 thorpej CKF(GROUP_IS_SOL_OUT, IS_NO_SOL_OUT);
727 1.1 thorpej
728 1.1 thorpej } else if (PARS("send_solicit")) {
729 1.11 thorpej CKF(GROUP_IS_SOL_OUT, IS_SOL_OUT);
730 1.1 thorpej
731 1.1 thorpej } else if (PARS("no_rdisc_adv")) {
732 1.11 thorpej CKF(GROUP_IS_ADV_OUT, IS_NO_ADV_OUT);
733 1.1 thorpej
734 1.1 thorpej } else if (PARS("rdisc_adv")) {
735 1.11 thorpej CKF(GROUP_IS_ADV_OUT, IS_ADV_OUT);
736 1.1 thorpej
737 1.1 thorpej } else if (PARS("bcast_rdisc")) {
738 1.1 thorpej parm.parm_int_state |= IS_BCAST_RDISC;
739 1.1 thorpej
740 1.1 thorpej } else if (PARS("passive")) {
741 1.11 thorpej CKF((GROUP_IS_SOL_OUT|GROUP_IS_ADV_OUT), IS_NO_RDISC);
742 1.1 thorpej parm.parm_int_state |= IS_NO_RIP;
743 1.1 thorpej
744 1.5 christos } else if (PARSEQ("rdisc_pref")) {
745 1.1 thorpej if (parm.parm_rdisc_pref != 0
746 1.11 thorpej || (parm.parm_rdisc_pref = (int)strtol(buf,&p,0),
747 1.1 thorpej *p != '\0'))
748 1.11 thorpej return bad_str(tgt);
749 1.1 thorpej
750 1.1 thorpej } else if (PARS("pm_rdisc")) {
751 1.5 christos if (IS_RIP_OUT_OFF(parm.parm_int_state))
752 1.11 thorpej return bad_str(tgt);
753 1.1 thorpej parm.parm_int_state |= IS_PM_RDISC;
754 1.1 thorpej
755 1.5 christos } else if (PARSEQ("rdisc_interval")) {
756 1.1 thorpej if (parm.parm_rdisc_int != 0
757 1.5 christos || (parm.parm_rdisc_int = (int)strtoul(buf,&p,0),
758 1.1 thorpej *p != '\0')
759 1.1 thorpej || parm.parm_rdisc_int < MinMaxAdvertiseInterval
760 1.1 thorpej || parm.parm_rdisc_int > MaxMaxAdvertiseInterval)
761 1.11 thorpej return bad_str(tgt);
762 1.1 thorpej
763 1.5 christos } else if (PARSEQ("fake_default")) {
764 1.1 thorpej if (parm.parm_d_metric != 0
765 1.5 christos || IS_RIP_OUT_OFF(parm.parm_int_state)
766 1.5 christos || (parm.parm_d_metric = (int)strtoul(buf,&p,0),
767 1.1 thorpej *p != '\0')
768 1.1 thorpej || parm.parm_d_metric > HOPCNT_INFINITY-1)
769 1.11 thorpej return bad_str(tgt);
770 1.5 christos
771 1.5 christos } else if (PARSEQ("trust_gateway")) {
772 1.11 thorpej /* look for trust_gateway=x.y.z|net/mask|...) */
773 1.11 thorpej p = buf;
774 1.11 thorpej if (0 > parse_quote(&p, "|", &delim,
775 1.11 thorpej buf2, sizeof(buf2))
776 1.11 thorpej || !gethost(buf2,&addr))
777 1.11 thorpej return bad_str(tgt);
778 1.11 thorpej tg = (struct tgate *)rtmalloc(sizeof(*tg),
779 1.11 thorpej "parse_parms"
780 1.11 thorpej "trust_gateway");
781 1.11 thorpej bzero(tg, sizeof(*tg));
782 1.11 thorpej tg->tgate_addr = addr;
783 1.11 thorpej i = 0;
784 1.11 thorpej /* The default is to trust all routes. */
785 1.11 thorpej while (delim == '|') {
786 1.11 thorpej p++;
787 1.11 thorpej if (i >= MAX_TGATE_NETS
788 1.11 thorpej || 0 > parse_quote(&p, "|", &delim,
789 1.11 thorpej buf2, sizeof(buf2))
790 1.11 thorpej || !getnet(buf2, &tg->tgate_nets[i].net,
791 1.11 thorpej &tg->tgate_nets[i].mask)
792 1.11 thorpej || tg->tgate_nets[i].net == RIP_DEFAULT
793 1.11 thorpej || tg->tgate_nets[i].mask == 0)
794 1.11 thorpej return bad_str(tgt);
795 1.11 thorpej i++;
796 1.11 thorpej }
797 1.5 christos tg->tgate_next = tgates;
798 1.5 christos tgates = tg;
799 1.5 christos parm.parm_int_state |= IS_DISTRUST;
800 1.5 christos
801 1.5 christos } else if (PARS("redirect_ok")) {
802 1.5 christos parm.parm_int_state |= IS_REDIRECT_OK;
803 1.1 thorpej
804 1.1 thorpej } else {
805 1.11 thorpej return bad_str(tgt); /* error */
806 1.1 thorpej }
807 1.1 thorpej }
808 1.1 thorpej
809 1.1 thorpej return check_parms(&parm);
810 1.1 thorpej #undef PARS
811 1.5 christos #undef PARSEQ
812 1.1 thorpej }
813 1.1 thorpej
814 1.1 thorpej
815 1.1 thorpej /* check for duplicate parameter specifications */
816 1.1 thorpej char * /* 0 or error message */
817 1.1 thorpej check_parms(struct parm *new)
818 1.1 thorpej {
819 1.5 christos struct parm *parmp, **parmpp;
820 1.5 christos int i, num_passwds;
821 1.1 thorpej
822 1.3 christos /* set implicit values
823 1.3 christos */
824 1.3 christos if (new->parm_int_state & IS_NO_ADV_IN)
825 1.3 christos new->parm_int_state |= IS_NO_SOL_OUT;
826 1.11 thorpej if (new->parm_int_state & IS_NO_SOL_OUT)
827 1.11 thorpej new->parm_int_state |= IS_NO_ADV_IN;
828 1.3 christos
829 1.5 christos for (i = num_passwds = 0; i < MAX_AUTH_KEYS; i++) {
830 1.5 christos if (new->parm_auth[i].type != RIP_AUTH_NONE)
831 1.5 christos num_passwds++;
832 1.5 christos }
833 1.3 christos
834 1.3 christos /* compare with existing sets of parameters
835 1.3 christos */
836 1.5 christos for (parmpp = &parms;
837 1.5 christos (parmp = *parmpp) != 0;
838 1.5 christos parmpp = &parmp->parm_next) {
839 1.1 thorpej if (strcmp(new->parm_name, parmp->parm_name))
840 1.1 thorpej continue;
841 1.5 christos if (!on_net(htonl(parmp->parm_net),
842 1.5 christos new->parm_net, new->parm_mask)
843 1.5 christos && !on_net(htonl(new->parm_net),
844 1.5 christos parmp->parm_net, parmp->parm_mask))
845 1.1 thorpej continue;
846 1.1 thorpej
847 1.5 christos for (i = 0; i < MAX_AUTH_KEYS; i++) {
848 1.5 christos if (parmp->parm_auth[i].type != RIP_AUTH_NONE)
849 1.5 christos num_passwds++;
850 1.5 christos }
851 1.5 christos if (num_passwds > MAX_AUTH_KEYS)
852 1.5 christos return "too many conflicting passwords";
853 1.5 christos
854 1.11 thorpej if ((0 != (new->parm_int_state & GROUP_IS_SOL_OUT)
855 1.11 thorpej && 0 != (parmp->parm_int_state & GROUP_IS_SOL_OUT)
856 1.5 christos && 0 != ((new->parm_int_state ^ parmp->parm_int_state)
857 1.11 thorpej && GROUP_IS_SOL_OUT))
858 1.11 thorpej || (0 != (new->parm_int_state & GROUP_IS_ADV_OUT)
859 1.11 thorpej && 0 != (parmp->parm_int_state & GROUP_IS_ADV_OUT)
860 1.1 thorpej && 0 != ((new->parm_int_state ^ parmp->parm_int_state)
861 1.11 thorpej && GROUP_IS_ADV_OUT))
862 1.1 thorpej || (new->parm_rdisc_pref != 0
863 1.1 thorpej && parmp->parm_rdisc_pref != 0
864 1.1 thorpej && new->parm_rdisc_pref != parmp->parm_rdisc_pref)
865 1.1 thorpej || (new->parm_rdisc_int != 0
866 1.1 thorpej && parmp->parm_rdisc_int != 0
867 1.5 christos && new->parm_rdisc_int != parmp->parm_rdisc_int)) {
868 1.5 christos return ("conflicting, duplicate router discovery"
869 1.5 christos " parameters");
870 1.5 christos
871 1.5 christos }
872 1.5 christos
873 1.5 christos if (new->parm_d_metric != 0
874 1.5 christos && parmp->parm_d_metric != 0
875 1.5 christos && new->parm_d_metric != parmp->parm_d_metric) {
876 1.5 christos return ("conflicting, duplicate poor man's router"
877 1.5 christos " discovery or fake default metric");
878 1.5 christos }
879 1.1 thorpej }
880 1.1 thorpej
881 1.5 christos /* link new entry on the so that when the entries are scanned,
882 1.5 christos * they affect the result in the order the operator specified.
883 1.5 christos */
884 1.11 thorpej parmp = (struct parm*)rtmalloc(sizeof(*parmp), "check_parms");
885 1.7 lukem memmove(parmp, new, sizeof(*parmp));
886 1.5 christos *parmpp = parmp;
887 1.1 thorpej
888 1.1 thorpej return 0;
889 1.1 thorpej }
890 1.1 thorpej
891 1.1 thorpej
892 1.1 thorpej /* get a network number as a name or a number, with an optional "/xx"
893 1.1 thorpej * netmask.
894 1.1 thorpej */
895 1.1 thorpej int /* 0=bad */
896 1.1 thorpej getnet(char *name,
897 1.11 thorpej naddr *netp, /* network in host byte order */
898 1.5 christos naddr *maskp) /* masks are always in host order */
899 1.1 thorpej {
900 1.1 thorpej int i;
901 1.1 thorpej struct netent *np;
902 1.5 christos naddr mask; /* in host byte order */
903 1.5 christos struct in_addr in; /* a network and so host byte order */
904 1.1 thorpej char hname[MAXHOSTNAMELEN+1];
905 1.1 thorpej char *mname, *p;
906 1.1 thorpej
907 1.1 thorpej
908 1.1 thorpej /* Detect and separate "1.2.3.4/24"
909 1.1 thorpej */
910 1.7 lukem if (0 != (mname = strrchr(name,'/'))) {
911 1.1 thorpej i = (int)(mname - name);
912 1.1 thorpej if (i > sizeof(hname)-1) /* name too long */
913 1.1 thorpej return 0;
914 1.7 lukem memmove(hname, name, i);
915 1.1 thorpej hname[i] = '\0';
916 1.1 thorpej mname++;
917 1.1 thorpej name = hname;
918 1.1 thorpej }
919 1.1 thorpej
920 1.1 thorpej np = getnetbyname(name);
921 1.1 thorpej if (np != 0) {
922 1.1 thorpej in.s_addr = (naddr)np->n_net;
923 1.11 thorpej if (0 == (in.s_addr & 0xff000000))
924 1.11 thorpej in.s_addr <<= 8;
925 1.11 thorpej if (0 == (in.s_addr & 0xff000000))
926 1.11 thorpej in.s_addr <<= 8;
927 1.11 thorpej if (0 == (in.s_addr & 0xff000000))
928 1.11 thorpej in.s_addr <<= 8;
929 1.1 thorpej } else if (inet_aton(name, &in) == 1) {
930 1.5 christos NTOHL(in.s_addr);
931 1.5 christos } else if (!mname && !strcasecmp(name,"default")) {
932 1.5 christos in.s_addr = RIP_DEFAULT;
933 1.1 thorpej } else {
934 1.1 thorpej return 0;
935 1.1 thorpej }
936 1.1 thorpej
937 1.5 christos if (!mname) {
938 1.1 thorpej /* we cannot use the interfaces here because we have not
939 1.1 thorpej * looked at them yet.
940 1.1 thorpej */
941 1.5 christos mask = std_mask(htonl(in.s_addr));
942 1.5 christos if ((~mask & in.s_addr) != 0)
943 1.1 thorpej mask = HOST_MASK;
944 1.1 thorpej } else {
945 1.1 thorpej mask = (naddr)strtoul(mname, &p, 0);
946 1.1 thorpej if (*p != '\0' || mask > 32)
947 1.1 thorpej return 0;
948 1.4 ws if (mask != 0)
949 1.4 ws mask = HOST_MASK << (32-mask);
950 1.1 thorpej }
951 1.5 christos
952 1.5 christos /* must have mask of 0 with default */
953 1.1 thorpej if (mask != 0 && in.s_addr == RIP_DEFAULT)
954 1.1 thorpej return 0;
955 1.5 christos /* no host bits allowed in a network number */
956 1.5 christos if ((~mask & in.s_addr) != 0)
957 1.5 christos return 0;
958 1.5 christos /* require non-zero network number */
959 1.5 christos if ((mask & in.s_addr) == 0 && in.s_addr != RIP_DEFAULT)
960 1.5 christos return 0;
961 1.5 christos if (in.s_addr>>24 == 0 && in.s_addr != RIP_DEFAULT)
962 1.5 christos return 0;
963 1.5 christos if (in.s_addr>>24 == 0xff)
964 1.1 thorpej return 0;
965 1.1 thorpej
966 1.5 christos *netp = in.s_addr;
967 1.1 thorpej *maskp = mask;
968 1.1 thorpej return 1;
969 1.1 thorpej }
970 1.1 thorpej
971 1.1 thorpej
972 1.1 thorpej int /* 0=bad */
973 1.1 thorpej gethost(char *name,
974 1.1 thorpej naddr *addrp)
975 1.1 thorpej {
976 1.1 thorpej struct hostent *hp;
977 1.1 thorpej struct in_addr in;
978 1.1 thorpej
979 1.1 thorpej
980 1.1 thorpej /* Try for a number first, even in IRIX where gethostbyname()
981 1.1 thorpej * is smart. This avoids hitting the name server which
982 1.1 thorpej * might be sick because routing is.
983 1.1 thorpej */
984 1.1 thorpej if (inet_aton(name, &in) == 1) {
985 1.5 christos /* get a good number, but check that it it makes some
986 1.5 christos * sense.
987 1.5 christos */
988 1.5 christos if (ntohl(in.s_addr)>>24 == 0
989 1.5 christos || ntohl(in.s_addr)>>24 == 0xff)
990 1.5 christos return 0;
991 1.1 thorpej *addrp = in.s_addr;
992 1.1 thorpej return 1;
993 1.1 thorpej }
994 1.1 thorpej
995 1.1 thorpej hp = gethostbyname(name);
996 1.1 thorpej if (hp) {
997 1.7 lukem memmove(addrp, hp->h_addr, sizeof(*addrp));
998 1.1 thorpej return 1;
999 1.1 thorpej }
1000 1.1 thorpej
1001 1.1 thorpej return 0;
1002 1.1 thorpej }
1003