148893Sjkh/* 250479Speter * $FreeBSD$ 348893Sjkh * 448893Sjkh * Copyright (c) 1999 548893Sjkh * C. Stone. All rights reserved. 648893Sjkh * 748893Sjkh * Redistribution and use in source and binary forms, with or without 848893Sjkh * modification, are permitted provided that the following conditions 948893Sjkh * are met: 1048893Sjkh * 1. Redistributions of source code must retain the above copyright 1148893Sjkh * notice, this list of conditions and the following disclaimer, 1248893Sjkh * verbatim and that no modifications are made prior to this 1348893Sjkh * point in the file. 1448893Sjkh * 2. Redistributions in binary form must reproduce the above copyright 1548893Sjkh * notice, this list of conditions and the following disclaimer in the 1648893Sjkh * documentation and/or other materials provided with the distribution. 1748893Sjkh * 1848893Sjkh * THIS SOFTWARE IS PROVIDED BY C. STONE ``AS IS'' AND ANY EXPRESS OR IMPLIED 1948893Sjkh * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 2048893Sjkh * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2148893Sjkh * IN NO EVENT SHALL C STONE OR HIS BODILY PARASITES BE LIABLE FOR ANY DIRECT, 2248893Sjkh * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 2348893Sjkh * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2448893Sjkh * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION) 2548893Sjkh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2648893Sjkh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2748893Sjkh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2848893Sjkh * SUCH DAMAGE BY THE VOICES IN YOUR HEAD BEFOREHAND. 2948893Sjkh * 3048893Sjkh */ 3148893Sjkh 3248893Sjkh#include "sysinstall.h" 3348893Sjkh 3448893Sjkh#include <ctype.h> 3548893Sjkh 3648893Sjkhint 3748893SjkhdhcpParseLeases(char *file, char *hostname, char *domain, char *nameserver, 3848893Sjkh char *ipaddr, char *gateway, char *netmask) 3948893Sjkh{ 4048893Sjkh char tempbuf[1024]; 4148893Sjkh char optbuf[1024], *optname = NULL; 4248893Sjkh char *tptr; 4348893Sjkh int endedflag = 0; 4448893Sjkh int leaseflag = 0; 4548893Sjkh FILE *fp; 4648893Sjkh enum { P_NOSTMT, P_NOSTMT1, P_STMT, P_STMTLINE } state; 4748893Sjkh 4848893Sjkh if ((fp = fopen(file, "r")) == NULL) { 4948893Sjkh msgDebug("error opening file %s: %s\n", file, strerror(errno)); 5048893Sjkh return -1; 5148893Sjkh } 5248893Sjkh 5348893Sjkh state = P_NOSTMT; 5448893Sjkh while (fscanf(fp, "%1023s", tempbuf) > 0) { 5548893Sjkh switch (state) { 5648893Sjkh case P_NOSTMT: 5748893Sjkh state = P_NOSTMT1; 5848893Sjkh if (!strncasecmp(tempbuf, "lease", 5)) { 5948893Sjkh if (!leaseflag) 6048893Sjkh leaseflag = 1; 6148893Sjkh else { 6248893Sjkh fclose(fp); 6348893Sjkh return 0; 6448893Sjkh } 6548893Sjkh } 6648893Sjkh break; 6748893Sjkh 6848893Sjkh case P_NOSTMT1: 6948893Sjkh if (tempbuf[0] != '{') { 7048893Sjkh msgWarn("dhcpParseLeases: '{' expected"); 7148893Sjkh fclose(fp); 7248893Sjkh return -1; 7348893Sjkh } 7448893Sjkh state = P_STMT; 7548893Sjkh break; 7648893Sjkh 7748893Sjkh case P_STMT: 7848893Sjkh if (!strncasecmp("option", tempbuf, 6)) 7948893Sjkh continue; 8048893Sjkh if (tempbuf[0] == '}') { 8148893Sjkh state = P_NOSTMT; 8248893Sjkh leaseflag = 0; 8348893Sjkh continue; 8448893Sjkh } 8548893Sjkh if (!leaseflag) 8648893Sjkh break; 8748893Sjkh if (tempbuf[0] == ';') { /* play it safe */ 8848893Sjkh state = P_STMT; 8948893Sjkh continue; 9048893Sjkh } 9148893Sjkh if ((tptr = (char *)strchr(tempbuf, ';')) && (*(tptr + 1) == 0)) { 92126844Sbde *tptr = '\0'; 9348893Sjkh endedflag = 1; 9448893Sjkh } 9548893Sjkh if (!isalnum(tempbuf[0])) { 9648893Sjkh msgWarn("dhcpParseLeases: bad option"); 9748893Sjkh fclose(fp); 9848893Sjkh return -1; 9948893Sjkh } 10048893Sjkh if (optname) 10148893Sjkh free(optname); 10248893Sjkh optname = strdup(tempbuf); 10348893Sjkh if (endedflag) { 10448893Sjkh state = P_STMT; 10548893Sjkh endedflag = 0; 10648893Sjkh continue; 10748893Sjkh } 10848893Sjkh state = P_STMTLINE; 10948893Sjkh break; 11048932Sjkh 11148893Sjkh case P_STMTLINE: 11248893Sjkh if (tempbuf[0] == ';') { 11348893Sjkh state = P_STMT; 11448893Sjkh continue; 11548893Sjkh } 11648893Sjkh if ((tptr = (char *)strchr(tempbuf, ';')) && (*(tptr + 1) == 0)) { 117126844Sbde *tptr = '\0'; 11848893Sjkh endedflag = 1; 11948893Sjkh } 12048893Sjkh if (tempbuf[0] == '"') { 12148893Sjkh if (sscanf(tempbuf, "\"%[^\" ]\"", optbuf) < 1) { 12248893Sjkh msgWarn("dhcpParseLeases: bad option value"); 12348893Sjkh fclose(fp); 12448893Sjkh return -1; 12548893Sjkh } 12648893Sjkh } 12748893Sjkh else 12848893Sjkh strcpy(optbuf, tempbuf); 12948893Sjkh 13048932Sjkh if (!strcasecmp("host-name", optname)) { 13148893Sjkh strcpy(hostname, optbuf); 13248893Sjkh } else if (!strcasecmp("domain-name", optname)) { 13348893Sjkh strcpy(domain, optbuf); 13448893Sjkh } else if (!strcasecmp("fixed-address", optname)) { 13548893Sjkh strcpy(ipaddr, optbuf); 13648893Sjkh } else if (!strcasecmp("routers", optname)) { 13783144Smurray if((tptr = (char *)strchr(optbuf, ','))) 138126844Sbde *tptr = '\0'; 13948893Sjkh strcpy(gateway, optbuf); 14048893Sjkh } else if (!strcasecmp("subnet-mask", optname)) { 14148893Sjkh strcpy(netmask, optbuf); 14248893Sjkh } else if (!strcasecmp("domain-name-servers", optname)) { 14348893Sjkh /* <jkh> ...one value per property */ 14448893Sjkh if((tptr = (char *)strchr(optbuf, ','))) 145126844Sbde *tptr = '\0'; 14648893Sjkh strcpy(nameserver, optbuf); 14748893Sjkh } 14848893Sjkh if (endedflag) { 14948893Sjkh state = P_STMT; 15048893Sjkh endedflag = 0; 15148893Sjkh continue; 15248893Sjkh } 15348893Sjkh break; 15448893Sjkh } 15548893Sjkh } 15648893Sjkh fclose(fp); 15748893Sjkh return 0; 15848893Sjkh} 159