/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#pragma ident "%Z%%M% %I% %E% SMI"
#include "mt.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <ulimit.h>
#include <wait.h>
#include <stropts.h>
#include <ctype.h>
#include <errno.h>
#include <signal.h>
#include "sac.h"
#define NOWAIT 0
static char *eatwhite(char *);
static int doassign(char *);
static int dopush(int, char *);
static int dopop(int, char *);
static int dorun(char *, int);
/*
* doconfig - the configuration script interpreter, if all is ok,
* return 0. If there is a "system" error, return -1.
* If there is an error performing a command, or there
* is a syntax error, return the line number in error.
*
* args: fd - file descriptor to push and pop from
* script - name of the configuration script
* rflag - restriction flag to determine what "commands"
* can be run
*/
int
{
char *p; /* scratch pointer */
/* if the script does not exist, then there is nothing to do */
return (0);
return (-1);
line = 0;
line++;
/* if no \n, then line is too long */
if (p == NULL) {
return (line);
}
*p = '\0';
/* remove comments */
if (p)
*p = '\0';
/* remove leading whitespace */
/* see if anything is left */
if (*bp == '\0')
continue;
/* remove trailing whitespace */
while (*p && isspace(*p))
*p-- = '\0';
/* get the command */
p = bp;
while (*p && !isspace(*p))
p++;
if (*p)
*p++ = '\0';
/* skip any whitespace here too (between command and args) */
p = eatwhite(p);
return (line);
}
return (line);
}
return (line);
}
return (line);
}
return (line);
}
} else {
/* unknown command */
return (line);
}
}
return (-1);
}
return (0);
}
/*
* doassign - handle an `assign' command
*
* args: p - assignment string
*/
static int
doassign(char *p)
{
if (*p == '\0')
return (-1);
var = p;
/* skip first token, but stop if we see a '=' */
while (*p && !isspace(*p) && (*p != '='))
p++;
/* if we found end of string, it's an error */
if (*p == '\0')
return (-1);
/* if we found a space, look for the '=', otherwise it's an error */
if (isspace(*p)) {
*p++ = '\0';
while (*p && isspace(*p))
p++;
if (*p == '\0')
return (-1);
if (*p == '=')
p++;
else
return (-1);
} else {
/* skip over '=' */
*p = '\0';
p++;
}
/* skip over any whitespace */
p = eatwhite(p);
if (*p == '\'' || *p == '"') {
/* handle quoted values */
delim = *p++;
for (;;) {
if (*p == '\0') {
return (-1);
} else if (*p == delim) {
if (*(p - 1) != '\\')
break;
else
*(tp - 1) = *p++;
} else
*tp++ = *p++;
}
*tp = '\0';
/*
* these assignments make the comment below true
* (values of tp and p
*/
tp = ++p;
p = val;
} else {
tp = p;
/* look for end of token */
tp++;
}
/*
* at this point, p points to the value, and tp points to the
* end of the token. check to make sure there is no garbage on
* the end of the line
*/
if (*tp)
return (-1);
/* note: need to malloc fresh space so putenv works */
return (-1);
return (-1);
return (0);
}
/*
* dopush - handle a `push' command
*
* args: fd - file descriptor to push on
* p - list of modules to push
*/
static int
{
int i; /* scratch variable */
if (*p == '\0')
return (-1);
npush = 0;
for (;;) {
if (*p == '\0') /* found end of line */
return (0);
p = eatwhite(p);
if (*p == '\0')
return (-1);
tp = p;
tp++;
if (*tp)
*tp++ = '\0';
/*
* try to pop all that we've done, if pop fails it doesn't matter because
* nothing can be done anyhow
*/
for (i = 0; i < npush; ++i)
return (-1);
}
/* count the number of modules we've pushed */
npush++;
p = tp;
}
}
/*
* dopop - handle a `pop' command
*
* args: fd - file descriptor to pop from
* p - name of module to pop to or ALL (null means pop top only)
*/
static int
{
if (*p == '\0') {
/* just a pop with no args */
return (-1);
return (0);
}
/* skip any whitespace in between */
p = eatwhite(p);
modp = p;
/* find end of module name */
while (*p && !isspace(*p))
p++;
if (*p) /* if not end of line, extra junk on line */
return (-1);
/* it's the magic name, pop them all */
;
/* After all popped, we'll get an EINVAL, which is expected */
return (-1);
return (0);
}
/* check to see if the named module is on the stream */
return (-1);
/* pop them until the right one is on top */
for (;;) {
return (-1);
/* we're done */
return (0);
return (-1);
}
/* NOTREACHED */
}
/*
* dorun - handle a `run' command
*
* args: p - command line to run
* waitflag - flag indicating whether a wait should be done
*/
static int
{
void (*func)(); /* return from signal */
if (*p == '\0')
return (-1);
/*
* get first token
*/
;
savech = '\0';
if (*tp) {
*tp = '\0';
}
/*
* look for built-in's
*/
if (strcmp(p, "cd") == 0) {
if (*tp == '\0')
/* if nothing there, try to cd to $HOME */
return (-1);
} else if (strcmp(p, "ulimit") == 0) {
/* must have an argument */
if (*tp == '\0')
return (-1);
/* make sure nothing appears on line after arg */
;
if (*ep)
return (-1);
return (-1);
return (-1);
} else if (strcmp(p, "umask") == 0) {
/* must have an argument */
if (*tp == '\0')
return (-1);
/* make sure nothing appears on line after arg */
;
if (*ep)
return (-1);
return (-1);
} else {
/* not a built-in */
return (-1);
}
if (pid) {
status = 0;
rpid = -1;
if (status) {
/* child failed */
return (-1);
}
}
} else {
/* set IFS for security */
(void) putenv("IFS=\" \"");
/*
* need to close all files to prevent unauthorized
* access in the children. Setup stdin, stdout,
*/
closefrom(0);
/* stdin */
return (-1);
/* stdout */
if (dup(0) != 1)
return (-1);
/* stderr */
if (dup(0) != 2)
return (-1);
/*
* if we get here, there is a problem - remember that
* this is the child
*/
exit(1);
}
}
return (0);
}
/*
* eatwhite - swallow any leading whitespace, return pointer to first
* non-white space character or to terminating null character
* if nothing else is there
*
* args: p - string to parse
*/
static char *
eatwhite(char *p)
{
while (*p && isspace(*p))
p++;
return (p);
}