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