xref: /illumos-gate/usr/src/cmd/svr4pkg/libinst/setadmin.c (revision 8119dad84d6416f13557b0ba8e2aaf9064cbcfd3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2017 Peter Tribble.
24  */
25 
26 /*
27  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
32 /* All Rights Reserved */
33 
34 
35 #include <stdio.h>
36 #include <limits.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <pkglocs.h>
42 #include <locale.h>
43 #include <libintl.h>
44 #include <pkglib.h>
45 #include <install.h>
46 #include <libinst.h>
47 #include <libadm.h>
48 #include <messages.h>
49 
50 #define	DEFMAIL	"root"
51 
52 extern struct admin	adm;		/* holds info about install admin */
53 extern int		warnflag;	/* != 0 non-fatal error occurred 2 */
54 
55 static struct {
56 	char	**memloc;
57 	char	*tag;
58 } admlist[] = {
59 	&adm.action,		"action",
60 	&adm.basedir,		"basedir",
61 	&adm.conflict,		"conflict",
62 	&adm.idepend,		"idepend",
63 	&adm.instance,		"instance",
64 	&adm.mail,		"mail",
65 	&adm.partial,		"partial",
66 	&adm.rdepend,		"rdepend",
67 	&adm.RSCRIPTALT,	RSCRIPTALT_KEYWORD,
68 	&adm.runlevel,		"runlevel",
69 	&adm.setuid,		"setuid",
70 	&adm.space,		"space",
71 	/* MUST BE LAST ENTRY IN LIST */
72 	(char **)NULL,		(char *)NULL
73 };
74 
75 /*
76  * Name:	setadminSetting
77  * Description:	set one administration parameter setting
78  * Arguments:	a_paramName - pointer to string representing the name of
79  *			the administration parameter to set
80  *		a_paramValue - pointer to string representing the value
81  *			to set the specified administration parameter to
82  * Returns:	char *
83  *			- old value the parameter had before being set
84  *			== NULL - the old paramter was not set
85  */
86 
87 char *
88 setadminSetting(char *a_paramName, char *a_paramValue)
89 {
90 	char	*oldValue = (char *)NULL;
91 	int	i;
92 
93 	/* locate and update the specified admin setting */
94 
95 	for (i = 0; admlist[i].memloc; i++) {
96 		if (strcmp(a_paramName, admlist[i].tag) == 0) {
97 			oldValue = *admlist[i].memloc;
98 			*admlist[i].memloc = a_paramValue;
99 			break;
100 		}
101 	}
102 
103 	if (admlist[i].memloc == (char **)NULL) {
104 		logerr(WRN_UNKNOWN_ADM_PARAM, a_paramName);
105 	}
106 
107 	return (oldValue);
108 }
109 
110 /*
111  * Name:	setadminFile
112  * Description:	read and remember settings from administration file
113  * Arguments:	file - pointer to string representing the path to the
114  *			administration file to read - if this is NULL
115  *			then the name "default" is used - if this is
116  *			the string "none" then the admin "basedir"
117  *			setting is set to "ask" so that the location
118  *			of the administration file will be interactively
119  *			asked at the appropriate time
120  * Returns:	void
121  */
122 
123 void
124 setadminFile(char *file)
125 {
126 	FILE	*fp;
127 	int	i;
128 	char	param[MAX_PKG_PARAM_LENGTH];
129 	char	*value;
130 	char	path[PATH_MAX];
131 	int	mail = 0;
132 
133 	if (file == NULL)
134 		file = "default";
135 	else if (strcmp(file, "none") == 0) {
136 		adm.basedir = "ask";
137 		return;
138 	}
139 
140 	if (file[0] == '/')
141 		(void) strcpy(path, file);
142 	else {
143 		(void) snprintf(path, sizeof (path), "%s/admin/%s",
144 				get_PKGADM(), file);
145 		if (access(path, R_OK)) {
146 			(void) snprintf(path, sizeof (path), "%s/admin/%s",
147 				PKGADM, file);
148 		}
149 	}
150 
151 	if ((fp = fopen(path, "r")) == NULL) {
152 		progerr(ERR_OPEN_ADMIN_FILE, file, strerror(errno));
153 		quit(99);
154 	}
155 
156 	param[0] = '\0';
157 	while (value = fpkgparam(fp, param)) {
158 		if (strcmp(param, "mail") == 0) {
159 			mail = 1;
160 		}
161 		if (value[0] == '\0') {
162 			param[0] = '\0';
163 			continue; /* same as not being set at all */
164 		}
165 		for (i = 0; admlist[i].memloc; i++) {
166 			if (strcmp(param, admlist[i].tag) == 0) {
167 				*admlist[i].memloc = value;
168 				break;
169 			}
170 		}
171 		if (admlist[i].memloc == NULL) {
172 			logerr(WRN_UNKNOWN_ADM_PARAM, param);
173 			free(value);
174 		}
175 		param[0] = '\0';
176 	}
177 
178 	(void) fclose(fp);
179 
180 	if (!mail) {
181 		adm.mail = DEFMAIL; 	/* if we don't assign anything to it */
182 	}
183 }
184