#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <locale.h>
#include <syslog.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <dirent.h>
#include <limits.h>
#include <thread.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/vfs.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mnttab.h>
#include <sys/mntent.h>
#include <sys/mount.h>
#include <sys/signal.h>
#include <sys/utsname.h>
#include <sys/systeminfo.h>
#include <sys/tiuser.h>
#include <sys/utsname.h>
#include <rpc/rpc.h>
#include <rpcsvc/nfs_prot.h>
#include <rpcsvc/daemon_utils.h>
#include <assert.h>
#include "automount.h"
#include <deflt.h>
#include <zone.h>
#include <priv.h>
#include <fcntl.h>
#include <libshare.h>
#include <libscf.h>
#include "smfcfg.h"
static char *check_hier(char *);
static int arch(char *, size_t, bool_t);
static int cpu(char *, size_t);
static int natisa(char *, size_t);
static int platform(char *, size_t);
struct mntlist *current_mounts;
static bool_t nodirect_map = FALSE;
static void
loadzone_maps(char *mntpnt, char *map, char *opts, char **stack, char ***stkptr)
{
zoneid_t *zids = NULL;
zoneid_t my_zoneid;
uint_t nzents_saved;
uint_t nzents;
int i;
if (!priv_ineffect(PRIV_SYS_MOUNT))
return;
if (zone_list(NULL, &nzents) != 0) {
return;
}
my_zoneid = getzoneid();
again:
if (nzents == 0)
return;
zids = malloc(nzents * sizeof (zoneid_t));
nzents_saved = nzents;
if (zone_list(zids, &nzents) != 0) {
free(zids);
return;
}
if (nzents != nzents_saved) {
free(zids);
goto again;
}
for (i = 0; i < nzents; i++) {
char zonename[ZONENAME_MAX];
char zoneroot[MAXPATHLEN];
if (getzonenamebyid(zids[i], zonename, ZONENAME_MAX) != -1) {
char appended_map[MAXPATHLEN];
char prepended_mntpnt[MAXPATHLEN];
char map_path[MAXPATHLEN];
int fd;
(void) snprintf(appended_map, sizeof (appended_map),
"%s_%s", map, zonename);
if (zids[i] != my_zoneid) {
(void) snprintf(prepended_mntpnt,
sizeof (prepended_mntpnt),
"/zone/%s%s", zonename, mntpnt);
if (zone_getattr(zids[i], ZONE_ATTR_ROOT,
zoneroot, sizeof (zoneroot)) == -1)
continue;
} else {
(void) strcpy(prepended_mntpnt, mntpnt);
zoneroot[0] = '\0';
}
dirinit(prepended_mntpnt, appended_map, opts, 0, stack,
stkptr);
(void) snprintf(map_path, sizeof (map_path),
"/etc/%s", appended_map);
if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL,
S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) {
int len;
char map_rec[MAXPATHLEN];
len = snprintf(map_rec, sizeof (map_rec),
"+%s\n*\t-fstype=lofs\t:%s/export/home/&\n",
appended_map, zoneroot);
if (len <= sizeof (map_rec))
(void) write(fd, map_rec, len);
(void) close(fd);
}
}
}
free(zids);
}
void
dirinit(char *mntpnt, char *map, char *opts, int direct, char **stack,
char ***stkptr)
{
struct autodir *dir;
char *p;
if (strcmp(map, "-null") == 0) {
if (strcmp(mntpnt, "/-") == 0)
nodirect_map = TRUE;
goto enter;
}
p = mntpnt + (strlen(mntpnt) - 1);
if (*p == '/')
*p = '\0';
if (*mntpnt != '/') {
pr_msg("dir %s must start with '/'", mntpnt);
return;
}
if (p = check_hier(mntpnt)) {
pr_msg("hierarchical mountpoint: %s and %s",
p, mntpnt);
return;
}
if ((strcmp(mntpnt, "/-") == 0) && !(nodirect_map)) {
(void) loaddirect_map(map, map, opts, stack, stkptr);
return;
}
if (is_system_labeled() &&
(strcmp(mntpnt, "/home") == 0) &&
(strcmp(map, "auto_home") == 0)) {
(void) loadzone_maps(mntpnt, map, opts, stack, stkptr);
return;
}
enter:
dir = (struct autodir *)malloc(sizeof (*dir));
if (dir == NULL)
goto alloc_failed;
dir->dir_name = strdup(mntpnt);
if (dir->dir_name == NULL)
goto alloc_failed;
dir->dir_map = strdup(map);
if (dir->dir_map == NULL)
goto alloc_failed;
dir->dir_opts = strdup(opts);
if (dir->dir_opts == NULL)
goto alloc_failed;
dir->dir_direct = direct;
dir->dir_remount = 0;
dir->dir_next = NULL;
if (dir_head == NULL)
dir_head = dir;
else
dir_tail->dir_next = dir;
dir->dir_prev = dir_tail;
dir_tail = dir;
return;
alloc_failed:
if (dir != NULL) {
if (dir->dir_opts)
free(dir->dir_opts);
if (dir->dir_map)
free(dir->dir_map);
if (dir->dir_name)
free(dir->dir_name);
free(dir);
}
pr_msg("dirinit: memory allocation failed");
}
static char *
check_hier(mntpnt)
char *mntpnt;
{
register struct autodir *dir;
register char *p, *q;
for (dir = dir_head; dir; dir = dir->dir_next) {
p = dir->dir_name;
q = mntpnt;
for (; *p == *q; p++, q++)
if (*p == '\0')
break;
if (*p == '/' && *q == '\0')
return (dir->dir_name);
if (*p == '\0' && *q == '/')
return (dir->dir_name);
if (*p == '\0' && *q == '\0')
return (NULL);
}
return (NULL);
}
int
getword(char *w, char *wq, char **p, char **pq, char delim, int wordsz)
{
char *tmp = w;
char *tmpq = wq;
int count = wordsz;
if (wordsz <= 0) {
if (verbose)
syslog(LOG_ERR,
"getword: input word size %d must be > 0", wordsz);
return (-1);
}
while ((delim == ' ' ? isspace(**p) : **p == delim) && **pq == ' ')
(*p)++, (*pq)++;
while (**p &&
!((delim == ' ' ? isspace(**p) : **p == delim) &&
**pq == ' ')) {
if (--count <= 0) {
*tmp = '\0';
*tmpq = '\0';
syslog(LOG_ERR,
"maximum word length (%d) exceeded", wordsz);
return (-1);
}
*w++ = *(*p)++;
*wq++ = *(*pq)++;
}
*w = '\0';
*wq = '\0';
return (0);
}
char *
get_line(FILE *fp, char *map, char *line, int linesz)
{
register char *p = line;
register int len;
int excess = 0;
*p = '\0';
for (;;) {
if (fgets(p, linesz - (p-line), fp) == NULL) {
return (*line ? line : NULL);
}
len = strlen(line);
if (len <= 0) {
p = line;
continue;
}
p = &line[len - 1];
if (*p != '\n') {
excess = 1;
(void) ungetc(*p, fp);
break;
}
trim:
while (p >= line && isspace(*(uchar_t *)p))
*p-- = '\0';
if (p < line) {
p = line;
continue;
}
if (*p == '\\') {
*p = '\0';
continue;
}
p = line;
while (p = strchr(p, '#')) {
if (p == line || isspace(*(p-1))) {
*p-- = '\0';
goto trim;
}
p++;
}
break;
}
if (excess) {
int c;
while ((c = getc(fp)) != EOF) {
*p = c;
if (*p == '\n')
break;
else if (*p == '\\') {
if (getc(fp) == EOF)
break;
}
}
syslog(LOG_ERR,
"map %s: line too long (max %d chars)",
map, linesz-1);
*line = '\0';
}
return (line);
}
int
get_retry(char *opts)
{
int retry = 0;
char buf[MAXOPTSLEN];
char *p, *pb, *lasts;
if (opts == NULL)
return (retry);
(void) strcpy(buf, opts);
pb = buf;
while (p = (char *)strtok_r(pb, ",", &lasts)) {
pb = NULL;
if (strncmp(p, "retry=", 6) == 0)
retry = atoi(p+6);
}
return (retry > 0 ? retry : 0);
}
int
str_opt(struct mnttab *mnt, char *opt, char **sval)
{
char *str, *comma;
if (str = hasmntopt(mnt, opt)) {
str += strlen(opt);
if (*str++ != '=' ||
(*str == ',' || *str == '\0')) {
syslog(LOG_ERR, "Bad option field");
return (-1);
}
comma = strchr(str, ',');
if (comma != NULL)
*comma = '\0';
*sval = strdup(str);
if (comma != NULL)
*comma = ',';
if (*sval == NULL)
return (-1);
} else
*sval = NULL;
return (0);
}
int
macro_expand(key, pline, plineq, size)
char *key, *pline, *plineq;
int size;
{
register char *p, *q;
register char *bp, *bq;
register char *s;
char buffp[LINESZ], buffq[LINESZ];
char namebuf[64], *pn;
int expand = 0;
struct utsname name;
char procbuf[SYS_NMLN];
char isaname[64];
p = pline; q = plineq;
bp = buffp; bq = buffq;
while (*p) {
if (*p == '&' && *q == ' ') {
if ((int)((bp - buffp) + strlen(key)) < size) {
for (s = key; *s; s++) {
*bp++ = *s;
*bq++ = ' ';
}
expand++;
p++; q++;
continue;
} else {
return (1);
}
}
if (*p == '$' && *q == ' ') {
p++; q++;
pn = namebuf;
if (*p == '{') {
p++; q++;
while (*p && *p != '}') {
*pn++ = *p++;
q++;
}
if (*p) {
p++; q++;
}
} else {
while (*p && (*p == '_' || isalnum(*p))) {
*pn++ = *p++;
q++;
}
}
*pn = '\0';
s = getenv(namebuf);
if (!s) {
if (strcmp(namebuf, "ARCH") == 0) {
if (arch(procbuf, sizeof (procbuf),
FALSE))
s = procbuf;
} else if (strcmp(namebuf, "CPU") == 0) {
if (cpu(procbuf, sizeof (procbuf)))
s = procbuf;
} else if (strcmp(namebuf, "HOST") == 0) {
(void) uname(&name);
s = name.nodename;
} else if (strcmp(namebuf, "KARCH") == 0) {
if (arch(procbuf, sizeof (procbuf),
TRUE))
s = procbuf;
} else if (strcmp(namebuf, "OSREL") == 0) {
(void) uname(&name);
s = name.release;
} else if (strcmp(namebuf, "OSNAME") == 0) {
(void) uname(&name);
s = name.sysname;
} else if (strcmp(namebuf, "OSVERS") == 0) {
(void) uname(&name);
s = name.version;
} else if (strcmp(namebuf, "NATISA") == 0) {
if (natisa(isaname, sizeof (isaname)))
s = isaname;
} else if (strcmp(namebuf, "PLATFORM") == 0) {
if (platform(procbuf, sizeof (procbuf)))
s = procbuf;
}
}
if (s) {
if ((int)((bp - buffp) + strlen(s)) < size) {
while (*s) {
*bp++ = *s++;
*bq++ = ' ';
}
} else {
return (1);
}
}
expand++;
continue;
}
if (bp - buffp == size - 1) {
return (1);
}
*bp++ = *p++;
*bq++ = *q++;
}
if (!expand)
return (0);
*bp = '\0';
*bq = '\0';
(void) strcpy(pline, buffp);
(void) strcpy(plineq, buffq);
return (0);
}
void
unquote(str, qbuf)
char *str, *qbuf;
{
register int escaped, inquote, inbracket, quoted;
register char *ip, *bp, *qp;
char buf[LINESZ];
escaped = inquote = inbracket = quoted = 0;
for (ip = str, bp = buf, qp = qbuf; *ip; ip++) {
if (!escaped) {
if (*ip == '\\') {
escaped = 1;
quoted++;
continue;
} else
if (*ip == '"') {
inquote = !inquote;
quoted++;
continue;
} else
if (*ip == '[') {
inbracket++;
quoted++;
} else
if (*ip == ']') {
if (inbracket > 0) inbracket--;
}
}
*bp++ = *ip;
*qp++ = (inquote || escaped) ? '^'
: ((inbracket && (*ip == ':')) ? '^' : ' ');
escaped = 0;
}
*bp = '\0';
*qp = '\0';
if (quoted)
(void) strcpy(str, buf);
}
void
unbracket(s)
char **s;
{
char *b = *s + strlen(*s) - 1;
if (*b == ']')
*b = '\0';
if (**s == '[')
(*s)++;
}
void
trim(s)
char *s;
{
char *p = &s[strlen(s) - 1];
while (p >= s && isspace(*(uchar_t *)p))
*p-- = '\0';
}
char *
auto_rddir_malloc(unsigned nbytes)
{
char *p;
int again = 0;
if ((p = malloc(nbytes)) == NULL) {
mutex_lock(&cleanup_lock);
cond_signal(&cleanup_start_cv);
if (cond_wait(&cleanup_done_cv, &cleanup_lock)) {
mutex_unlock(&cleanup_lock);
syslog(LOG_ERR, "auto_rddir_malloc interrupted\n");
} else {
mutex_unlock(&cleanup_lock);
again = 1;
}
}
if (again)
p = malloc(nbytes);
return (p);
}
char *
auto_rddir_strdup(const char *s1)
{
char *s2;
int again = 0;
if ((s2 = strdup(s1)) == NULL) {
mutex_lock(&cleanup_lock);
cond_signal(&cleanup_start_cv);
if (cond_wait(&cleanup_done_cv, &cleanup_lock)) {
mutex_unlock(&cleanup_lock);
syslog(LOG_ERR, "auto_rddir_strdup interrupted\n");
} else {
mutex_unlock(&cleanup_lock);
again = 1;
}
}
if (again)
s2 = strdup(s1);
return (s2);
}
struct dir_entry *
btree_lookup(struct dir_entry *head, char *name)
{
register struct dir_entry *p;
register int direction;
for (p = head; p != NULL; ) {
direction = strcmp(name, p->name);
if (direction == 0)
return (p);
if (direction > 0)
p = p->right;
else p = p->left;
}
return (NULL);
}
void
btree_enter(struct dir_entry **head, struct dir_entry *ent)
{
register struct dir_entry *p, *prev = NULL;
register int direction;
ent->right = ent->left = NULL;
if (*head == NULL) {
*head = ent;
return;
}
for (p = *head; p != NULL; ) {
prev = p;
direction = strcmp(ent->name, p->name);
if (direction == 0) {
return;
}
if (direction > 0)
p = p->right;
else p = p->left;
}
assert(prev != NULL);
if (direction > 0)
prev->right = ent;
else prev->left = ent;
}
int
add_dir_entry(char *name, struct dir_entry **list, struct dir_entry **last)
{
struct dir_entry *e, *l;
if ((*list != NULL) && (*last == NULL)) {
for (l = *list; l != NULL; l = l->next)
*last = l;
}
if (btree_lookup(*list, name) == NULL) {
e = (struct dir_entry *)
auto_rddir_malloc(sizeof (struct dir_entry));
if (e == NULL)
return (ENOMEM);
(void) memset((char *)e, 0, sizeof (*e));
e->name = auto_rddir_strdup(name);
if (e->name == NULL) {
free(e);
return (ENOMEM);
}
e->next = NULL;
if (*list == NULL) {
*list = *last = e;
} else {
assert(*last != NULL);
(*last)->next = e;
*last = e;
}
btree_enter(list, e);
}
return (0);
}
#define FMT_BUFSIZ 1024
void
trace_prt(int id, char *fmt, ...)
{
va_list args;
char buf[FMT_BUFSIZ];
if (id) {
(void) sprintf(buf, "t%u\t%s", thr_self(), fmt);
fmt = buf;
}
va_start(args, fmt);
(void) vfprintf(stderr, fmt, args);
va_end(args);
}
static char *
isalist(void)
{
char *buf;
size_t bufsize = BUFSIZ;
long ret;
buf = malloc(bufsize);
do {
ret = sysinfo(SI_ISALIST, buf, bufsize);
if (ret == -1l)
return (NULL);
if (ret > bufsize) {
bufsize = ret;
buf = realloc(buf, bufsize);
} else
break;
} while (buf != NULL);
return (buf);
}
static int
bitness(char *isaname)
{
if (strcmp(isaname, "sparc") == 0 ||
strcmp(isaname, "i386") == 0)
return (32);
if (strcmp(isaname, "sparcv9") == 0 ||
strcmp(isaname, "amd64") == 0)
return (64);
return (0);
}
static int
arch(char *buf, size_t bufsize, bool_t karch)
{
long ret;
ret = sysinfo(SI_MACHINE, buf, bufsize);
if (ret == -1L)
return (0);
if (!karch && strncmp(buf, "sun4", 4) == 0)
(void) strlcpy(buf, "sun4", bufsize);
return (1);
}
static int
cpu(char *buf, size_t bufsize)
{
long ret;
ret = sysinfo(SI_ARCHITECTURE, buf, bufsize);
if (ret == -1L)
return (0);
else
return (1);
}
static int
natisa(char *buf, size_t bufsize)
{
int bits;
char *isa, *list;
char *lasts;
if ((list = isalist()) == NULL)
return (0);
for (isa = strtok_r(list, " ", &lasts);
isa; isa = strtok_r(0, " ", &lasts))
if ((bits = bitness(isa)) != 0)
break;
if (isa == 0 || bits == 0) {
free(list);
return (0);
}
(void) strncpy(buf, isa, bufsize);
free(list);
return (1);
}
static int
platform(char *buf, size_t bufsize)
{
long ret;
ret = sysinfo(SI_PLATFORM, buf, bufsize);
if (ret == -1L)
return (0);
else
return (1);
}
void
put_automountd_env(void)
{
char defval[PATH_MAX], *p, *a, *c;
int ret = 0, bufsz = PATH_MAX;
ret = autofs_smf_get_prop("environment", defval, DEFAULT_INSTANCE,
SCF_TYPE_ASTRING, AUTOMOUNTD, &bufsz);
if (ret == SA_OK) {
a = c = defval;
if (*a == '\0')
return;
while ((p = strchr(c, ',')) != NULL) {
if (*(p - 1) == '\\') {
c = p + 1;
continue;
}
*p = '\0';
if ((c = strchr(a, '=')) != NULL)
putenv(strdup(a));
a = c = p + 1;
}
if (*a != '\0') {
if ((c = strchr(a, '=')) != NULL)
putenv(strdup(a));
}
}
}