#include "config.h"
#include <sys/queue.h>
#include <sys/stat.h>
#include <bitstring.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "../common/common.h"
#include "tag.h"
#include "pathnames.h"
enum rc { NOEXIST, NOPERM, RCOK };
static enum rc exrc_isok(SCR *, struct stat *, int *, char *, int, int);
static int ex_run_file(SCR *, int, char *);
int
ex_screen_copy(SCR *orig, SCR *sp)
{
EX_PRIVATE *oexp, *nexp;
CALLOC_RET(orig, nexp, 1, sizeof(EX_PRIVATE));
sp->ex_private = nexp;
TAILQ_INIT(&nexp->tq);
TAILQ_INIT(&nexp->tagfq);
if (orig == NULL) {
} else {
oexp = EXP(orig);
if (oexp->lastbcomm != NULL &&
(nexp->lastbcomm = strdup(oexp->lastbcomm)) == NULL) {
msgq(sp, M_SYSERR, NULL);
return(1);
}
if (ex_tag_copy(orig, sp))
return (1);
}
return (0);
}
int
ex_screen_end(SCR *sp)
{
EX_PRIVATE *exp;
int rval;
if ((exp = EXP(sp)) == NULL)
return (0);
rval = 0;
if (F_ISSET(sp, SC_SCRIPT) && sscr_end(sp))
rval = 1;
if (argv_free(sp))
rval = 1;
free(exp->ibp);
free(exp->lastbcomm);
if (ex_tag_free(sp))
rval = 1;
free(exp);
sp->ex_private = NULL;
return (rval);
}
int
ex_optchange(SCR *sp, int offset, char *str, u_long *valp)
{
switch (offset) {
case O_TAGS:
return (ex_tagf_alloc(sp, str));
}
return (0);
}
int
ex_exrc(SCR *sp)
{
struct stat hsb, lsb;
char *p, path[PATH_MAX];
int fd;
switch (exrc_isok(sp, &hsb, &fd, _PATH_SYSEXRC, 1, 0)) {
case NOEXIST:
case NOPERM:
break;
case RCOK:
if (ex_run_file(sp, fd, _PATH_SYSEXRC))
return (1);
break;
}
if (EXCMD_RUNNING(sp->gp))
(void)ex_cmd(sp);
if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE))
return (0);
if ((p = getenv("NEXINIT")) != NULL) {
if (ex_run_str(sp, "NEXINIT", p, strlen(p), 1, 0))
return (1);
} else if ((p = getenv("EXINIT")) != NULL) {
if (ex_run_str(sp, "EXINIT", p, strlen(p), 1, 0))
return (1);
} else if ((p = getenv("HOME")) != NULL && *p) {
(void)snprintf(path, sizeof(path), "%s/%s", p, _PATH_NEXRC);
switch (exrc_isok(sp, &hsb, &fd, path, 0, 1)) {
case NOEXIST:
(void)snprintf(path,
sizeof(path), "%s/%s", p, _PATH_EXRC);
if (exrc_isok(sp, &hsb, &fd, path, 0, 1) == RCOK &&
ex_run_file(sp, fd, path))
return (1);
break;
case NOPERM:
break;
case RCOK:
if (ex_run_file(sp, fd, path))
return (1);
break;
}
}
if (EXCMD_RUNNING(sp->gp))
(void)ex_cmd(sp);
if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE))
return (0);
if (O_ISSET(sp, O_EXRC)) {
switch (exrc_isok(sp, &lsb, &fd, _PATH_NEXRC, 0, 0)) {
case NOEXIST:
if (exrc_isok(sp, &lsb, &fd, _PATH_EXRC, 0, 0)
== RCOK) {
if (lsb.st_dev != hsb.st_dev ||
lsb.st_ino != hsb.st_ino) {
if (ex_run_file(sp, fd, _PATH_EXRC))
return (1);
} else
close(fd);
}
break;
case NOPERM:
break;
case RCOK:
if (lsb.st_dev != hsb.st_dev ||
lsb.st_ino != hsb.st_ino) {
if (ex_run_file(sp, fd, _PATH_NEXRC))
return (1);
} else
close(fd);
break;
}
if (EXCMD_RUNNING(sp->gp))
(void)ex_cmd(sp);
if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE))
return (0);
}
return (0);
}
static int
ex_run_file(SCR *sp, int fd, char *name)
{
ARGS *ap[2], a;
EXCMD cmd;
ex_cinit(&cmd, C_SOURCE, 0, OOBLNO, OOBLNO, 0, ap);
ex_cadd(&cmd, &a, name, strlen(name));
return (ex_sourcefd(sp, &cmd, fd));
}
int
ex_run_str(SCR *sp, char *name, char *str, size_t len, int ex_flags,
int nocopy)
{
GS *gp;
EXCMD *ecp;
gp = sp->gp;
if (EXCMD_RUNNING(gp)) {
CALLOC_RET(sp, ecp, 1, sizeof(EXCMD));
LIST_INSERT_HEAD(&gp->ecq, ecp, q);
} else
ecp = &gp->excmd;
F_INIT(ecp,
ex_flags ? E_BLIGNORE | E_NOAUTO | E_NOPRDEF | E_VLITONLY : 0);
if (nocopy)
ecp->cp = str;
else
if ((ecp->cp = v_strdup(sp, str, len)) == NULL)
return (1);
ecp->clen = len;
if (name == NULL)
ecp->if_name = NULL;
else {
if ((ecp->if_name = v_strdup(sp, name, strlen(name))) == NULL)
return (1);
ecp->if_lno = 1;
F_SET(ecp, E_NAMEDISCARD);
}
return (0);
}
static enum rc
exrc_isok(SCR *sp, struct stat *sbp, int *fdp, char *path, int rootown,
int rootid)
{
enum { ROOTOWN, OWN, WRITER } etype;
uid_t euid;
int nf1, nf2;
char *a, *b, buf[PATH_MAX];
if ((*fdp = open(path, O_RDONLY)) < 0) {
if (errno == ENOENT)
return (NOEXIST);
msgq_str(sp, M_SYSERR, path, "%s");
return (NOPERM);
}
if (fstat(*fdp, sbp)) {
msgq_str(sp, M_SYSERR, path, "%s");
close(*fdp);
return (NOPERM);
}
euid = geteuid();
if (!(rootown && sbp->st_uid == 0) &&
!(rootid && euid == 0) && sbp->st_uid != euid) {
etype = rootown ? ROOTOWN : OWN;
goto denied;
}
if (sbp->st_mode & (S_IWGRP | S_IWOTH)) {
etype = WRITER;
goto denied;
}
return (RCOK);
denied: a = msg_print(sp, path, &nf1);
if (strchr(path, '/') == NULL && getcwd(buf, sizeof(buf)) != NULL) {
b = msg_print(sp, buf, &nf2);
switch (etype) {
case ROOTOWN:
msgq(sp, M_ERR,
"%s/%s: not sourced: not owned by you or root",
b, a);
break;
case OWN:
msgq(sp, M_ERR,
"%s/%s: not sourced: not owned by you", b, a);
break;
case WRITER:
msgq(sp, M_ERR,
"%s/%s: not sourced: writable by a user other than the owner", b, a);
break;
}
if (nf2)
FREE_SPACE(sp, b, 0);
} else
switch (etype) {
case ROOTOWN:
msgq(sp, M_ERR,
"%s: not sourced: not owned by you or root", a);
break;
case OWN:
msgq(sp, M_ERR,
"%s: not sourced: not owned by you", a);
break;
case WRITER:
msgq(sp, M_ERR,
"%s: not sourced: writable by a user other than the owner", a);
break;
}
if (nf1)
FREE_SPACE(sp, a, 0);
close(*fdp);
return (NOPERM);
}