diff --git a/libexec/ftpd/popen.c b/libexec/ftpd/popen.c index b0b3b61330e7..abc4ec2cd5a2 100644 --- a/libexec/ftpd/popen.c +++ b/libexec/ftpd/popen.c @@ -1,201 +1,200 @@ /* * Copyright (c) 1988, 1993, 1994 * The Regents of the University of California. All rights reserved. * * This code is derived from software written by Ken Arnold and * published in UNIX Review, Vol. 6, No. 8. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef lint #if 0 static char sccsid[] = "@(#)popen.c 8.3 (Berkeley) 4/6/94"; #endif static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ #include #include #include #include #include #include #include #include #include #include #include "extern.h" #include "pathnames.h" #include #include -#include #define MAXUSRARGS 100 #define MAXGLOBARGS 1000 /* * Special version of popen which avoids call to shell. This ensures noone * may create a pipe to a hidden program as a side effect of a list or dir * command. */ static int *pids; static int fds; FILE * ftpd_popen(char *program, char *type) { char *cp; FILE *iop; int argc, gargc, pdes[2], pid; char **pop, *argv[MAXUSRARGS], *gargv[MAXGLOBARGS]; if (((*type != 'r') && (*type != 'w')) || type[1]) return (NULL); if (!pids) { if ((fds = getdtablesize()) <= 0) return (NULL); if ((pids = (int *)malloc((u_int)(fds * sizeof(int)))) == NULL) return (NULL); memset(pids, 0, fds * sizeof(int)); } if (pipe(pdes) < 0) return (NULL); /* break up string into pieces */ for (argc = 0, cp = program; argc < MAXUSRARGS; cp = NULL) { if (!(argv[argc++] = strtok(cp, " \t\n"))) break; } argv[argc - 1] = NULL; /* glob each piece */ gargv[0] = argv[0]; for (gargc = argc = 1; argv[argc] && gargc < (MAXGLOBARGS-1); argc++) { glob_t gl; int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE; memset(&gl, 0, sizeof(gl)); gl.gl_matchc = MAXGLOBARGS; flags |= GLOB_LIMIT; if (glob(argv[argc], flags, NULL, &gl)) gargv[gargc++] = strdup(argv[argc]); else for (pop = gl.gl_pathv; *pop && gargc < (MAXGLOBARGS-1); pop++) gargv[gargc++] = strdup(*pop); globfree(&gl); } gargv[gargc] = NULL; iop = NULL; fflush(NULL); pid = (strcmp(gargv[0], _PATH_LS) == 0) ? fork() : vfork(); switch(pid) { case -1: /* error */ (void)close(pdes[0]); (void)close(pdes[1]); goto pfree; /* NOTREACHED */ case 0: /* child */ if (*type == 'r') { if (pdes[1] != STDOUT_FILENO) { dup2(pdes[1], STDOUT_FILENO); (void)close(pdes[1]); } dup2(STDOUT_FILENO, STDERR_FILENO); /* stderr too! */ (void)close(pdes[0]); } else { if (pdes[0] != STDIN_FILENO) { dup2(pdes[0], STDIN_FILENO); (void)close(pdes[0]); } (void)close(pdes[1]); } if (strcmp(gargv[0], _PATH_LS) == 0) { /* Reset getopt for ls_main() */ optreset = optind = optopt = 1; /* Close syslogging to remove pwd.db missing msgs */ closelog(); /* Trigger to sense new /etc/localtime after chroot */ if (getenv("TZ") == NULL) { setenv("TZ", "", 0); tzset(); unsetenv("TZ"); tzset(); } exit(ls_main(gargc, gargv)); } execv(gargv[0], gargv); _exit(1); } /* parent; assume fdopen can't fail... */ if (*type == 'r') { iop = fdopen(pdes[0], type); (void)close(pdes[1]); } else { iop = fdopen(pdes[1], type); (void)close(pdes[0]); } pids[fileno(iop)] = pid; pfree: for (argc = 1; gargv[argc] != NULL; argc++) free(gargv[argc]); return (iop); } int ftpd_pclose(FILE *iop) { int fdes, omask, status; pid_t pid; /* * pclose returns -1 if stream is not associated with a * `popened' command, or, if already `pclosed'. */ if (pids == 0 || pids[fdes = fileno(iop)] == 0) return (-1); (void)fclose(iop); omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); while ((pid = waitpid(pids[fdes], &status, 0)) < 0 && errno == EINTR) continue; (void)sigsetmask(omask); pids[fdes] = 0; if (pid < 0) return (pid); if (WIFEXITED(status)) return (WEXITSTATUS(status)); return (1); } diff --git a/libexec/rpc.rquotad/rquotad.c b/libexec/rpc.rquotad/rquotad.c index acc1e64a7bb0..653ea15257b2 100644 --- a/libexec/rpc.rquotad/rquotad.c +++ b/libexec/rpc.rquotad/rquotad.c @@ -1,328 +1,327 @@ /* * by Manuel Bouyer (bouyer@ensta.fr) * * There is no copyright, you can use it as you want. */ #ifndef lint static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include -#include #include #include #include #include #include void rquota_service(struct svc_req *request, SVCXPRT *transp); void sendquota(struct svc_req *request, SVCXPRT *transp); void printerr_reply(SVCXPRT *transp); void initfs(void); int getfsquota(long id, char *path, struct dqblk *dqblk); int hasquota(struct fstab *fs, char **qfnamep); /* * structure containing informations about ufs filesystems * initialised by initfs() */ struct fs_stat { struct fs_stat *fs_next; /* next element */ char *fs_file; /* mount point of the filesystem */ char *qfpathname; /* pathname of the quota file */ dev_t st_dev; /* device of the filesystem */ } fs_stat; struct fs_stat *fs_begin = NULL; int from_inetd = 1; void cleanup(int sig) { (void) rpcb_unset(RQUOTAPROG, RQUOTAVERS, NULL); exit(0); } int main(int argc, char *argv[]) { SVCXPRT *transp; int ok; struct sockaddr_storage from; int fromlen; fromlen = sizeof(from); if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) { from_inetd = 0; } if (!from_inetd) { daemon(0, 0); (void) rpcb_unset(RQUOTAPROG, RQUOTAVERS, NULL); (void) signal(SIGINT, cleanup); (void) signal(SIGTERM, cleanup); (void) signal(SIGHUP, cleanup); } openlog("rpc.rquotad", LOG_CONS|LOG_PID, LOG_DAEMON); /* create and register the service */ if (from_inetd) { transp = svc_tli_create(0, NULL, NULL, 0, 0); if (transp == NULL) { syslog(LOG_ERR, "couldn't create udp service."); exit(1); } ok = svc_reg(transp, RQUOTAPROG, RQUOTAVERS, rquota_service, NULL); } else ok = svc_create(rquota_service, RQUOTAPROG, RQUOTAVERS, "udp"); if (!ok) { syslog(LOG_ERR, "unable to register (RQUOTAPROG, RQUOTAVERS, %s)", (!from_inetd)?"udp":"(inetd)"); exit(1); } initfs(); /* init the fs_stat list */ svc_run(); syslog(LOG_ERR, "svc_run returned"); exit(1); } void rquota_service(struct svc_req *request, SVCXPRT *transp) { switch (request->rq_proc) { case NULLPROC: (void)svc_sendreply(transp, (xdrproc_t)xdr_void, (char *)NULL); break; case RQUOTAPROC_GETQUOTA: case RQUOTAPROC_GETACTIVEQUOTA: sendquota(request, transp); break; default: svcerr_noproc(transp); break; } if (from_inetd) exit(0); } /* read quota for the specified id, and send it */ void sendquota(struct svc_req *request, SVCXPRT *transp) { struct getquota_args getq_args; struct getquota_rslt getq_rslt; struct dqblk dqblk; struct timeval timev; bzero((char *)&getq_args, sizeof(getq_args)); if (!svc_getargs(transp, (xdrproc_t)xdr_getquota_args, &getq_args)) { svcerr_decode(transp); return; } if (request->rq_cred.oa_flavor != AUTH_UNIX) { /* bad auth */ getq_rslt.status = Q_EPERM; } else if (!getfsquota(getq_args.gqa_uid, getq_args.gqa_pathp, &dqblk)) { /* failed, return noquota */ getq_rslt.status = Q_NOQUOTA; } else { gettimeofday(&timev, NULL); getq_rslt.status = Q_OK; getq_rslt.getquota_rslt_u.gqr_rquota.rq_active = TRUE; getq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize = DEV_BSIZE; getq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit = dqblk.dqb_bhardlimit; getq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit = dqblk.dqb_bsoftlimit; getq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks = dqblk.dqb_curblocks; getq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit = dqblk.dqb_ihardlimit; getq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit = dqblk.dqb_isoftlimit; getq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles = dqblk.dqb_curinodes; getq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft = dqblk.dqb_btime - timev.tv_sec; getq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft = dqblk.dqb_itime - timev.tv_sec; } if (!svc_sendreply(transp, (xdrproc_t)xdr_getquota_rslt, &getq_rslt)) { svcerr_systemerr(transp); } if (!svc_freeargs(transp, (xdrproc_t)xdr_getquota_args, &getq_args)) { syslog(LOG_ERR, "unable to free arguments"); exit(1); } } void printerr_reply(SVCXPRT *transp) /* when a reply to a request failed */ { char name[INET6_ADDRSTRLEN]; struct sockaddr *caller; int save_errno; save_errno = errno; caller = (struct sockaddr *)svc_getrpccaller(transp)->buf; getnameinfo(caller, caller->sa_len, name, sizeof (name), NULL, 0, NI_NUMERICHOST); errno = save_errno; if (errno == 0) syslog(LOG_ERR, "couldn't send reply to %s", name); else syslog(LOG_ERR, "couldn't send reply to %s: %m", name); } /* initialise the fs_tab list from entries in /etc/fstab */ void initfs(void) { struct fs_stat *fs_current = NULL; struct fs_stat *fs_next = NULL; char *qfpathname; struct fstab *fs; struct stat st; setfsent(); while ((fs = getfsent())) { if (strcmp(fs->fs_vfstype, "ufs")) continue; if (!hasquota(fs, &qfpathname)) continue; fs_current = (struct fs_stat *) malloc(sizeof(struct fs_stat)); fs_current->fs_next = fs_next; /* next element */ fs_current->fs_file = malloc(sizeof(char) * (strlen(fs->fs_file) + 1)); strcpy(fs_current->fs_file, fs->fs_file); fs_current->qfpathname = malloc(sizeof(char) * (strlen(qfpathname) + 1)); strcpy(fs_current->qfpathname, qfpathname); stat(fs_current->fs_file, &st); fs_current->st_dev = st.st_dev; fs_next = fs_current; } endfsent(); fs_begin = fs_current; } /* * gets the quotas for id, filesystem path. * Return 0 if fail, 1 otherwise */ int getfsquota(long id, char *path, struct dqblk *dqblk) { struct stat st_path; struct fs_stat *fs; int qcmd, fd, ret = 0; if (stat(path, &st_path) < 0) return (0); qcmd = QCMD(Q_GETQUOTA, USRQUOTA); for (fs = fs_begin; fs != NULL; fs = fs->fs_next) { /* where the devise is the same as path */ if (fs->st_dev != st_path.st_dev) continue; /* find the specified filesystem. get and return quota */ if (quotactl(fs->fs_file, qcmd, id, dqblk) == 0) return (1); if ((fd = open(fs->qfpathname, O_RDONLY)) < 0) { syslog(LOG_ERR, "open error: %s: %m", fs->qfpathname); return (0); } if (lseek(fd, (off_t)(id * sizeof(struct dqblk)), L_SET) == (off_t)-1) { close(fd); return (1); } switch (read(fd, dqblk, sizeof(struct dqblk))) { case 0: /* * Convert implicit 0 quota (EOF) * into an explicit one (zero'ed dqblk) */ bzero(dqblk, sizeof(struct dqblk)); ret = 1; break; case sizeof(struct dqblk): /* OK */ ret = 1; break; default: /* ERROR */ syslog(LOG_ERR, "read error: %s: %m", fs->qfpathname); close(fd); return (0); } close(fd); } return (ret); } /* * Check to see if a particular quota is to be enabled. * Comes from quota.c, NetBSD 0.9 */ int hasquota(struct fstab *fs, char **qfnamep) { static char initname, usrname[100]; static char buf[BUFSIZ]; char *opt, *cp; char *qfextension[] = INITQFNAMES; if (!initname) { sprintf(usrname, "%s%s", qfextension[USRQUOTA], QUOTAFILENAME); initname = 1; } strcpy(buf, fs->fs_mntops); for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) { if ((cp = index(opt, '='))) *cp++ = '\0'; if (strcmp(opt, usrname) == 0) break; } if (!opt) return (0); if (cp) { *qfnamep = cp; return (1); } sprintf(buf, "%s/%s.%s", fs->fs_file, QUOTAFILENAME, qfextension[USRQUOTA]); *qfnamep = buf; return (1); } diff --git a/usr.bin/lock/lock.c b/usr.bin/lock/lock.c index 3d0c06dd76bb..15d994147e1f 100644 --- a/usr.bin/lock/lock.c +++ b/usr.bin/lock/lock.c @@ -1,286 +1,285 @@ /* * Copyright (c) 1980, 1987, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Bob Toxen. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef lint static const char copyright[] = "@(#) Copyright (c) 1980, 1987, 1993\n\ The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #ifndef lint #if 0 static char sccsid[] = "@(#)lock.c 8.1 (Berkeley) 6/6/93"; #endif #endif /* not lint */ #include __FBSDID("$FreeBSD$"); /* * Lock a terminal up until the given key is entered or the given * interval times out. * * Timeout interval is by default TIMEOUT, it can be changed with * an argument of the form -time where time is in minutes */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include -#include #define TIMEOUT 15 void quit(int); void bye(int); void hi(int); static void usage(void); struct timeval timeout; struct timeval zerotime; struct termios tty, ntty; long nexttime; /* keep the timeout time */ int no_timeout; /* lock terminal forever */ int vtyunlock; /* Unlock flag and code. */ /*ARGSUSED*/ int main(int argc, char **argv) { struct passwd *pw; struct timeval timval; time_t timval_sec; struct itimerval ntimer, otimer; struct tm *timp; int ch, failures, sectimeout, usemine, vtylock; char *ap, *mypw, *ttynam, *tzn; char hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ]; openlog("lock", LOG_ODELAY, LOG_AUTH); sectimeout = TIMEOUT; mypw = NULL; usemine = 0; no_timeout = 0; vtylock = 0; while ((ch = getopt(argc, argv, "npt:v")) != -1) switch((char)ch) { case 't': if ((sectimeout = atoi(optarg)) <= 0) errx(1, "illegal timeout value"); break; case 'p': usemine = 1; if (!(pw = getpwuid(getuid()))) errx(1, "unknown uid %d", getuid()); mypw = strdup(pw->pw_passwd); break; case 'n': no_timeout = 1; break; case 'v': vtylock = 1; break; case '?': default: usage(); } timeout.tv_sec = sectimeout * 60; setuid(getuid()); /* discard privs */ if (tcgetattr(0, &tty)) /* get information for header */ exit(1); gethostname(hostname, sizeof(hostname)); if (!(ttynam = ttyname(0))) errx(1, "not a terminal?"); if (gettimeofday(&timval, (struct timezone *)NULL)) err(1, "gettimeofday"); nexttime = timval.tv_sec + (sectimeout * 60); timval_sec = timval.tv_sec; timp = localtime(&timval_sec); ap = asctime(timp); tzn = timp->tm_zone; (void)signal(SIGINT, quit); (void)signal(SIGQUIT, quit); ntty = tty; ntty.c_lflag &= ~ECHO; (void)tcsetattr(0, TCSADRAIN|TCSASOFT, &ntty); if (!mypw) { /* get key and check again */ (void)printf("Key: "); if (!fgets(s, sizeof(s), stdin) || *s == '\n') quit(0); (void)printf("\nAgain: "); /* * Don't need EOF test here, if we get EOF, then s1 != s * and the right things will happen. */ (void)fgets(s1, sizeof(s1), stdin); (void)putchar('\n'); if (strcmp(s1, s)) { (void)printf("\07lock: passwords didn't match.\n"); (void)tcsetattr(0, TCSADRAIN|TCSASOFT, &tty); exit(1); } s[0] = '\0'; mypw = s1; } /* set signal handlers */ (void)signal(SIGINT, hi); (void)signal(SIGQUIT, hi); (void)signal(SIGTSTP, hi); (void)signal(SIGALRM, bye); ntimer.it_interval = zerotime; ntimer.it_value = timeout; if (!no_timeout) setitimer(ITIMER_REAL, &ntimer, &otimer); if (vtylock) { /* * If this failed, we want to err out; warn isn't good * enough, since we don't want the user to think that * everything is nice and locked because they got a * "Key:" prompt. */ if (ioctl(0, VT_LOCKSWITCH, &vtylock) == -1) { (void)tcsetattr(0, TCSADRAIN|TCSASOFT, &tty); err(1, "locking vty"); } vtyunlock = 0x2; } /* header info */ (void)printf("lock: %s on %s.", ttynam, hostname); if (no_timeout) (void)printf(" no timeout."); else (void)printf(" timeout in %d minute%s.", sectimeout, sectimeout != 1 ? "s" : ""); if (vtylock) (void)printf(" vty locked."); (void)printf("\ntime now is %.20s%s%s", ap, tzn, ap + 19); failures = 0; for (;;) { (void)printf("Key: "); if (!fgets(s, sizeof(s), stdin)) { clearerr(stdin); hi(0); continue; } if (usemine) { s[strlen(s) - 1] = '\0'; if (!strcmp(mypw, crypt(s, mypw))) break; } else if (!strcmp(s, s1)) break; (void)printf("\07\n"); failures++; if (getuid() == 0) syslog(LOG_NOTICE, "%d ROOT UNLOCK FAILURE%s (%s on %s)", failures, failures > 1 ? "S": "", ttynam, hostname); if (tcgetattr(0, &ntty)) exit(1); sleep(1); /* to discourage guessing */ } if (getuid() == 0) syslog(LOG_NOTICE, "ROOT UNLOCK ON hostname %s port %s", hostname, ttynam); quit(0); return(0); /* not reached */ } static void usage(void) { (void)fprintf(stderr, "usage: lock [-npv] [-t timeout]\n"); exit(1); } void hi(int signo __unused) { struct timeval timval; if (!gettimeofday(&timval, (struct timezone *)NULL)) { (void)printf("lock: type in the unlock key. "); if (no_timeout) { (void)putchar('\n'); } else { (void)printf("timeout in %ld:%ld minutes\n", (nexttime - timval.tv_sec) / 60, (nexttime - timval.tv_sec) % 60); } } } void quit(int signo __unused) { (void)putchar('\n'); (void)tcsetattr(0, TCSADRAIN|TCSASOFT, &tty); if (vtyunlock) (void)ioctl(0, VT_LOCKSWITCH, &vtyunlock); exit(0); } void bye(int signo __unused) { if (!no_timeout) { (void)tcsetattr(0, TCSADRAIN|TCSASOFT, &tty); if (vtyunlock) (void)ioctl(0, VT_LOCKSWITCH, &vtyunlock); (void)printf("lock: timeout\n"); exit(1); } }