Hardwire i386 instead of using the borken machine variable
[dragonfly.git] / release / sysinstall / dhcp.c
1 /*
2  * $FreeBSD: src/release/sysinstall/dhcp.c,v 1.3.4.1 2001/09/16 23:05:50 murray Exp $
3  * $DragonFly: src/release/sysinstall/Attic/dhcp.c,v 1.2 2003/06/17 04:27:21 dillon Exp $
4  *
5  * Copyright (c) 1999
6  *      C. Stone.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer,
13  *    verbatim and that no modifications are made prior to this
14  *    point in the file.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY C. STONE ``AS IS'' AND ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL C STONE OR HIS BODILY PARASITES BE LIABLE FOR ANY DIRECT,
23  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE BY THE VOICES IN YOUR HEAD BEFOREHAND.
30  *
31  */
32
33 #include "sysinstall.h"
34
35 #include <ctype.h>
36
37 int
38 dhcpParseLeases(char *file, char *hostname, char *domain, char *nameserver,
39                 char *ipaddr, char *gateway, char *netmask)
40 {
41     char tempbuf[1024];
42     char optbuf[1024], *optname = NULL;
43     char *tptr;
44     int endedflag = 0;
45     int leaseflag = 0;
46     FILE *fp;
47     enum { P_NOSTMT, P_NOSTMT1, P_STMT, P_STMTLINE } state;
48
49     if ((fp = fopen(file, "r")) == NULL) {
50         msgDebug("error opening file %s: %s\n", file, strerror(errno));
51         return -1;
52     }
53
54     state = P_NOSTMT;
55     while (fscanf(fp, "%1023s", tempbuf) > 0) {
56         switch (state) {
57         case P_NOSTMT:
58             state = P_NOSTMT1;
59             if (!strncasecmp(tempbuf, "lease", 5)) {
60                 if (!leaseflag)
61                     leaseflag = 1;
62                 else {
63                     fclose(fp);
64                     return 0;
65                 }
66             }
67             break;
68
69         case P_NOSTMT1: 
70             if (tempbuf[0] != '{') {
71                 msgWarn("dhcpParseLeases: '{' expected");
72                 fclose(fp);
73                 return -1;
74             }
75             state = P_STMT;
76             break;
77
78         case P_STMT:
79             if (!strncasecmp("option", tempbuf, 6))
80                 continue;
81             if (tempbuf[0] == '}') {
82                 state = P_NOSTMT;
83                 leaseflag = 0;
84                 continue;
85             }
86             if (!leaseflag) 
87                 break;
88             if (tempbuf[0] == ';') {    /* play it safe */
89                 state = P_STMT;
90                 continue;
91             }
92             if ((tptr = (char *)strchr(tempbuf, ';')) && (*(tptr + 1) == 0)) {
93                 *tptr = NULL;
94                 endedflag = 1;
95             }
96             if (!isalnum(tempbuf[0])) {
97                 msgWarn("dhcpParseLeases: bad option");
98                 fclose(fp);
99                 return -1;
100             }
101             if (optname)
102                 free(optname);
103             optname = strdup(tempbuf);
104             if (endedflag) {
105                 state = P_STMT;
106                 endedflag = 0;
107                 continue;
108             }
109             state = P_STMTLINE;
110             break;
111
112         case P_STMTLINE:
113             if (tempbuf[0] == ';') {
114                 state = P_STMT;
115                 continue;
116             }
117             if ((tptr = (char *)strchr(tempbuf, ';')) && (*(tptr + 1) == 0)) {
118                 *tptr = NULL;
119                 endedflag = 1;
120             }
121             if (tempbuf[0] == '"') {
122                 if (sscanf(tempbuf, "\"%[^\" ]\"", optbuf) < 1) {
123                     msgWarn("dhcpParseLeases: bad option value");
124                     fclose(fp);
125                     return -1;
126                 }
127             }
128             else
129                 strcpy(optbuf, tempbuf);
130
131             if (!strcasecmp("host-name", optname)) {
132                 strcpy(hostname, optbuf);
133             } else if (!strcasecmp("domain-name", optname)) {
134                 strcpy(domain, optbuf);
135             } else if (!strcasecmp("fixed-address", optname)) {
136                 strcpy(ipaddr, optbuf);
137             } else if (!strcasecmp("routers", optname)) {
138                 if((tptr = (char *)strchr(optbuf, ',')))
139                     *tptr = NULL;
140                 strcpy(gateway, optbuf);
141             } else if (!strcasecmp("subnet-mask", optname)) {
142                 strcpy(netmask, optbuf);
143             } else if (!strcasecmp("domain-name-servers", optname)) {
144                 /* <jkh> ...one value per property */
145                 if((tptr = (char *)strchr(optbuf, ',')))
146                     *tptr = NULL;
147                 strcpy(nameserver, optbuf);
148             }
149             if (endedflag) {
150                 state = P_STMT;
151                 endedflag = 0;
152                 continue;
153             }
154             break;
155         }
156     }
157     fclose(fp);
158     return 0;
159 }