xref: /onnv-gate/usr/src/lib/libdhcpsvc/private/confopt.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * This module contains the functions implementing the interface to the
31*0Sstevel@tonic-gate  * /etc/inet/dhcpsvc.conf DHCP service configuration file.
32*0Sstevel@tonic-gate  */
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include <thread.h>
35*0Sstevel@tonic-gate #include <stdio.h>
36*0Sstevel@tonic-gate #include <stdlib.h>
37*0Sstevel@tonic-gate #include <unistd.h>
38*0Sstevel@tonic-gate #include <sys/types.h>
39*0Sstevel@tonic-gate #include <alloca.h>
40*0Sstevel@tonic-gate #include <string.h>
41*0Sstevel@tonic-gate #include <sys/stat.h>
42*0Sstevel@tonic-gate #include <fcntl.h>
43*0Sstevel@tonic-gate #include <errno.h>
44*0Sstevel@tonic-gate #include <dhcp_svc_confkey.h>
45*0Sstevel@tonic-gate #include <dhcp_svc_confopt.h>
46*0Sstevel@tonic-gate #include <dhcp_svc_private.h>
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate /*
49*0Sstevel@tonic-gate  * Finds the parameter called key, and returns a reference to it.  Returns
50*0Sstevel@tonic-gate  * NULL if not found or an error occurred.
51*0Sstevel@tonic-gate  */
52*0Sstevel@tonic-gate static dhcp_confopt_t *
find_dhcp_confopt(dhcp_confopt_t * ddp,const char * key)53*0Sstevel@tonic-gate find_dhcp_confopt(dhcp_confopt_t *ddp, const char *key)
54*0Sstevel@tonic-gate {
55*0Sstevel@tonic-gate 	unsigned int	i;
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate 	if (ddp == NULL || key == NULL)
58*0Sstevel@tonic-gate 		return (NULL);
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate 	for (i = 0; ddp[i].co_type != DHCP_END; i++) {
61*0Sstevel@tonic-gate 		if (ddp[i].co_type == DHCP_KEY &&
62*0Sstevel@tonic-gate 		    strcasecmp(ddp[i].co_key, key) == 0)
63*0Sstevel@tonic-gate 			return (&ddp[i]);
64*0Sstevel@tonic-gate 	}
65*0Sstevel@tonic-gate 	return (NULL);
66*0Sstevel@tonic-gate }
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate /*
69*0Sstevel@tonic-gate  * Adds a dhcp_confopt_t to the ddpp table. If the table is NULL, one is
70*0Sstevel@tonic-gate  * created. The table is terminated by a NULL entry.  The key and value
71*0Sstevel@tonic-gate  * arguments are copied, not referenced directly.  No check is done to see
72*0Sstevel@tonic-gate  * if the parameter already exists.  Returns 0 for success, nonzero
73*0Sstevel@tonic-gate  * otherwise.
74*0Sstevel@tonic-gate  */
75*0Sstevel@tonic-gate int
add_dsvc_conf(dhcp_confopt_t ** ddpp,const char * key,const char * value)76*0Sstevel@tonic-gate add_dsvc_conf(dhcp_confopt_t **ddpp, const char *key, const char *value)
77*0Sstevel@tonic-gate {
78*0Sstevel@tonic-gate 	dhcp_confopt_t		*ndp, tdp;
79*0Sstevel@tonic-gate 	unsigned int		i;
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 	if (ddpp == NULL || key == NULL || value == NULL) {
82*0Sstevel@tonic-gate 		errno = EINVAL;
83*0Sstevel@tonic-gate 		return (-1);
84*0Sstevel@tonic-gate 	}
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	tdp.co_key = strdup(key);
87*0Sstevel@tonic-gate 	tdp.co_type = DHCP_KEY;
88*0Sstevel@tonic-gate 	tdp.co_value = strdup(value);
89*0Sstevel@tonic-gate 	if (tdp.co_key == NULL || tdp.co_value == NULL) {
90*0Sstevel@tonic-gate 		free(tdp.co_key);
91*0Sstevel@tonic-gate 		free(tdp.co_value);
92*0Sstevel@tonic-gate 		errno = ENOMEM;
93*0Sstevel@tonic-gate 		return (-1);
94*0Sstevel@tonic-gate 	}
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	for (i = 0; *ddpp && (*ddpp)[i].co_key != NULL; i++)
97*0Sstevel@tonic-gate 		;
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	ndp = realloc(*ddpp, (i + 2) * sizeof (dhcp_confopt_t));
100*0Sstevel@tonic-gate 	if (ndp == NULL) {
101*0Sstevel@tonic-gate 		free(tdp.co_key);
102*0Sstevel@tonic-gate 		free(tdp.co_value);
103*0Sstevel@tonic-gate 		errno = ENOMEM;
104*0Sstevel@tonic-gate 		return (-1);
105*0Sstevel@tonic-gate 	}
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	ndp[i] = tdp;
108*0Sstevel@tonic-gate 	(void) memset(&ndp[i + 1], 0, sizeof (dhcp_confopt_t));
109*0Sstevel@tonic-gate 	*ddpp = ndp;
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	return (0);
112*0Sstevel@tonic-gate }
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate /*
115*0Sstevel@tonic-gate  * Reads the contents of the configuration file into a dynamically
116*0Sstevel@tonic-gate  * allocated array of dhcp_confopt_t records.  A zeroed element marks the
117*0Sstevel@tonic-gate  * end of the array.  Blank lines are ignored.  Caller is responsible for
118*0Sstevel@tonic-gate  * freeing ddp.
119*0Sstevel@tonic-gate  */
120*0Sstevel@tonic-gate int
read_dsvc_conf(dhcp_confopt_t ** ddpp)121*0Sstevel@tonic-gate read_dsvc_conf(dhcp_confopt_t **ddpp)
122*0Sstevel@tonic-gate {
123*0Sstevel@tonic-gate 	struct stat	sb;
124*0Sstevel@tonic-gate 	int		dd;
125*0Sstevel@tonic-gate 	int		error;
126*0Sstevel@tonic-gate 	unsigned int	entry;
127*0Sstevel@tonic-gate 	char		*cp, *dp, *eol, *value;
128*0Sstevel@tonic-gate 	dhcp_confopt_t	confopt, *tdp, *ddp = NULL;
129*0Sstevel@tonic-gate 	char		conf[MAXPATHLEN];
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	if (ddpp == NULL) {
132*0Sstevel@tonic-gate 		errno = EINVAL;
133*0Sstevel@tonic-gate 		return (-1);
134*0Sstevel@tonic-gate 	}
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	(void) snprintf(conf, sizeof (conf), "%s" DHCP_CONFOPT_FILE,
137*0Sstevel@tonic-gate 	    DHCP_CONFOPT_ROOT);
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	if ((dd = open(conf, O_RDONLY)) == -1)
140*0Sstevel@tonic-gate 		return (-1);
141*0Sstevel@tonic-gate 	if (fstat(dd, &sb) == -1) {
142*0Sstevel@tonic-gate 		error = errno;
143*0Sstevel@tonic-gate 		(void) close(dd);
144*0Sstevel@tonic-gate 		errno = error;
145*0Sstevel@tonic-gate 		return (-1);
146*0Sstevel@tonic-gate 	}
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	dp = alloca(sb.st_size);
149*0Sstevel@tonic-gate 	if (read(dd, dp, sb.st_size) != sb.st_size) {
150*0Sstevel@tonic-gate 		error = errno;
151*0Sstevel@tonic-gate 		(void) close(dd);
152*0Sstevel@tonic-gate 		errno = error;
153*0Sstevel@tonic-gate 		return (-1);
154*0Sstevel@tonic-gate 	}
155*0Sstevel@tonic-gate 	(void) close(dd);
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	for (entry = 0, cp = dp; cp < &dp[sb.st_size]; cp = eol + 1) {
158*0Sstevel@tonic-gate 		eol = strchr(cp, '\n');
159*0Sstevel@tonic-gate 		if (eol == NULL)		/* done parsing file */
160*0Sstevel@tonic-gate 			break;
161*0Sstevel@tonic-gate 		if (eol == cp) 			/* blank line -- skip */
162*0Sstevel@tonic-gate 			continue;
163*0Sstevel@tonic-gate 		*eol = '\0';
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 		if (*cp == '#') {
166*0Sstevel@tonic-gate 			confopt.co_type = DHCP_COMMENT;
167*0Sstevel@tonic-gate 			confopt.co_comment = strdup(cp + 1);
168*0Sstevel@tonic-gate 			if (confopt.co_comment == NULL)
169*0Sstevel@tonic-gate 				goto nomem;
170*0Sstevel@tonic-gate 		} else {
171*0Sstevel@tonic-gate 			value = strchr(cp, '=');
172*0Sstevel@tonic-gate 			if (value == NULL)
173*0Sstevel@tonic-gate 				continue;
174*0Sstevel@tonic-gate 			*value = '\0';
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 			confopt.co_type = DHCP_KEY;
177*0Sstevel@tonic-gate 			confopt.co_key = strdup(cp);
178*0Sstevel@tonic-gate 			if (confopt.co_key == NULL)
179*0Sstevel@tonic-gate 				goto nomem;
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 			confopt.co_value = strdup(value + 1);
182*0Sstevel@tonic-gate 			if (confopt.co_value == NULL) {
183*0Sstevel@tonic-gate 				free(confopt.co_key);
184*0Sstevel@tonic-gate 				goto nomem;
185*0Sstevel@tonic-gate 			}
186*0Sstevel@tonic-gate 		}
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 		/* always allocate a spare slot for the zeroed entry */
189*0Sstevel@tonic-gate 		tdp = realloc(ddp, (entry + 2) * sizeof (dhcp_confopt_t));
190*0Sstevel@tonic-gate 		if (tdp == NULL)
191*0Sstevel@tonic-gate 			goto nomem;
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 		tdp[entry] = confopt;
194*0Sstevel@tonic-gate 		(void) memset(&tdp[entry + 1], 0, sizeof (dhcp_confopt_t));
195*0Sstevel@tonic-gate 		ddp = tdp;
196*0Sstevel@tonic-gate 		entry++;
197*0Sstevel@tonic-gate 	}
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	if (ddp == NULL)
200*0Sstevel@tonic-gate 		return (-1);
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	*ddpp = ddp;
203*0Sstevel@tonic-gate 	return (0);
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate nomem:
206*0Sstevel@tonic-gate 	if (ddp != NULL)
207*0Sstevel@tonic-gate 		free_dsvc_conf(ddp);
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 	errno = ENOMEM;
210*0Sstevel@tonic-gate 	return (-1);
211*0Sstevel@tonic-gate }
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate /*
214*0Sstevel@tonic-gate  * If the requested parameter exists, replace its value with the new
215*0Sstevel@tonic-gate  * value. If it doesn't exist, then add the parameter with the new value.
216*0Sstevel@tonic-gate  * Returns 0 for success, -1 otherwise (errno is set).
217*0Sstevel@tonic-gate  */
218*0Sstevel@tonic-gate int
replace_dsvc_conf(dhcp_confopt_t ** ddpp,const char * key,const char * value)219*0Sstevel@tonic-gate replace_dsvc_conf(dhcp_confopt_t **ddpp, const char *key, const char *value)
220*0Sstevel@tonic-gate {
221*0Sstevel@tonic-gate 	dhcp_confopt_t	*tdp;
222*0Sstevel@tonic-gate 	int		err;
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 	if (ddpp == NULL || key == NULL || value == NULL) {
225*0Sstevel@tonic-gate 		errno = EINVAL;
226*0Sstevel@tonic-gate 		return (-1);
227*0Sstevel@tonic-gate 	}
228*0Sstevel@tonic-gate 	if ((tdp = find_dhcp_confopt(*ddpp, key)) != NULL) {
229*0Sstevel@tonic-gate 		char	*valp;
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 		if ((valp = strdup(value)) == NULL)
232*0Sstevel@tonic-gate 			return (-1); /* NOMEM */
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate 		if (tdp->co_value != NULL)
235*0Sstevel@tonic-gate 			free(tdp->co_value);
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 		tdp->co_value = valp;
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 		errno = 0;
240*0Sstevel@tonic-gate 		err = 0;
241*0Sstevel@tonic-gate 	} else
242*0Sstevel@tonic-gate 		err = (add_dsvc_conf(ddpp, key, value) == 0) ? 0 : -1;
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 	return (err);
245*0Sstevel@tonic-gate }
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate /*
248*0Sstevel@tonic-gate  * Writes ddp array to the configuration file.  If the configuration file
249*0Sstevel@tonic-gate  * already exists, its contents are replaced with the contents of the ddp
250*0Sstevel@tonic-gate  * array.  If the configuration file does not exist, it is created using
251*0Sstevel@tonic-gate  * the identity of the caller (euid/egid) with the permission bits
252*0Sstevel@tonic-gate  * specified by the mode argument (and modified by the umask).  Caller is
253*0Sstevel@tonic-gate  * responsible for freeing the array.
254*0Sstevel@tonic-gate  */
255*0Sstevel@tonic-gate int
write_dsvc_conf(dhcp_confopt_t * ddp,mode_t mode)256*0Sstevel@tonic-gate write_dsvc_conf(dhcp_confopt_t *ddp, mode_t mode)
257*0Sstevel@tonic-gate {
258*0Sstevel@tonic-gate 	int		tdd;
259*0Sstevel@tonic-gate 	ssize_t		bytes;
260*0Sstevel@tonic-gate 	size_t		i, size;
261*0Sstevel@tonic-gate 	char		*tmpbuf;
262*0Sstevel@tonic-gate 	char		tmpconf[MAXPATHLEN], conf[MAXPATHLEN];
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 	if (ddp == NULL) {
265*0Sstevel@tonic-gate 		errno = EINVAL;
266*0Sstevel@tonic-gate 		return (-1);
267*0Sstevel@tonic-gate 	}
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 	/* guess at final file size */
270*0Sstevel@tonic-gate 	for (i = 0, size = 0; ddp[i].co_type != DHCP_END; i++) {
271*0Sstevel@tonic-gate 		if (ddp[i].co_type == DHCP_KEY) {
272*0Sstevel@tonic-gate 			size += strlen(ddp[i].co_key) + 1; /* include = */
273*0Sstevel@tonic-gate 			size += strlen(ddp[i].co_value) + 1; /* include \n */
274*0Sstevel@tonic-gate 		} else
275*0Sstevel@tonic-gate 			size += strlen(ddp[i].co_comment) + 2; /* inc # + \n */
276*0Sstevel@tonic-gate 	}
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 	if (size == 0) {
279*0Sstevel@tonic-gate 		errno = EINVAL;
280*0Sstevel@tonic-gate 		return (-1);
281*0Sstevel@tonic-gate 	}
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate 	(void) snprintf(conf, sizeof (conf), "%s" DHCP_CONFOPT_FILE,
284*0Sstevel@tonic-gate 	    DHCP_CONFOPT_ROOT);
285*0Sstevel@tonic-gate 	(void) snprintf(tmpconf, sizeof (tmpconf),
286*0Sstevel@tonic-gate 	    "%s" DHCP_CONFOPT_FILE ".%ld.%u", DHCP_CONFOPT_ROOT, getpid(),
287*0Sstevel@tonic-gate 	    thr_self());
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate 	if ((tdd = open(tmpconf, O_CREAT | O_EXCL | O_WRONLY, mode)) < 0)
290*0Sstevel@tonic-gate 		return (-1);
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 	tmpbuf = alloca(size);
293*0Sstevel@tonic-gate 	for (i = 0; ddp[i].co_type != DHCP_END; i++) {
294*0Sstevel@tonic-gate 		if (ddp[i].co_type == DHCP_KEY)
295*0Sstevel@tonic-gate 			(void) snprintf(tmpbuf, size, "%s=%s\n", ddp[i].co_key,
296*0Sstevel@tonic-gate 			    ddp[i].co_value);
297*0Sstevel@tonic-gate 		else
298*0Sstevel@tonic-gate 			(void) snprintf(tmpbuf, size, "#%s\n",
299*0Sstevel@tonic-gate 			    ddp[i].co_comment);
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate 		bytes = write(tdd, tmpbuf, strlen(tmpbuf));
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate 		/* Nuke the file if we can't successfully update it */
304*0Sstevel@tonic-gate 		if (bytes != strlen(tmpbuf)) {
305*0Sstevel@tonic-gate 			(void) close(tdd);
306*0Sstevel@tonic-gate 			(void) unlink(tmpconf);
307*0Sstevel@tonic-gate 			return (-1);
308*0Sstevel@tonic-gate 		}
309*0Sstevel@tonic-gate 	}
310*0Sstevel@tonic-gate 	(void) close(tdd);
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate 	/* Move new file into place */
313*0Sstevel@tonic-gate 	if (rename(tmpconf, conf) < 0) {
314*0Sstevel@tonic-gate 		(void) unlink(tmpconf);
315*0Sstevel@tonic-gate 		return (-1);
316*0Sstevel@tonic-gate 	}
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate 	return (0);
319*0Sstevel@tonic-gate }
320*0Sstevel@tonic-gate 
321*0Sstevel@tonic-gate /*
322*0Sstevel@tonic-gate  * Frees the memory associated with the ddp array.
323*0Sstevel@tonic-gate  */
324*0Sstevel@tonic-gate void
free_dsvc_conf(dhcp_confopt_t * ddp)325*0Sstevel@tonic-gate free_dsvc_conf(dhcp_confopt_t *ddp)
326*0Sstevel@tonic-gate {
327*0Sstevel@tonic-gate 	unsigned int	i;
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate 	if (ddp == NULL)
330*0Sstevel@tonic-gate 		return;
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate 	for (i = 0; ddp[i].co_type != DHCP_END; i++) {
333*0Sstevel@tonic-gate 		if (ddp[i].co_type == DHCP_KEY) {
334*0Sstevel@tonic-gate 			free(ddp[i].co_key);
335*0Sstevel@tonic-gate 			free(ddp[i].co_value);
336*0Sstevel@tonic-gate 		} else
337*0Sstevel@tonic-gate 			free(ddp[i].co_comment);
338*0Sstevel@tonic-gate 	}
339*0Sstevel@tonic-gate 	free(ddp);
340*0Sstevel@tonic-gate }
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate /*
343*0Sstevel@tonic-gate  * Deletes the configuration file.
344*0Sstevel@tonic-gate  */
345*0Sstevel@tonic-gate int
delete_dsvc_conf(void)346*0Sstevel@tonic-gate delete_dsvc_conf(void)
347*0Sstevel@tonic-gate {
348*0Sstevel@tonic-gate 	char confpath[MAXPATHLEN];
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate 	(void) snprintf(confpath, sizeof (confpath), "%s" DHCP_CONFOPT_FILE,
351*0Sstevel@tonic-gate 	    DHCP_CONFOPT_ROOT);
352*0Sstevel@tonic-gate 	return (unlink(confpath));
353*0Sstevel@tonic-gate }
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate /*
356*0Sstevel@tonic-gate  * Return a copy of the value portion of the named key.  Caller is
357*0Sstevel@tonic-gate  * responsible for freeing value when they're finished using it.  Returns 0
358*0Sstevel@tonic-gate  * for success, -1 otherwise (errno is set).
359*0Sstevel@tonic-gate  */
360*0Sstevel@tonic-gate int
query_dsvc_conf(dhcp_confopt_t * ddp,const char * key,char ** value)361*0Sstevel@tonic-gate query_dsvc_conf(dhcp_confopt_t *ddp, const char *key, char **value)
362*0Sstevel@tonic-gate {
363*0Sstevel@tonic-gate 	dhcp_confopt_t	*tdp;
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate 	if (key == NULL || value == NULL) {
366*0Sstevel@tonic-gate 		errno = EINVAL;
367*0Sstevel@tonic-gate 		return (-1);
368*0Sstevel@tonic-gate 	}
369*0Sstevel@tonic-gate 	if ((tdp = find_dhcp_confopt(ddp, key)) != NULL) {
370*0Sstevel@tonic-gate 		*value = strdup(tdp->co_value);
371*0Sstevel@tonic-gate 		if (*value == NULL) {
372*0Sstevel@tonic-gate 			errno = ENOMEM;
373*0Sstevel@tonic-gate 			return (-1);
374*0Sstevel@tonic-gate 		}
375*0Sstevel@tonic-gate 		errno = 0;
376*0Sstevel@tonic-gate 		return (0);
377*0Sstevel@tonic-gate 	}
378*0Sstevel@tonic-gate 	errno = ENOENT;
379*0Sstevel@tonic-gate 	*value = NULL;
380*0Sstevel@tonic-gate 	return (-1);
381*0Sstevel@tonic-gate }
382*0Sstevel@tonic-gate 
383*0Sstevel@tonic-gate /*
384*0Sstevel@tonic-gate  * Given a dhcp_confopt_t structure, fill in a dsvc_datastore_t.
385*0Sstevel@tonic-gate  * Data is copied from dhcp_confopt_t structure.
386*0Sstevel@tonic-gate  */
387*0Sstevel@tonic-gate int
confopt_to_datastore(dhcp_confopt_t * ddp,dsvc_datastore_t * dsp)388*0Sstevel@tonic-gate confopt_to_datastore(dhcp_confopt_t *ddp, dsvc_datastore_t *dsp)
389*0Sstevel@tonic-gate {
390*0Sstevel@tonic-gate 	dhcp_confopt_t	*tdp;
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate 	if (ddp == NULL || dsp == NULL)
393*0Sstevel@tonic-gate 		return (DSVC_INVAL);
394*0Sstevel@tonic-gate 
395*0Sstevel@tonic-gate 	tdp = find_dhcp_confopt(ddp, DSVC_CK_CONVER);
396*0Sstevel@tonic-gate 	if (tdp == NULL || tdp->co_value == NULL)
397*0Sstevel@tonic-gate 		return (DSVC_BAD_CONVER);
398*0Sstevel@tonic-gate 	dsp->d_conver = atoi(tdp->co_value);
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 	if (query_dsvc_conf(ddp, DSVC_CK_RESOURCE, &dsp->d_resource) == -1)
401*0Sstevel@tonic-gate 		return (DSVC_BAD_RESOURCE);
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate 	if (query_dsvc_conf(ddp, DSVC_CK_PATH, &dsp->d_location) == -1) {
404*0Sstevel@tonic-gate 		free(dsp->d_resource);
405*0Sstevel@tonic-gate 		return (DSVC_BAD_PATH);
406*0Sstevel@tonic-gate 	}
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate 	/*
409*0Sstevel@tonic-gate 	 * RESOURCE_CONFIG is optional - underlying service will complain
410*0Sstevel@tonic-gate 	 * if it isn't right.
411*0Sstevel@tonic-gate 	 */
412*0Sstevel@tonic-gate 	(void) query_dsvc_conf(ddp, DSVC_CK_RESOURCE_CONFIG, &dsp->d_config);
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate 	return (DSVC_SUCCESS);
415*0Sstevel@tonic-gate }
416