extern int resolvepath(const char* file, char* path, size_t size) { register char* s; register int n; register int r; r = *file != '/'; n = strlen(file) + r + 1; if (n >= size) { #ifdef ENAMETOOLONG errno = ENAMETOOLONG; #else errno = ENOMEM; #endif return 0; } if (!r) s = path; else if (!getcwd(path, size - n)) return 0; else { s = path + strlen(path); *s++ = '/'; } strlcpy(s, file, size - (s - path)); return (s = pathcanon(path, size, PATH_PHYSICAL|PATH_DOTDOT|PATH_EXISTS)) ? (s - path) : -1; }
static int initiate(Cs_t* state, const char* svc, char* cmd) { pid_t pid; pid_t n; char* av[3]; #ifdef SIGCHLD Handler_t fun; children = 0; if ((fun = signal(SIGCHLD, child)) == SIG_DFL) signal(SIGCHLD, fun); else if (children) children++; #endif pathcanon(cmd, 0, 0); av[0] = cmd; av[1] = (char*)svc; av[2] = 0; if ((pid = spawnveg(av[0], av, environ, 0)) == -1) { messagef((state->id, NiL, -1, "local: %s: cannot initiate %s", svc, cmd)); return -1; } while ((n = waitpid(pid, NiL, 0)) == -1 && errno == EINTR); #ifdef SIGCHLD if (fun != SIG_DFL) { signal(SIGCHLD, fun); if (fun != SIG_IGN) while (--children > 0) (*fun)(SIGCHLD); } #endif /* * yuk: looks like we have to give fdp services time * to start up -- a few seconds shouldn't hurt */ sleep(2); return n; }
static #endif char* pathreal(const char* apath, register int type, struct stat* st) { char* path = (char*)apath; register char* sp; register char* cp; register char* ip; Table_t* safe; int oerrno = errno; int opaqued = 0; int len; int vir; int safesize; int safe_dir; long visits; char buf[PATH_MAX + 1]; static struct stat stbuf; static struct stat tsbuf; state.path.level = state.path.synthesize = state.path.nlinks = 0; if (!path) { errno = EFAULT; return 0; } initialize(); if (state.in_2d) { if (!st || (!state.level || *path == '/') && !LSTAT(path, st)) return path; if (state.level && streq(path, ".") && !CHDIR(state.pwd)) { state.level = 0; return path; } return 0; } #if FS if (mounted() && (sp = fsreal(state.path.monitor, MSG_stat, state.path.mount))) apath = (char*)(path = sp); #endif /* * handle null path, . and / separately */ if (safe = state.safe ? &state.vsafe : (Table_t*)0) { type |= P_ABSOLUTE; if (!(safesize = state.safe->servicesize)) safesize = strlen(state.safe->service); } else type &= ~P_SAFE; again: if (!*path) { errno = ENOENT; return 0; } cp = sp = path; state.path.synthesize = state.path.linksize = 0; if (!st) st = &stbuf; /* * check if virtual dir has been created by another process * only P_PATHONLY|P_TOP calls (usually create or modify link) and * references to "." are checked for performance */ if (state.level > 0 && state.pwd && ((type & (P_PATHONLY|P_TOP)) && *sp != '/' || *sp == '.' && sp[1] == 0)) { if (!CHDIR(state.pwd)) state.level = 0; else if (!(type & (P_PATHONLY|P_TOP))) { len = 0; state.path.level += (state.path.synthesize = state.level); sp = strcpy(state.path.name, state.pwd); goto skip; } } if (!state.pwd || sp[1] == 0 && (*sp == '.' || *sp == '/' && !safe)) { if (st != &stbuf && LSTAT(sp, st)) return 0; if (*sp == '/' || !state.pwd && (type & P_PATHONLY)) strncpy(state.path.name, sp, PATH_MAX); else if (!state.pwd) { /* * treat the current directory as if were empty */ errno = ENOENT; return 0; } else strncpy(state.path.name, state.pwd, PATH_MAX); errno = oerrno; return state.path.name; } /* * put absolute pathname into state.path */ safe_dir = 0; if (*path != '/') { strcpy(state.path.name, state.pwd); sp = state.path.name + state.pwdsize; *sp++ = '/'; if (safe && state.pwdsize >= safesize && !strncmp(state.pwd, state.safe->service, safesize) && (!state.pwd[safesize] || state.pwd[safesize] == '/')) safe_dir = safesize; } else sp = state.path.name; ip = state.path.name + elementsof(state.path.name); while (sp < ip && (*sp = *cp++)) sp++; if (type & P_DOTDOT) strcpy(sp, "/.."); sp = state.path.name; if (!(ip = pathcanon(sp + safe_dir, sizeof(state.path.name) - safe_dir, 0))) { errno = ENOENT; return 0; } if (type & (P_DOTDOT|P_NOSLASH)) { /* * remove trailing slashes */ while (*--ip == '/'); *++ip = 0; } else if ((type & P_SLASH) && *(ip - 1) != '/') *ip++ = '/'; if (*(ip - 1) == '/' && ip - sp > 1) { /* * trailing slash is equivalent to trailing slash-dot * this forces the common-sense interpretation */ #if DEBUG if (!(state.test & 010)) #endif *ip++ = '.'; *ip = 0; } len = ip - sp; /* * try to use relative path */ if (!(type & (P_LSTAT|P_READLINK))) { for (ip = state.pwd; *ip && *ip == *sp++; ip++); if (*ip != 0 || *sp && *sp != '/' || state.level < 0) sp = state.path.name; else { state.path.level += (state.path.synthesize = state.level); if (state.level && !(type & P_PATHONLY) && st == &stbuf) { sp = state.path.name; len -= state.pwdsize; } else if (type & P_ABSOLUTE) sp = state.path.name; else if (*sp == '/') sp++; } if (*sp == 0) sp = state.dot; } skip: if ((type & P_NOOPAQUE) && !LSTAT(sp, st) && checkopaque(sp, st)) { message((-1, "%s: remove opaque", sp)); UNLINK(sp); opaqued = 1; } if (safe && *sp == '/') { state.path.table = safe; cp = pathnext(sp, NiL, NiL); state.path.table = safe = 0; if (cp) { state.path.level = 0; path = strcpy(buf, sp); message((-5, "pathreal: == safe map %s", path)); type &= ~(P_DOTDOT|P_SAFE); goto again; } if (!*(sp + 1)) { strncpy(sp, state.safe->service, safesize); sp[safesize] = 0; } else if (strncmp(sp, state.safe->service, safesize) || sp[safesize] && sp[safesize] != '/') { if (*path != '/' && safe_dir) { errno = EPERM; return 0; } if (sp[1]) strcpy(buf, sp); else *buf = 0; len = sfsprintf(sp, sizeof(state.path.name), "%-*s%s", safesize, state.safe->service, buf); message((-5, "pathreal: == safe next %s", sp)); if (!pathnext(sp, NiL, NiL)) { errno = EPERM; return 0; } } else type &= ~P_SAFE; } if ((type & P_SAFE) && state.path.level) { errno = EPERM; return 0; } if (type & P_PATHONLY) { errno = oerrno; return sp; } visits = 0; vir = 1; while (LSTAT(sp, st)) { if (vir) { if (apath[0] == '.' && apath[1] == '.' && apath[2] == '.' && !apath[3]) { if (state.level > 0) { message((-1, "pathreal: %s => %s", apath, sp)); LSTAT(".", st); return sp; } errno = ENOENT; return 0; } vir = 0; } if (errno == ENOTDIR) { /* * check for version instance */ cp = ip = sp + strlen(sp); while (ip > sp && *--ip != '/'); if (ip < sp) return 0; while (ip > sp && *--ip == '/'); if (ip < sp) return 0; while (ip > sp && *--ip != '/'); if (*ip == '/') ip++; while (cp >= ip) { cp[4] = *cp; cp--; } memcpy(ip, state.opaque, 4); if (!LSTAT(sp, st)) break; errno = ENOTDIR; return 0; } if (errno != ENOENT || opaqued) return 0; #if FS /* * check user mount */ if (visits) { Mount_t* mp; const char* up; if ((mp = getmount(sp, &up)) && (mp->fs->flags & FS_NAME) && (sp = fsreal(mp, MSG_open, (char*)up)) && !LSTAT(sp, st)) break; } #endif /* * search down the viewpath */ if (type & P_SAFE) { errno = EPERM; return 0; } if (!pathnext(state.path.name, NiL, &visits)) return 0; sp = state.path.name; if (!(type & P_ABSOLUTE)) { /* * try to use relative path */ for (ip = state.pwd; *ip && *ip == *sp++; ip++); if (*ip == 0 && *sp == '/') sp++; else sp = state.path.name; } if (*sp == 0) sp = state.dot; } if (st->st_nlink > 1 && checkopaque(sp, st)) return 0; if ((type & P_TOP) && state.path.level) { int rfd; int wfd; if ((rfd = OPEN(sp, O_RDONLY, 0)) < 0) sp = 0; else { tsbuf = *st; wfd = open(apath, O_WRONLY|O_CREAT|O_TRUNC|O_cloexec, st->st_mode & S_IPERM); *st = tsbuf; if (wfd < 0) sp = 0; else { if (fs3d_copy(rfd, wfd, st)) sp = 0; CLOSE(wfd); } CLOSE(rfd); } if (!sp) { errno = EROFS; return 0; } if (st == &stbuf) st = 0; return pathreal(apath, P_PATHONLY, st); } IVIEW(st, state.path.level); if (state.path.synthesize) { if (state.path.level < state.level) { if (len) { ip = state.path.name + strlen(state.path.name) - len; len = *ip; *ip = 0; } if (!CHDIR(state.path.name)) state.level = state.path.level; message((-1, "chdir=%s level=%d", state.path.name, state.level)); *ip = len; } else if (S_ISDIR(st->st_mode)) { int mask; static int uid = -1; static int gid; umask(mask = umask(0)); st->st_mode = (st->st_mode | (S_IRWXU|S_IRWXG|S_IRWXO)) & ~(mask & (S_IRWXU|S_IRWXG|S_IRWXO)); if (uid == -1) { uid = geteuid(); gid = getegid(); } st->st_uid = uid; st->st_gid = gid; } } ip = sp; /* * symbolic links handled specially * get filename from pathname */ if (S_ISLNK(st->st_mode) && (len = checklink(sp, st, type)) > 1 && !(type & (P_LSTAT|P_READLINK)) && state.path.nlinks++ < MAXSYMLINKS) { path = strcpy(buf, state.path.name); message((-1, "pathreal: == again %s", path)); if (*path != '/') state.path.level = 0; type &= ~(P_DOTDOT|P_SAFE); goto again; } #if VCS && defined(VCS_REAL) VCS_REAL(state.path.name, st); #endif errno = oerrno; return sp; }
int csopen(register Cs_t* state, const char* apath, int op) { register char* path = (char*)apath; register char* b; register char* s; register int n; int fd; char* t; char* u; char* type; char* endtype; char* host; char* endhost; char* serv; char* endserv; char* qual; char* endqual; char* opath; char* user = 0; char* group = 0; char* trust = 0; char* arg = 0; int nfd = -1; int uid = -1; int gid = -1; int sid = -1; int auth = 1; int mode; unsigned long addr; unsigned long port = 0; struct stat st; char buf[PATH_MAX]; char tmp[PATH_MAX]; if (!path) { errno = EFAULT; return -1; } csprotect(&cs); if (op < 0) op = CS_OPEN_TEST; messagef((state->id, NiL, -8, "open(%s,%o) call", path, op)); /* * blast out the parts */ opath = path; if (pathgetlink(path, buf, sizeof(buf)) <= 0) { if (strlen(path) >= sizeof(buf)) return -1; strcpy(buf, path); } else if ((state->flags & CS_ADDR_LOCAL) && (s = strrchr(buf, '/'))) { /* * dynamic ip assignment can change the addr * underfoot in some implementations so we * double check the local ip here */ strcpy(tmp, buf); if (tokscan(tmp, NiL, "/dev/%s/%s/%s", &type, NiL, &serv) == 3) sfsprintf(buf, sizeof(buf), "/dev/%s/%s/%s", type, csntoa(state, 0), serv); } path = buf; pathcanon(path, 0, 0); errno = ENOENT; strcpy(state->path, path); b = path; if ((*b++ != '/') || !(s = strchr(b, '/'))) return -1; *s++ = 0; if (!streq(b, "dev")) return -1; if (b = strchr(s, '/')) *b++ = 0; if (streq(s, "fdp")) { #if !( CS_LIB_SOCKET_UN || CS_LIB_STREAM || CS_LIB_V10 ) if (access(CS_PROC_FD_TST, F_OK)) { errno = ENODEV; messagef((state->id, NiL, -1, "open: %s: %s: not supported", state->path, s)); return -1; } #endif } else if (!streq(s, "tcp") && !streq(s, "udp")) { messagef((state->id, NiL, -1, "open: %s: %s: invalid type", state->path, s)); return -1; } #if !( CS_LIB_SOCKET || CS_LIB_STREAM || CS_LIB_V10 ) else { errno = ENODEV; messagef((state->id, NiL, -1, "open: %s: %s: not supported", state->path, s)); return -1; } #endif type = s; qual = state->qual; if (!b) host = serv = 0; else { host = b; if (!(s = strchr(b, '/'))) serv = 0; else { *s++ = 0; serv = s; /* * grab the next fd to preserve open semantics */ for (n = 0; n < 10; n++) if ((nfd = dup(n)) >= 0) break; /* * get qual, perm and arg */ mode = S_IRWXU|S_IRWXG|S_IRWXO; if (b = strchr(s, '/')) { *b++ = 0; do { if (*b == '#') { arg = b + 1; break; } if (u = strchr(b, '/')) *u++ = 0; if (s = strchr(b, '=')) *s++ = 0; for (n = 0, t = b; *t; n = HASHKEYPART(n, *t++)); switch (n) { case HASHKEY5('g','r','o','u','p'): group = s ? s : ""; break; case HASHKEY5('l','o','c','a','l'): op |= CS_OPEN_LOCAL; break; case HASHKEY3('n','o','w'): op |= CS_OPEN_NOW; break; case HASHKEY5('o','t','h','e','r'): auth = 0; break; case HASHKEY6('r','e','m','o','t','e'): op |= CS_OPEN_REMOTE; break; case HASHKEY5('s','h','a','r','e'): op |= CS_OPEN_SHARE; break; case HASHKEY5('s','l','a','v','e'): op |= CS_OPEN_SLAVE; break; case HASHKEY4('t','e','s','t'): op |= CS_OPEN_TEST; break; case HASHKEY5('t','r','u','s','t'): op |= CS_OPEN_TRUST; trust = s; break; case HASHKEY4('u','s','e','r'): user = s ? s : ""; break; default: qual += sfsprintf(qual, sizeof(state->qual) - (qual - state->qual) - 1, "%s%s", qual == state->qual ? "" : "-", b); if (s) *(s - 1) = '='; break; } } while (b = u); } } } if (*type != 't') auth = 0; strncpy(state->type, type, sizeof(state->type) - 1); qual = (qual == state->qual) ? (char*)0 : state->qual; messagef((state->id, NiL, -8, "open: type=%s host=%s serv=%s qual=%s", type, host, serv, qual)); if (host) { /* * validate host */ if (!(state->addr = addr = csaddr(state, host))) { if (serv && !(op & CS_OPEN_CREATE) && *type == 't' && (port = csport(state, type, serv)) >= CS_PORT_MIN && port <= CS_PORT_MAX) { /* * attempt proxy connection */ if (nfd >= 0) { close(nfd); nfd = -1; } if ((fd = state->proxy.addr ? csbind(state, type, state->proxy.addr, state->proxy.port, 0L) : reopen(state, csvar(state, CS_VAR_PROXY, 0))) >= 0) { state->proxy.addr = state->addr; state->proxy.port = state->port; n = sfsprintf(tmp, sizeof(tmp), "\n%s!%s!%d\n\n%s\n%s\n0\n-1\n-1\n", type, host, port, csname(state, 0), error_info.id ? error_info.id : state->id); if (cswrite(state, fd, tmp, n) == n && (n = csread(state, fd, tmp, sizeof(tmp), CS_LINE)) >= 2) { if (tmp[0] == '0' && tmp[1] == '\n') return fd; if (error_info.trace <= -4 && n > 2) { s = tmp; s[n - 1] = 0; while (*s && *s++ != '\n'); messagef((state->id, NiL, -4, "%s error message `%s'", csvar(state, CS_VAR_PROXY, 0), s)); } } close(fd); } } #ifdef EADDRNOTAVAIL errno = EADDRNOTAVAIL; #else errno = ENOENT; #endif goto bad; } if (op & CS_OPEN_LOCAL) { state->flags |= CS_ADDR_LOCAL; state->flags &= ~CS_ADDR_REMOTE; } if (op & CS_OPEN_NOW) state->flags |= CS_ADDR_NOW; if ((op & (CS_OPEN_AGENT|CS_OPEN_REMOTE)) == CS_OPEN_REMOTE) { state->flags |= CS_ADDR_REMOTE; state->flags &= ~CS_ADDR_LOCAL; } if (op & CS_OPEN_SHARE) state->flags |= CS_ADDR_SHARE; if (op & CS_OPEN_SLAVE) state->flags |= CS_DAEMON_SLAVE; if (op & CS_OPEN_TEST) state->flags |= CS_ADDR_TEST; if (op & CS_OPEN_TRUST) state->flags |= CS_ADDR_TRUST; if ((state->flags & CS_ADDR_REMOTE) && (!serv || !strneq(serv, CS_SVC_INET, sizeof(CS_SVC_INET) - 1) && (strtol(serv, &t, 0), *t))) return agent(state, state->host, state->user, state->path); if (s = user) { n = geteuid(); if (*s) { if ((uid = struid(s)) < 0) { uid = strtol(s, &t, 0); if (*t) { errno = EACCES; goto bad; } } if (n && uid != n) { errno = EACCES; goto bad; } } else uid = n; mode &= ~(S_IRWXG|S_IRWXO); } if (s = group) { n = getegid(); if (*s) { if ((gid = strgid(s)) < 0) { gid = strtol(s, &t, 0); if (*t) { errno = EACCES; goto bad; } } if (geteuid() && gid != n) { gid_t* groups; int g; if ((g = getgroups(0, NiL)) <= 0) g = getconf("NGROUPS_MAX"); if (groups = newof(0, gid_t, g, 0)) { for (n = getgroups(g, groups); n >= 0; n--) if (gid == groups[n]) break; free(groups); } else n = -1; if (n < 0) { errno = EACCES; goto bad; } } } else gid = n; mode &= ~S_IRWXO; } if (s = trust) { if (!*s) sid = geteuid(); else if ((sid = struid(s)) < 0) { sid = strtol(s, &t, 0); if (*t) { errno = EACCES; goto bad; } } } if (state->flags & CS_ADDR_SHARE) host = CS_HOST_SHARE; else { host = state->host; if (!(state->flags & CS_ADDR_LOCAL)) { if (*type == 'f') { errno = ENODEV; goto bad; } if (op & CS_OPEN_CREATE) { errno = EROFS; goto bad; } } if (serv && !qual && *type != 'f' && (port = csport(state, type, serv)) != CS_PORT_INVALID) { if (op & CS_OPEN_CREATE) addr = 0; else if (port == CS_PORT_RESERVED || port == CS_PORT_NORMAL) goto bad; if (nfd >= 0) { close(nfd); nfd = -1; } state->control = 0; if ((fd = csbind(state, type, addr, port, 0L)) >= 0) { if (mode != (S_IRWXU|S_IRWXG|S_IRWXO) && csauth(state, fd, NiL, NiL)) { close(fd); return -1; } return fd; } } } } /* * get the mount dir prefix */ if (opath == (b = path = state->mount)) { #ifdef ELOOP errno = ELOOP; #else errno = EINVAL; #endif goto bad; } if (*type == 'f') { if (host && !(state->flags & CS_ADDR_LOCAL)) { errno = ENODEV; goto bad; } b += sfsprintf(b, sizeof(state->mount) - (b - path), "%s", csvar(state, CS_VAR_LOCAL, 0)); if ((op & CS_OPEN_CREATE) && eaccess(path, X_OK) && (mkdir(path, S_IRWXU|S_IRWXG|S_IRWXO) || chmod(path, S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO))) goto bad; } else { if (op & CS_OPEN_TRUST) { if (!pathaccess(csvar(state, CS_VAR_TRUST, 1), csvar(state, CS_VAR_SHARE, 1), NiL, PATH_EXECUTE, b, sizeof(state->mount) - (b - state->mount))) goto bad; } else if (!pathpath(csvar(state, CS_VAR_SHARE, 0), "", PATH_EXECUTE, b, sizeof(state->mount) - (b - state->mount))) goto bad; b += strlen(b); } /* * add the type */ b += sfsprintf(b, sizeof(state->mount) - (b - path), "/%s", type); if (!host) { *(state->control = b + 1) = 0; if (nfd >= 0) close(nfd); if ((fd = open(path, O_RDONLY)) < 0) { mkmount(state, S_IRWXU|S_IRWXG|S_IRWXO, -1, -1, NiL, NiL, NiL); fd = open(path, O_RDONLY); } if (fd < 0) messagef((state->id, NiL, -1, "open: %s: %s: open error", state->path, path)); return fd; } endtype = b; /* * add the host */ if (strlen(host) <= CS_MNT_MAX) b += sfsprintf(b, sizeof(state->mount) - (b - path), "/%s", host); else { s = csntoa(state, addr); if (strlen(s) <= CS_MNT_MAX) b += sfsprintf(b, sizeof(state->mount) - (b - path), "/%s", s); else { unsigned char* a = (unsigned char*)&addr; b += sfsprintf(b, sizeof(state->mount) - (b - path), "/0x%X.%X.%X.%X", a[0], a[1], a[2], a[3]); } } messagef((state->id, NiL, -8, "%s:%d host=`%s' path=`%s'", __FILE__, __LINE__, host, path)); if (!serv) { *(state->control = b + 1) = 0; if (nfd >= 0) close(nfd); if ((fd = open(path, O_RDONLY)) < 0) messagef((state->id, NiL, -1, "open: %s: %s: open error", state->path, path)); return fd; } endhost = b; /* * add the service */ sfsprintf(b, sizeof(state->mount) - (b - path), "%s/%s/%s/%s%s", CS_SVC_DIR, type, serv, serv, CS_SVC_SUFFIX); if (!pathpath(b, "", PATH_ABSOLUTE|PATH_EXECUTE, tmp, sizeof(tmp)) || stat(tmp, &st)) op |= CS_OPEN_TEST; else { *strrchr(tmp, '/') = 0; if (!(op & CS_OPEN_TRUST)) sid = st.st_uid; if (!st.st_size) op |= CS_OPEN_TEST; } b += sfsprintf(b, sizeof(state->mount) - (b - path), "/%s", serv); endserv = b; /* * add the qualifier and perm */ if (sid >= 0) b += sfsprintf(b, sizeof(state->mount) - (b - path), "/%d-", sid); else b += sfsprintf(b, sizeof(state->mount) - (b - path), "/-"); if (uid >= 0) b += sfsprintf(b, sizeof(state->mount) - (b - path), "%d-", uid); else if (gid >= 0) b += sfsprintf(b, sizeof(state->mount) - (b - path), "-%d", gid); else b += sfsprintf(b, sizeof(state->mount) - (b - path), "-"); #if limit_qualifier_length endqual = endserv + CS_MNT_MAX + 1; #else endqual = state->mount + sizeof(state->mount) - 1; #endif if (qual) { if (b < endqual) *b++ = '-'; while (b < endqual && *qual) *b++ = *qual++; } if (*type == 't' && !auth) { if (b >= endqual) b--; *b++ = CS_MNT_OTHER; } /* * add in the connect stream control */ *b++ = '/'; *b = CS_MNT_STREAM; strcpy(b + 1, CS_MNT_TAIL); messagef((state->id, NiL, -8, "%s:%d %s", __FILE__, __LINE__, state->mount)); state->control = b; /* * create the mount subdirs if necessary */ if ((op & CS_OPEN_CREATE) && mkmount(state, mode, uid, gid, endserv, endhost, endtype)) goto bad; mode &= S_IRWXU|S_IRWXG|S_IRWXO; if (nfd >= 0) { close(nfd); nfd = -1; } if (op & CS_OPEN_MOUNT) { messagef((state->id, NiL, -1, "open(%s,%o) = %d, mount = %s", state->path, op, state->mount)); return 0; } if (*type == 'f') { /* * {fdp} */ if ((fd = doattach(state, path, op, mode, user, opath, tmp, serv, b)) < 0) return -1; } else { /* * {tcp,udp} */ messagef((state->id, NiL, -8, "%s:%d %s", __FILE__, __LINE__, state->mount)); if ((fd = reopen(state, path)) < 0) { /* * check for old single char cs mount */ *(state->control + 1) = 0; if ((fd = reopen(state, path)) < 0) messagef((state->id, NiL, -1, "open: %s: %s: reopen error", state->path, path)); *(state->control + 1) = CS_MNT_TAIL[0]; } if (op & CS_OPEN_CREATE) { if (fd >= 0) { close(fd); errno = EEXIST; return -1; } if (errno != ENOENT && errno != ENOTDIR) return -1; sigcritical(1); *state->control = CS_MNT_LOCK; if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0)) < 0) { if (stat(path, &st)) { messagef((state->id, NiL, -1, "open: %s: %s: creat error", state->path, path)); goto unblock; } if ((CSTIME() - (unsigned long)st.st_ctime) < 2 * 60) { errno = EEXIST; messagef((state->id, NiL, -1, "open: %s: %s: another server won the race", state->path, path)); goto unblock; } if (remove(path)) { messagef((state->id, NiL, -1, "open: %s: %s: remove error", state->path, path)); goto unblock; } if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0)) < 0) { messagef((state->id, NiL, -1, "open: %s: %s: creat error", state->path, path)); goto unblock; } } close(fd); if (!port && (n = strtol(serv, &t, 0)) && t > serv && !*t) port = n; else if (geteuid()) port = CS_NORMAL; else port = CS_RESERVED; if ((fd = csbind(state, type, 0L, port, 0L)) >= 0) { *state->control = CS_MNT_STREAM; remove(path); if (pathsetlink(cspath(state, fd, 0), path)) { messagef((state->id, NiL, -1, "open: %s: %s: link error", cspath(state, fd, 0), path)); close(fd); fd = -1; } } unblock: *state->control = CS_MNT_LOCK; remove(path); sigcritical(0); *state->control = CS_MNT_STREAM; if (fd < 0) return -1; } else if (fd < 0 && ((op & CS_OPEN_TEST) || initiate(state, user, opath, tmp, serv) || (fd = reopen(state, path)) < 0)) { messagef((state->id, NiL, -1, "open: %s: %s: reopen/initiate error", state->path, path)); return -1; } else if (!(op & CS_OPEN_AGENT)) { *state->control = CS_MNT_AUTH; n = csauth(state, fd, path, arg); *state->control = CS_MNT_STREAM; if (n) { close(fd); messagef((state->id, NiL, -1, "open: %s: %s: authentication error", state->path, path)); return -1; } } } /* * fd is open at this point * make sure its not a bogus mount */ if (mode != (S_IRWXU|S_IRWXG|S_IRWXO)) { *state->control = 0; n = stat(path, &st); *state->control = CS_MNT_STREAM; if (n) { messagef((state->id, NiL, -1, "open: %s: %s: stat error", state->path, path)); close(fd); return -1; } if (uid >= 0 && st.st_uid != uid || gid >= 0 && st.st_gid != gid) { close(fd); errno = EPERM; messagef((state->id, NiL, -1, "open: %s: %s: uid/gid error", state->path, path)); return -1; } } return fd; bad: if (nfd >= 0) close(nfd); return -1; }
int main(int argc, register char** argv) { register int n; register char* s; char* args; char* codes; char** av; char** ap; int i; int count; int len; int traverse; int size; Dir_t* firstdir; Dir_t* lastdir; Exnode_t* x; Exnode_t* y; Ftw_t ftw; Finddisc_t disc; setlocale(LC_ALL, ""); error_info.id = "tw"; av = argv + 1; args = 0; codes = 0; count = 0; size = 0; traverse = 1; firstdir = lastdir = newof(0, Dir_t, 1, 0); firstdir->name = "."; state.action = LIST; state.cmdflags = CMD_EXIT|CMD_IGNORE|CMD_IMPLICIT|CMD_NEWLINE; state.errexit = EXIT_QUIT; state.ftwflags = ftwflags()|FTW_DELAY; state.select = ALL; state.separator = '\n'; memset(&disc, 0, sizeof(disc)); for (;;) { switch (optget(argv, usage)) { case 'a': args = opt_info.arg; state.cmdflags |= CMD_POST; continue; case 'c': if ((count = opt_info.num) < 0) error(3, "argument count must be >= 0"); continue; case 'd': lastdir = lastdir->next = newof(0, Dir_t, 1, 0); lastdir->name = opt_info.arg; continue; case 'e': compile(opt_info.arg, 0); continue; case 'f': state.pattern = opt_info.arg; continue; case 'i': state.ignore = 1; continue; case 'l': state.localfs = 1; continue; case 'm': state.intermediate = 1; continue; case 'n': traverse = 0; continue; case 'p': state.ftwflags |= FTW_TWICE; continue; case 'q': state.cmdflags |= CMD_QUERY; continue; case 'r': state.ftwflags |= FTW_RECURSIVE; continue; case 's': if ((size = opt_info.num) < 0) error(3, "command size must be >= 0"); continue; case 't': state.cmdflags |= CMD_TRACE; continue; case 'x': state.errexit = opt_info.arg ? opt_info.num : EXIT_QUIT; continue; case 'z': if (s = sfgetr(sfstdin, '\n', 1)) { if (!(s = strdup(s))) error(ERROR_SYSTEM|3, "out of space"); n = state.snapshot.format.delim = *s++; state.snapshot.format.path = s; if (!(s = strchr(s, n))) { osnap: error(3, "invalid snapshot on standard input"); } *s++ = 0; if (!streq(state.snapshot.format.path, SNAPSHOT_ID)) goto osnap; state.snapshot.format.path = s; if (!(s = strchr(s, n))) goto osnap; *s++ = 0; state.snapshot.format.easy = s; if (!(s = strchr(s, n))) goto osnap; *s++ = 0; if (*(state.snapshot.format.hard = s)) { if (!(s = strchr(s, n))) goto osnap; *s = 0; } else state.snapshot.format.hard = 0; state.snapshot.sp = sfstdin; state.snapshot.prev = sfgetr(sfstdin, '\n', 0); } else { state.snapshot.format.path = SNAPSHOT_PATH; state.snapshot.format.easy = SNAPSHOT_EASY; state.snapshot.format.hard = SNAPSHOT_HARD; state.snapshot.format.delim = SNAPSHOT_DELIM[0]; } if (!(state.snapshot.tmp = sfstropen())) error(ERROR_SYSTEM|3, "out of space"); compile("sort:name;", 0); continue; case 'C': state.ftwflags |= FTW_NOSEEDOTDIR; continue; case 'D': error_info.trace = -opt_info.num; continue; case 'E': compile(opt_info.arg, 1); continue; case 'F': codes = opt_info.arg; continue; case 'G': disc.flags |= FIND_GENERATE; if (streq(opt_info.arg, "old")) disc.flags |= FIND_OLD; else if (streq(opt_info.arg, "gnu") || streq(opt_info.arg, "locate")) disc.flags |= FIND_GNU; else if (streq(opt_info.arg, "type")) disc.flags |= FIND_TYPE; else if (streq(opt_info.arg, "?")) { error(2, "formats are { default|dir type old gnu|locate }"); return 0; } else if (!streq(opt_info.arg, "-") && !streq(opt_info.arg, "default") && !streq(opt_info.arg, "dir")) error(3, "%s: invalid find codes format -- { default|dir type old gnu|locate } expected", opt_info.arg); continue; case 'H': state.ftwflags |= FTW_META|FTW_PHYSICAL; continue; case 'I': state.icase = 1; continue; case 'L': state.ftwflags &= ~(FTW_META|FTW_PHYSICAL|FTW_SEEDOTDIR); continue; case 'P': state.ftwflags &= ~FTW_META; state.ftwflags |= FTW_PHYSICAL; continue; case 'S': state.separator = *opt_info.arg; continue; case 'X': state.ftwflags |= FTW_MOUNT; continue; case '?': error(ERROR_USAGE|4, "%s", opt_info.arg); continue; case ':': error(2, "%s", opt_info.arg); continue; } break; } argv += opt_info.index; argc -= opt_info.index; if (error_info.errors) error(ERROR_USAGE|4, "%s", optusage(NiL)); /* * do it */ if (state.snapshot.tmp) sfprintf(sfstdout, "%c%s%c%s%c%s%c%s%c\n", state.snapshot.format.delim, SNAPSHOT_ID, state.snapshot.format.delim, state.snapshot.format.path, state.snapshot.format.delim, state.snapshot.format.easy, state.snapshot.format.delim, state.snapshot.format.hard ? state.snapshot.format.hard : "", state.snapshot.format.delim); if (x = exexpr(state.program, "begin", NiL, 0)) eval(x, NiL); if ((x = exexpr(state.program, "select", NiL, INTEGER)) || (x = exexpr(state.program, NiL, NiL, INTEGER))) state.select = x; if (!(state.ftwflags & FTW_PHYSICAL)) state.ftwflags &= ~FTW_DELAY; memset(&ftw, 0, sizeof(ftw)); ftw.path = ftw.name = ""; if (traverse) { if (x = exexpr(state.program, "action", NiL, 0)) state.action = x; if (x = exexpr(state.program, "sort", NiL, 0)) { state.sortkey = x; y = 0; for (;;) { switch (x->op) { case ',': y = x->data.operand.right; /*FALLTHROUGH*/ case '!': case '~': case S2B: case X2I: x = x->data.operand.left; continue; case ID: if (!(x = y)) break; y = 0; continue; default: error(3, "invalid sort identifier (op 0x%02x)", x->op); break; } break; } state.sort = order; } if (*argv && (*argv)[0] == '-' && (*argv)[1] == 0) { state.ftwflags |= FTW_LIST; argv++; argc--; } if (*argv || args || count || !(state.cmdflags & CMD_IMPLICIT)) { Cmddisc_t disc; CMDDISC(&disc, state.cmdflags, errorf); state.cmd = cmdopen(argv, count, size, args, &disc); state.ftwflags |= FTW_DOT; } else state.cmdflags &= ~CMD_IMPLICIT; if (codes && (disc.flags & FIND_GENERATE)) { char* p; Dir_t* dp; char pwd[PATH_MAX]; char tmp[PATH_MAX]; disc.version = FIND_VERSION; if (state.cmdflags & CMD_TRACE) disc.flags |= FIND_TYPE; if (state.cmdflags & CMD_QUERY) disc.flags |= FIND_OLD; disc.errorf = errorf; if (!(state.find = findopen(codes, NiL, NiL, &disc))) exit(2); if (disc.flags & FIND_TYPE) { state.act = ACT_CODETYPE; compile("_tw_init:mime;", 0); state.magicdisc.flags |= MAGIC_MIME; } else state.act = ACT_CODE; state.icase = 1; state.pattern = 0; state.sort = order; if (!state.program) compile("1", 0); if (!(state.sortkey = newof(0, Exnode_t, 1, 0)) || !(state.sortkey->data.variable.symbol = (Exid_t*)dtmatch(state.program->symbols, "name"))) error(ERROR_SYSTEM|3, "out of space"); state.sortkey->op = ID; s = p = 0; for (dp = (firstdir == lastdir) ? firstdir : firstdir->next; dp; dp = dp->next) { if (*(s = dp->name) == '/') sfsprintf(tmp, sizeof(tmp), "%s", s); else if (!p && !(p = getcwd(pwd, sizeof(pwd)))) error(ERROR_SYSTEM|3, "cannot determine pwd path"); else sfsprintf(tmp, sizeof(tmp), "%s/%s", p, s); pathcanon(tmp, sizeof(tmp), PATH_PHYSICAL); if (!(dp->name = strdup(tmp))) error(ERROR_SYSTEM|3, "out of space [PATH_PHYSICAL]"); } } else if (state.snapshot.tmp) state.act = ACT_SNAPSHOT; else if (state.cmdflags & CMD_IMPLICIT) state.act = ACT_CMDARG; else if (state.action == LIST) state.act = ACT_LIST; else if (state.action) state.act = ACT_EVAL; if (state.intermediate) { state.actII = state.act; state.act = ACT_INTERMEDIATE; } if (state.pattern) { disc.version = FIND_VERSION; if (state.icase) disc.flags |= FIND_ICASE; disc.errorf = errorf; disc.dirs = ap = av; if (firstdir != lastdir) firstdir = firstdir->next; do {*ap++ = firstdir->name;} while (firstdir = firstdir->next); *ap = 0; if (!(state.find = findopen(codes, state.pattern, NiL, &disc))) exit(1); state.ftwflags |= FTW_TOP; n = state.select == ALL ? state.act : ACT_EVAL; while (s = findread(state.find)) { switch (n) { case ACT_CMDARG: if ((i = cmdarg(state.cmd, s, strlen(s))) >= state.errexit) exit(i); break; case ACT_LIST: sfputr(sfstdout, s, '\n'); break; default: ftwalk(s, tw, state.ftwflags, NiL); break; } } } else if (state.ftwflags & FTW_LIST) { sfopen(sfstdin, NiL, "rt"); n = state.select == ALL && state.act == ACT_CMDARG; for (;;) { if (s = sfgetr(sfstdin, state.separator, 1)) len = sfvalue(sfstdin) - 1; else if (state.separator != '\n') { state.separator = '\n'; continue; } else if (s = sfgetr(sfstdin, state.separator, -1)) len = sfvalue(sfstdin); else break; if (!n) ftwalk(s, tw, state.ftwflags, NiL); else if ((i = cmdarg(state.cmd, s, len)) >= state.errexit) exit(i); } if (sferror(sfstdin)) error(ERROR_SYSTEM|2, "input read error"); } else if (firstdir == lastdir) ftwalk(firstdir->name, tw, state.ftwflags, state.sort); else { ap = av; while (firstdir = firstdir->next) *ap++ = firstdir->name; *ap = 0; ftwalk((char*)av, tw, state.ftwflags|FTW_MULTIPLE, state.sort); } if (state.cmd && (i = cmdflush(state.cmd)) >= state.errexit) exit(i); if (state.find && (findclose(state.find) || state.finderror)) exit(2); } else if (state.select) error_info.errors = eval(state.select, &ftw) == 0; if (x = exexpr(state.program, "end", NiL, 0)) eval(x, &ftw); if (sfsync(sfstdout)) error(ERROR_SYSTEM|2, "write error"); exit(error_info.errors != 0); }
char* pathprobe_20100601(const char* lang, const char* tool, const char* aproc, int op, char* path, size_t pathsize, char* attr, size_t attrsize) { char* proc = (char*)aproc; register char* p; register char* k; register char* x; register char** ap; int n; int v; int force; ssize_t r; char* e; char* np; char* nx; char* probe; const char* dirs; const char* dir; Proc_t* pp; Sfio_t* sp; char buf[PATH_MAX]; char cmd[PATH_MAX]; char exe[PATH_MAX]; char lib[PATH_MAX]; char ver[PATH_MAX]; char key[16]; char* arg[8]; long ops[2]; unsigned long ptime; struct stat st; struct stat ps; if (*proc != '/') { if (p = strchr(proc, ' ')) { strncopy(buf, proc, p - proc + 1); proc = buf; } if (!(proc = pathpath(proc, NiL, PATH_ABSOLUTE|PATH_REGULAR|PATH_EXECUTE, cmd, sizeof(cmd)))) proc = (char*)aproc; else if (p) { n = strlen(proc); strncopy(proc + n, p, PATH_MAX - n - 1); } } if (!path) { path = buf; pathsize = sizeof(buf); } probe = PROBE; x = lib + sizeof(lib) - 1; k = lib + sfsprintf(lib, x - lib, "lib/%s/", probe); p = k + sfsprintf(k, x - k, "%s/%s/", lang, tool); pathkey(lang, tool, proc, key, sizeof(key), attr, attrsize); if (op >= -2) { strncopy(p, key, x - p); if (pathpath(lib, "", PATH_ABSOLUTE, path, pathsize) && !stat(path, &st) && (st.st_mode & S_IWUSR)) return path == buf ? strdup(path) : path; } e = strncopy(p, probe, x - p); if (!pathpath(lib, "", PATH_ABSOLUTE|PATH_EXECUTE, path, pathsize) || stat(path, &ps)) return 0; for (;;) { ptime = ps.st_mtime; n = strlen(path); if (n < (PATH_MAX - 5)) { strcpy(path + n, ".ini"); if (!stat(path, &st) && st.st_size && ptime < (unsigned long)st.st_mtime) ptime = st.st_mtime; path[n] = 0; } np = path + n - (e - k); nx = path + PATH_MAX - 1; strncopy(np, probe, nx - np); if (!stat(path, &st)) break; /* * yes lib/probe/<lang>/<proc>/probe * no lib/probe/probe * * do a manual pathaccess() to find a dir with both */ sfsprintf(exe, sizeof(exe), "lib/%s/%s", probe, probe); dirs = pathbin(); for (;;) { if (!(dir = dirs)) return 0; dirs = pathcat(dir, ':', "..", exe, path, pathsize); pathcanon(path, pathsize, 0); if (*path == '/' && pathexists(path, PATH_REGULAR|PATH_EXECUTE)) { pathcat(dir, ':', "..", lib, path, pathsize); pathcanon(path, pathsize, 0); if (*path == '/' && pathexists(path, PATH_REGULAR|PATH_EXECUTE) && !stat(path, &ps)) break; } } } strncopy(p, key, x - p); p = np; x = nx; strcpy(exe, path); if (op >= -1 && (!(st.st_mode & S_ISUID) && ps.st_uid != geteuid() || rofs(path))) { if (!(p = getenv("HOME"))) return 0; p = path + sfsprintf(path, PATH_MAX - 1, "%s/.%s/%s/", p, probe, HOSTTYPE); } strncopy(p, k, x - p); force = 0; if (op >= 0 && !stat(path, &st)) { if (ptime <= (unsigned long)st.st_mtime || ptime <= (unsigned long)st.st_ctime) { /* * verify (<sep><name><sep><option><sep><value>)* header */ if (sp = sfopen(NiL, path, "r")) { if (x = sfgetr(sp, '\n', 1)) { while (*x && *x != ' ') x++; while (*x == ' ') x++; if (n = *x++) for (;;) { for (k = x; *x && *x != n; x++); if (!*x) break; *x++ = 0; for (p = x; *x && *x != n; x++); if (!*x) break; *x++ = 0; for (e = x; *x && *x != n; x++); if (!*x) break; *x++ = 0; if (streq(k, "VERSION")) { ap = arg; *ap++ = proc; *ap++ = p; *ap = 0; ops[0] = PROC_FD_DUP(1, 2, 0); ops[1] = 0; if (pp = procopen(proc, arg, NiL, ops, PROC_READ)) { if ((v = x - e) >= sizeof(ver)) v = sizeof(ver) - 1; for (k = p = ver;; k++) { if (k >= p) { if (v <= 0 || (r = read(pp->rfd, k, v)) <= 0) break; v -= r; p = k + r; } if (*k == '\n' || *k == '\r') break; if (*k == n) *k = ' '; } *k = 0; if (strcmp(ver, e)) { force = 1; error(0, "probe processor %s version \"%s\" changed -- expected \"%s\"", proc, ver, e); } procclose(pp); } break; } } } sfclose(sp); } if (!force) op = -1; } if (op >= 0 && (st.st_mode & S_IWUSR)) { if (op == 0) error(0, "%s probe information for %s language processor %s must be manually regenerated", tool, lang, proc); op = -1; force = 0; } } if (op >= 0) { ap = arg; *ap++ = exe; if (force) *ap++ = "-f"; if (op > 0) *ap++ = "-s"; *ap++ = (char*)lang; *ap++ = (char*)tool; *ap++ = proc; *ap = 0; if (procrun(exe, arg, 0)) return 0; if (eaccess(path, R_OK)) return 0; } return path == buf ? strdup(path) : path; }
static int exists(int op, char* pred, register char* args) { register int c; register int type; char* pptoken; long state; char file[MAXTOKEN + 1]; state = (pp.state & ~DISABLE); PUSH_STRING(args); pptoken = pp.token; pp.token = file; pp.state |= HEADER|PASSEOF; type = pplex(); pp.state &= ~HEADER; pp.token = pptoken; switch (type) { case T_STRING: case T_HEADER: break; default: error(1, "%s: \"...\" or <...> argument expected", pred); c = 0; goto done; } if (op == X_EXISTS) { if ((c = pplex()) == ',') { while ((c = pplex()) == T_STRING) { if (pathaccess(pp.token, file, NiL, 0, pp.path, MAXTOKEN + 1)) { pathcanon(pp.path, 0, 0); message((-2, "%s: %s found", pred, pp.path)); c = 1; goto done; } if ((c = pplex()) != ',') break; } if (c) error(1, "%s: \"...\" arguments expected", pred); strcpy(pp.path, file); message((-2, "%s: %s not found", pred, file)); c = 0; } else c = ppsearch(file, type, SEARCH_EXISTS) >= 0; } else { register struct ppfile* fp; fp = ppsetfile(file); c = fp->flags || fp->guard == INC_IGNORE; } done: while (pplex()); pp.state = state; return c; }