dispatch.c revision 50479
1/*
2 * The new sysinstall program.
3 *
4 * This is probably the last program in the `sysinstall' line - the next
5 * generation being essentially a complete rewrite.
6 *
7 * $FreeBSD: head/usr.sbin/sade/dispatch.c 50479 1999-08-28 01:35:59Z peter $
8 *
9 * Copyright (c) 1995
10 *	Jordan Hubbard.  All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer,
17 *    verbatim and that no modifications are made prior to this
18 *    point in the file.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 *    notice, this list of conditions and the following disclaimer in the
21 *    documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 */
36
37#include "sysinstall.h"
38#include <ctype.h>
39#include <errno.h>
40#include <sys/signal.h>
41#include <sys/fcntl.h>
42
43#include "list.h"
44
45static int dispatch_shutdown(dialogMenuItem *unused);
46static int dispatch_systemExecute(dialogMenuItem *unused);
47static int dispatch_msgConfirm(dialogMenuItem *unused);
48
49static struct _word {
50    char *name;
51    int (*handler)(dialogMenuItem *self);
52} resWords[] = {
53    { "configAnonFTP",		configAnonFTP		},
54    { "configRouter",		configRouter		},
55    { "configNFSServer",	configNFSServer		},
56    { "configNTP",		configNTP		},
57    { "configPCNFSD",		configPCNFSD		},
58    { "configPackages",		configPackages		},
59    { "configUsers",		configUsers		},
60    { "configXSetup",		configXSetup	},
61    { "configXDesktop",		configXDesktop	},
62    { "diskPartitionEditor",	diskPartitionEditor	},
63    { "diskPartitionWrite",	diskPartitionWrite	},
64    { "diskLabelEditor",	diskLabelEditor		},
65    { "diskLabelCommit",	diskLabelCommit		},
66    { "distReset",		distReset		},
67    { "distSetCustom",		distSetCustom		},
68    { "distSetDeveloper",	distSetDeveloper	},
69    { "distSetXDeveloper",	distSetXDeveloper	},
70    { "distSetKernDeveloper",	distSetKernDeveloper	},
71    { "distSetUser",		distSetUser		},
72    { "distSetXUser",		distSetXUser		},
73    { "distSetMinimum",		distSetMinimum		},
74    { "distSetEverything",	distSetEverything	},
75    { "distSetDES",		distSetDES		},
76    { "distSetSrc",		distSetSrc		},
77    { "distSetXF86",		distSetXF86		},
78    { "distExtractAll",		distExtractAll		},
79    { "docBrowser",		docBrowser		},
80    { "docShowDocument",	docShowDocument		},
81    { "installCommit",		installCommit		},
82    { "installExpress",		installExpress		},
83    { "installNovice",		installNovice		},
84    { "installUpgrade",		installUpgrade		},
85    { "installFixupBin",	installFixupBin		},
86    { "installFixupXFree",	installFixupXFree	},
87    { "installFixitHoloShell",	installFixitHoloShell	},
88    { "installFixitCDROM",	installFixitCDROM	},
89    { "installFixitFloppy",	installFixitFloppy	},
90    { "installFilesystems",	installFilesystems	},
91    { "installVarDefaults",	installVarDefaults	},
92    { "loadConfig",		dispatch_load_file	},
93    { "loadFloppyConfig",	dispatch_load_floppy	},
94    { "mediaSetCDROM",		mediaSetCDROM		},
95    { "mediaSetFloppy",		mediaSetFloppy		},
96    { "mediaSetDOS",		mediaSetDOS		},
97    { "mediaSetTape",		mediaSetTape		},
98    { "mediaSetFTP",		mediaSetFTP		},
99    { "mediaSetFTPActive",	mediaSetFTPActive	},
100    { "mediaSetFTPPassive",	mediaSetFTPPassive	},
101    { "mediaSetUFS",		mediaSetUFS		},
102    { "mediaSetNFS",		mediaSetNFS		},
103    { "mediaSetFTPUserPass",	mediaSetFTPUserPass	},
104    { "mediaSetCPIOVerbosity",	mediaSetCPIOVerbosity	},
105    { "mediaGetType",		mediaGetType		},
106    { "msgConfirm",		dispatch_msgConfirm	},
107    { "optionsEditor",		optionsEditor		},
108    { "packageAdd",		packageAdd		},
109    { "addGroup",		userAddGroup		},
110    { "addUser",		userAddUser		},
111    { "shutdown",		dispatch_shutdown 	},
112    { "system",			dispatch_systemExecute	},
113    { "dumpVariables",		dump_variables		},
114    { "tcpMenuSelect",		tcpMenuSelect		},
115    { NULL, NULL },
116};
117
118/*
119 * Helper routines for buffering data.
120 *
121 * We read an entire configuration into memory before executing it
122 * so that we are truely standalone and can do things like nuke the
123 * file or disk we're working on.
124 */
125
126typedef struct command_buffer_ {
127    qelement	queue;
128    char *	string;
129} command_buffer;
130
131static void
132dispatch_free_command(command_buffer *item)
133{
134    REMQUE(item);
135    free(item->string);
136    free(item);
137}
138
139static void
140dispatch_free_all(qelement *head)
141{
142    command_buffer *item;
143
144    while (!EMPTYQUE(*head)) {
145	item = (command_buffer *) head->q_forw;
146	dispatch_free_command(item);
147    }
148}
149
150static command_buffer *
151dispatch_add_command(qelement *head, char *string)
152{
153    command_buffer *new;
154
155    new = malloc(sizeof(command_buffer));
156
157    if (!new)
158	return NULL;
159
160    new->string = strdup(string);
161    INSQUEUE(new, head->q_back);
162
163    return new;
164}
165
166/*
167 * Command processing
168 */
169
170/* Just convenience */
171static int
172dispatch_shutdown(dialogMenuItem *unused)
173{
174    systemShutdown(0);
175    return DITEM_FAILURE;
176}
177
178static int
179dispatch_systemExecute(dialogMenuItem *unused)
180{
181    char *cmd = variable_get(VAR_COMMAND);
182
183    if (cmd)
184	return systemExecute(cmd) ? DITEM_FAILURE : DITEM_SUCCESS;
185    else
186	msgDebug("_systemExecute: No command passed in `command' variable.\n");
187    return DITEM_FAILURE;
188}
189
190static int
191dispatch_msgConfirm(dialogMenuItem *unused)
192{
193    char *msg = variable_get(VAR_COMMAND);
194
195    if (msg) {
196	msgConfirm(msg);
197	return DITEM_SUCCESS;
198    }
199
200    msgDebug("_msgConfirm: No message passed in `command' variable.\n");
201    return DITEM_FAILURE;
202}
203
204static int
205call_possible_resword(char *name, dialogMenuItem *value, int *status)
206{
207    int i, rval;
208
209    rval = 0;
210    for (i = 0; resWords[i].name; i++) {
211	if (!strcmp(name, resWords[i].name)) {
212	    *status = resWords[i].handler(value);
213	    rval = 1;
214	    break;
215	}
216    }
217    return rval;
218}
219
220/* For a given string, call it or spit out an undefined command diagnostic */
221int
222dispatchCommand(char *str)
223{
224    int i;
225    char *cp;
226
227    if (!str || !*str) {
228	msgConfirm("Null or zero-length string passed to dispatchCommand");
229	return DITEM_FAILURE;
230    }
231    /* If it's got a newline, trim it */
232    if ((cp = index(str, '\n')) != NULL)
233	*cp = '\0';
234
235    /* If it's got a `=' sign in there, assume it's a variable setting */
236    if (index(str, '=')) {
237	if (isDebug())
238	    msgDebug("dispatch: setting variable `%s'\n", str);
239	variable_set(str, 0);
240	i = DITEM_SUCCESS;
241    }
242    else {
243	/* A command might be a pathname if it's encoded in argv[0], which
244	   we also support */
245	if ((cp = rindex(str, '/')) != NULL)
246	    str = cp + 1;
247	if (isDebug())
248	    msgDebug("dispatch: calling resword `%s'\n", str);
249	if (!call_possible_resword(str, NULL, &i)) {
250	    msgNotify("Warning: No such command ``%s''", str);
251	    i = DITEM_FAILURE;
252	}
253    }
254    return i;
255}
256
257
258/*
259 * File processing
260 */
261
262static qelement *
263dispatch_load_fp(FILE *fp)
264{
265    qelement *head;
266    char buf[BUFSIZ], *cp;
267
268    head = malloc(sizeof(qelement));
269
270    if (!head)
271	return NULL;
272
273    INITQUE(*head);
274
275    while (fgets(buf, sizeof buf, fp)) {
276
277	if ((cp = strchr(buf, '\n')) != NULL)
278	    *cp = '\0';
279	if (*buf == '\0' || *buf == '#')
280	    continue;
281
282	if (!dispatch_add_command(head, buf))
283	    return NULL;
284    }
285
286    return head;
287}
288
289static int
290dispatch_execute(qelement *head)
291{
292    int result = DITEM_SUCCESS;
293    command_buffer *item;
294    char *old_interactive;
295
296    if (!head)
297	return result | DITEM_FAILURE;
298
299    old_interactive = variable_get(VAR_NONINTERACTIVE);
300    if (old_interactive)
301	 old_interactive = strdup(old_interactive);	/* save copy */
302
303    /* Hint to others that we're running from a script, should they care */
304    variable_set2(VAR_NONINTERACTIVE, "yes", 0);
305
306    while (!EMPTYQUE(*head)) {
307	item = (command_buffer *) head->q_forw;
308
309	if (DITEM_STATUS(dispatchCommand(item->string)) != DITEM_SUCCESS) {
310	    /*
311	     * Allow a user to prefix a command with "noError" to cause
312	     * us to ignore any errors for that one command.
313	     */
314	    if (variable_get(VAR_NO_ERROR))
315		variable_unset(VAR_NO_ERROR);
316	    else {
317		msgConfirm("Command `%s' failed - rest of script aborted.\n",
318			   item->string);
319		result |= DITEM_FAILURE;
320		break;
321	    }
322	}
323	dispatch_free_command(item);
324    }
325
326    dispatch_free_all(head);
327
328    if (!old_interactive)
329	variable_unset(VAR_NONINTERACTIVE);
330    else {
331	variable_set2(VAR_NONINTERACTIVE, old_interactive, 0);
332	free(old_interactive);
333    }
334
335    return result;
336}
337
338int
339dispatch_load_file_int(int quiet)
340{
341    FILE *fp;
342    char *cp;
343    int  i;
344    qelement *list;
345
346    static const char *names[] = {
347	"install.cfg",
348	"/stand/install.cfg",
349	"/tmp/install.cfg",
350	NULL
351    };
352
353    fp = NULL;
354    cp = variable_get(VAR_CONFIG_FILE);
355    if (!cp) {
356	for (i = 0; names[i]; i++)
357	    if ((fp = fopen(names[i], "r")) != NULL)
358		break;
359    } else
360	fp = fopen(cp, "r");
361
362    if (!fp) {
363	if (!quiet)
364	    msgConfirm("Unable to open %s: %s", cp, strerror(errno));
365	return DITEM_FAILURE;
366    }
367
368    list = dispatch_load_fp(fp);
369    fclose(fp);
370
371    return dispatch_execute(list);
372}
373
374int
375dispatch_load_file(dialogMenuItem *self)
376{
377    return dispatch_load_file_int(FALSE);
378}
379
380int
381dispatch_load_floppy(dialogMenuItem *self)
382{
383    int             what = DITEM_RESTORE | DITEM_SUCCESS;
384    extern char    *distWanted;
385    char           *cp;
386    FILE           *fp;
387    qelement	   *list;
388
389    mediaClose();
390    dialog_clear_norefresh();
391
392    cp = variable_get_value(VAR_INSTALL_CFG,
393			    "Specify the name of a configuration file\n"
394			    "residing on a MSDOS or UFS floppy.", 0);
395    if (!cp || !*cp) {
396	variable_unset(VAR_INSTALL_CFG);
397	what |= DITEM_FAILURE;
398	return what;
399    }
400
401    distWanted = cp;
402    /* Try to open the floppy drive */
403    if (DITEM_STATUS(mediaSetFloppy(NULL)) == DITEM_FAILURE) {
404	msgConfirm("Unable to set media device to floppy.");
405	what |= DITEM_FAILURE;
406	mediaClose();
407	return what;
408    }
409
410    if (!mediaDevice->init(mediaDevice)) {
411	msgConfirm("Unable to mount floppy filesystem.");
412	what |= DITEM_FAILURE;
413	mediaClose();
414	return what;
415    }
416
417    fp = mediaDevice->get(mediaDevice, cp, TRUE);
418    if (fp) {
419	list = dispatch_load_fp(fp);
420	fclose(fp);
421	mediaClose();
422
423	what |= dispatch_execute(list);
424    }
425    else {
426	if (!variable_get(VAR_NO_ERROR))
427	    msgConfirm("Configuration file '%s' not found.", cp);
428	variable_unset(VAR_INSTALL_CFG);
429	what |= DITEM_FAILURE;
430	mediaClose();
431    }
432
433    return what;
434}
435
436