| 1 | /*- |
| 2 | * Copyright (c) 1992, 1993 |
| 3 | * The Regents of the University of California. All rights reserved. |
| 4 | * |
| 5 | * %sccs.include.redist.c% |
| 6 | */ |
| 7 | |
| 8 | #ifndef lint |
| 9 | static char sccsid[] = "@(#)util.c 8.2 (Berkeley) %G%"; |
| 10 | #endif /* not lint */ |
| 11 | |
| 12 | #include <sys/param.h> |
| 13 | #include <sys/stat.h> |
| 14 | #include <sys/wait.h> |
| 15 | |
| 16 | #include <ctype.h> |
| 17 | #include <err.h> |
| 18 | #include <errno.h> |
| 19 | #include <paths.h> |
| 20 | #include <signal.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #include "extern.h" |
| 27 | |
| 28 | char * |
| 29 | colon(cp) |
| 30 | char *cp; |
| 31 | { |
| 32 | if (*cp == ':') /* Leading colon is part of file name. */ |
| 33 | return (0); |
| 34 | |
| 35 | for (; *cp; ++cp) { |
| 36 | if (*cp == ':') |
| 37 | return (cp); |
| 38 | if (*cp == '/') |
| 39 | return (0); |
| 40 | } |
| 41 | return (0); |
| 42 | } |
| 43 | |
| 44 | void |
| 45 | verifydir(cp) |
| 46 | char *cp; |
| 47 | { |
| 48 | struct stat stb; |
| 49 | |
| 50 | if (!stat(cp, &stb)) { |
| 51 | if (S_ISDIR(stb.st_mode)) |
| 52 | return; |
| 53 | errno = ENOTDIR; |
| 54 | } |
| 55 | run_err("%s: %s", cp, strerror(errno)); |
| 56 | exit(1); |
| 57 | } |
| 58 | |
| 59 | int |
| 60 | okname(cp0) |
| 61 | char *cp0; |
| 62 | { |
| 63 | int c; |
| 64 | char *cp; |
| 65 | |
| 66 | cp = cp0; |
| 67 | do { |
| 68 | c = *cp; |
| 69 | if (c & 0200) |
| 70 | goto bad; |
| 71 | if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-') |
| 72 | goto bad; |
| 73 | } while (*++cp); |
| 74 | return (1); |
| 75 | |
| 76 | bad: warnx("%s: invalid user name", cp0); |
| 77 | return (0); |
| 78 | } |
| 79 | |
| 80 | int |
| 81 | susystem(s, userid) |
| 82 | int userid; |
| 83 | char *s; |
| 84 | { |
| 85 | sig_t istat, qstat; |
| 86 | int status, w; |
| 87 | pid_t pid; |
| 88 | |
| 89 | pid = vfork(); |
| 90 | switch (pid) { |
| 91 | case -1: |
| 92 | return (127); |
| 93 | |
| 94 | case 0: |
| 95 | (void)setuid(userid); |
| 96 | execl(_PATH_BSHELL, "sh", "-c", s, NULL); |
| 97 | _exit(127); |
| 98 | } |
| 99 | istat = signal(SIGINT, SIG_IGN); |
| 100 | qstat = signal(SIGQUIT, SIG_IGN); |
| 101 | if (waitpid(pid, &status, 0) < 0) |
| 102 | status = -1; |
| 103 | (void)signal(SIGINT, istat); |
| 104 | (void)signal(SIGQUIT, qstat); |
| 105 | return (status); |
| 106 | } |
| 107 | |
| 108 | BUF * |
| 109 | allocbuf(bp, fd, blksize) |
| 110 | BUF *bp; |
| 111 | int fd, blksize; |
| 112 | { |
| 113 | struct stat stb; |
| 114 | size_t size; |
| 115 | |
| 116 | if (fstat(fd, &stb) < 0) { |
| 117 | run_err("fstat: %s", strerror(errno)); |
| 118 | return (0); |
| 119 | } |
| 120 | size = roundup(stb.st_blksize, blksize); |
| 121 | if (size == 0) |
| 122 | size = blksize; |
| 123 | if (bp->cnt >= size) |
| 124 | return (bp); |
| 125 | if ((bp->buf = realloc(bp->buf, size)) == NULL) { |
| 126 | bp->cnt = 0; |
| 127 | run_err("%s", strerror(errno)); |
| 128 | return (0); |
| 129 | } |
| 130 | bp->cnt = size; |
| 131 | return (bp); |
| 132 | } |
| 133 | |
| 134 | void |
| 135 | lostconn(signo) |
| 136 | int signo; |
| 137 | { |
| 138 | if (!iamremote) |
| 139 | warnx("lost connection"); |
| 140 | exit(1); |
| 141 | } |