static void showquotas(int type, u_long id, const char *name) { struct quotause *qup; struct quotause *quplist; const char *msgi, *msgb; const char *nam; int lines = 0; static time_t now; if (now == 0) time(&now); quplist = getprivs(id, type); for (qup = quplist; qup; qup = qup->next) { if (!vflag && qup->dqblk.dqb_isoftlimit == 0 && qup->dqblk.dqb_ihardlimit == 0 && qup->dqblk.dqb_bsoftlimit == 0 && qup->dqblk.dqb_bhardlimit == 0) continue; msgi = NULL; if (qup->dqblk.dqb_ihardlimit && qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_ihardlimit) msgi = "File limit reached on"; else if (qup->dqblk.dqb_isoftlimit && qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_isoftlimit) { if (qup->dqblk.dqb_itime > now) msgi = "In file grace period on"; else msgi = "Over file quota on"; } msgb = NULL; if (qup->dqblk.dqb_bhardlimit && qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bhardlimit) msgb = "Block limit reached on"; else if (qup->dqblk.dqb_bsoftlimit && qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bsoftlimit) { if (qup->dqblk.dqb_btime > now) msgb = "In block grace period on"; else msgb = "Over block quota on"; } if (qflag) { if ((msgi != NULL || msgb != NULL) && lines++ == 0) heading(type, id, name, ""); if (msgi != NULL) printf("\t%s %s\n", msgi, qup->fsname); if (msgb != NULL) printf("\t%s %s\n", msgb, qup->fsname); continue; } if (vflag || qup->dqblk.dqb_curblocks || qup->dqblk.dqb_curinodes) { if (lines++ == 0) heading(type, id, name, ""); nam = qup->fsname; if (strlen(qup->fsname) > 15) { printf("%s\n", qup->fsname); nam = ""; } printf("%15s%8lu%c%7lu%8lu%8s" , nam , (u_long) (dbtob(qup->dqblk.dqb_curblocks) / 1024) , (msgb == NULL) ? ' ' : '*' , (u_long) (dbtob(qup->dqblk.dqb_bsoftlimit) / 1024) , (u_long) (dbtob(qup->dqblk.dqb_bhardlimit) / 1024) , (msgb == NULL) ? "" :timeprt(qup->dqblk.dqb_btime)); printf("%8lu%c%7lu%8lu%8s\n" , (u_long)qup->dqblk.dqb_curinodes , (msgi == NULL) ? ' ' : '*' , (u_long)qup->dqblk.dqb_isoftlimit , (u_long)qup->dqblk.dqb_ihardlimit , (msgi == NULL) ? "" : timeprt(qup->dqblk.dqb_itime) ); continue; } } if (!qflag && lines == 0) heading(type, id, name, "none"); }
static int showquotas(int type, u_long id, const char *name) { struct quotause *qup; struct quotause *quplist; const char *msgi, *msgb; const char *nam; char *bgrace = NULL, *igrace = NULL; int lines = 0, overquota = 0; static time_t now; if (now == 0) time(&now); quplist = getprivs(id, type); for (qup = quplist; qup; qup = qup->next) { msgi = NULL; if (qup->dqblk.dqb_ihardlimit && qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_ihardlimit) { overquota++; msgi = "File limit reached on"; } else if (qup->dqblk.dqb_isoftlimit && qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_isoftlimit) { overquota++; if (qup->dqblk.dqb_itime > now) msgi = "In file grace period on"; else msgi = "Over file quota on"; } msgb = NULL; if (qup->dqblk.dqb_bhardlimit && qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bhardlimit) { overquota++; msgb = "Block limit reached on"; } else if (qup->dqblk.dqb_bsoftlimit && qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bsoftlimit) { overquota++; if (qup->dqblk.dqb_btime > now) msgb = "In block grace period on"; else msgb = "Over block quota on"; } if (rflag) { showrawquotas(type, id, qup); continue; } if (!vflag && qup->dqblk.dqb_isoftlimit == 0 && qup->dqblk.dqb_ihardlimit == 0 && qup->dqblk.dqb_bsoftlimit == 0 && qup->dqblk.dqb_bhardlimit == 0) continue; if (qflag) { if ((msgi != NULL || msgb != NULL) && lines++ == 0) heading(type, id, name, ""); if (msgi != NULL) printf("\t%s %s\n", msgi, qup->fsname); if (msgb != NULL) printf("\t%s %s\n", msgb, qup->fsname); continue; } if (!vflag && qup->dqblk.dqb_curblocks == 0 && qup->dqblk.dqb_curinodes == 0) continue; if (lines++ == 0) heading(type, id, name, ""); nam = qup->fsname; if (strlen(qup->fsname) > 15) { printf("%s\n", qup->fsname); nam = ""; } printf("%-15s", nam); if (hflag) { prthumanval(7, dbtob(qup->dqblk.dqb_curblocks)); printf("%c", (msgb == NULL) ? ' ' : '*'); prthumanval(7, dbtob(qup->dqblk.dqb_bsoftlimit)); prthumanval(7, dbtob(qup->dqblk.dqb_bhardlimit)); } else { printf(" %7ju%c %7ju %7ju", (uintmax_t)dbtob(qup->dqblk.dqb_curblocks) / 1024, (msgb == NULL) ? ' ' : '*', (uintmax_t)dbtob(qup->dqblk.dqb_bsoftlimit) / 1024, (uintmax_t)dbtob(qup->dqblk.dqb_bhardlimit) / 1024); } if (msgb != NULL) bgrace = timeprt(qup->dqblk.dqb_btime); if (msgi != NULL) igrace = timeprt(qup->dqblk.dqb_itime); printf("%8s %6ju%c %6ju %6ju%8s\n" , (msgb == NULL) ? "" : bgrace , (uintmax_t)qup->dqblk.dqb_curinodes , (msgi == NULL) ? ' ' : '*' , (uintmax_t)qup->dqblk.dqb_isoftlimit , (uintmax_t)qup->dqblk.dqb_ihardlimit , (msgi == NULL) ? "" : igrace ); if (msgb != NULL) free(bgrace); if (msgi != NULL) free(igrace); } if (!qflag && !rflag && lines == 0) heading(type, id, name, "none"); return (overquota); }
int main(int argc, char **argv) { struct quotause *qup, *protoprivs, *curprivs; long id, protoid; long long lim; int i, quotatype, range, tmpfd; uid_t startuid, enduid; u_int32_t *limp; char *protoname, *cp, *oldoptarg, ch; int eflag = 0, tflag = 0, pflag = 0; char *fspath = NULL; char buf[MAXLOGNAME]; if (argc < 2) usage(); if (getuid()) errx(1, "permission denied"); quotatype = USRQUOTA; protoprivs = NULL; curprivs = NULL; protoname = NULL; while ((ch = getopt(argc, argv, "ugtf:p:e:")) != -1) { switch(ch) { case 'f': fspath = optarg; break; case 'p': protoname = optarg; pflag++; break; case 'g': quotatype = GRPQUOTA; break; case 'u': quotatype = USRQUOTA; break; case 't': tflag++; break; case 'e': if ((qup = malloc(sizeof(*qup))) == NULL) errx(2, "out of memory"); bzero(qup, sizeof(*qup)); i = 0; oldoptarg = optarg; for (cp = optarg; (cp = strsep(&optarg, ":")) != NULL; i++) { if (cp != oldoptarg) *(cp - 1) = ':'; limp = NULL; switch (i) { case 0: strlcpy(qup->fsname, cp, sizeof(qup->fsname)); break; case 1: limp = &qup->dqblk.dqb_bsoftlimit; break; case 2: limp = &qup->dqblk.dqb_bhardlimit; break; case 3: limp = &qup->dqblk.dqb_isoftlimit; break; case 4: limp = &qup->dqblk.dqb_ihardlimit; break; default: warnx("incorrect quota specification: " "%s", oldoptarg); usage(); break; /* XXX: report an error */ } if (limp != NULL) { lim = strtoll(cp, NULL, 10); if (lim < 0 || lim > UINT_MAX) errx(1, "invalid limit value: " "%lld", lim); *limp = (u_int32_t)lim; } } qup->dqblk.dqb_bsoftlimit = btodb((off_t)qup->dqblk.dqb_bsoftlimit * 1024); qup->dqblk.dqb_bhardlimit = btodb((off_t)qup->dqblk.dqb_bhardlimit * 1024); if (protoprivs == NULL) { protoprivs = curprivs = qup; } else { curprivs->next = qup; curprivs = qup; } eflag++; pflag++; break; default: usage(); } } argc -= optind; argv += optind; if (pflag) { if (protoprivs == NULL) { if ((protoid = getentry(protoname, quotatype)) == -1) exit(1); protoprivs = getprivs(protoid, quotatype, fspath); for (qup = protoprivs; qup; qup = qup->next) { qup->dqblk.dqb_btime = 0; qup->dqblk.dqb_itime = 0; } } for (; argc-- > 0; argv++) { if (strspn(*argv, "0123456789-") == strlen(*argv) && (cp = strchr(*argv, '-')) != NULL) { *cp++ = '\0'; startuid = atoi(*argv); enduid = atoi(cp); if (enduid < startuid) errx(1, "ending uid (%d) must be >= starting uid (%d) when using uid ranges", enduid, startuid); range = 1; } else { startuid = enduid = 0; range = 0; } for ( ; startuid <= enduid; startuid++) { if (range) snprintf(buf, sizeof(buf), "%d", startuid); else snprintf(buf, sizeof(buf), "%s", *argv); if ((id = getentry(buf, quotatype)) < 0) continue; if (eflag) { for (qup = protoprivs; qup; qup = qup->next) { curprivs = getprivs(id, quotatype, qup->fsname); if (curprivs == NULL) continue; strcpy(qup->qfname, curprivs->qfname); strcpy(qup->fsname, curprivs->fsname); } } putprivs(id, quotatype, protoprivs); } } exit(0); } tmpfd = mkstemp(tmpfil); fchown(tmpfd, getuid(), getgid()); if (tflag) { protoprivs = getprivs(0, quotatype, fspath); if (writetimes(protoprivs, tmpfd, quotatype) == 0) exit(1); if (editit(tmpfil) && readtimes(protoprivs, tmpfil)) putprivs(0, quotatype, protoprivs); freeprivs(protoprivs); close(tmpfd); unlink(tmpfil); exit(0); } for ( ; argc > 0; argc--, argv++) { if ((id = getentry(*argv, quotatype)) == -1) continue; curprivs = getprivs(id, quotatype, fspath); if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0) continue; if (editit(tmpfil) && readprivs(curprivs, tmpfil)) putprivs(id, quotatype, curprivs); freeprivs(curprivs); } close(tmpfd); unlink(tmpfil); exit(0); }
int main(int argc, char **argv) { uid_t uid; char *basename; int opt; int i; int tmpfd = -1; basename = argv[0]; if (argc < 2) { usage(); } if (quotactl(Q_SYNC, (char *)NULL, 0, (caddr_t)NULL) < 0 && errno == EINVAL) { (void) printf("Warning: " "Quotas are not compiled into this kernel\n"); (void) sleep(3); } if (getuid()) { (void) fprintf(stderr, "%s: permission denied\n", basename); exit(32); } setupfs(); if (fsqlist == NULL) { (void) fprintf(stderr, "%s: no UFS filesystems with %s file\n", MNTTAB, QFNAME); exit(32); } tmpfd = mkstemp(tmpfil); if (tmpfd == -1 || fchown(tmpfd, getuid(), getgid()) == -1) { fprintf(stderr, "failure in temporary file %s\n", tmpfil); exit(32); } (void) close(tmpfd); while ((opt = getopt(argc, argv, "p:tV")) != EOF) switch (opt) { case 't': gettimes(0); if (editit()) puttimes(0); (void) unlink(tmpfil); exit(0); /*NOTREACHED*/ case 'p': uid = getentry(optarg); if (uid > MAXUID) { (void) unlink(tmpfil); exit(32); } getprivs(uid); if (optind == argc) { (void) unlink(tmpfil); usage(); } for (i = optind; i < argc; i++) { uid = getentry(argv[i]); if (uid > MAXUID) { (void) unlink(tmpfil); exit(32); } getdiscq(uid); putprivs(uid); } (void) unlink(tmpfil); exit(0); /*NOTREACHED*/ case 'V': /* Print command line */ { char *optt; int optc; (void) printf("edquota -F UFS"); for (optc = 1; optc < argc; optc++) { optt = argv[optc]; if (optt) (void) printf(" %s ", optt); } (void) putchar('\n'); } break; case '?': usage(); } for (i = optind; i < argc; i++) { uid = getentry(argv[i]); if (uid > MAXUID) continue; getprivs(uid); if (editit()) putprivs(uid); if (uid == 0) { (void) printf("edquota: Note that uid 0's quotas " "are used as default values for other users,\n"); (void) printf("not as a limit on the uid 0 user.\n"); } } (void) unlink(tmpfil); return (0); }
int main( int argc, char *argv[] ) { sigset_t newmask; char c; extern char *optarg; extern int optind, opterr, optopt; char *cfgfile = HTUN_DEFAULT_CFGFILE; char *tunfile = NULL; char *logfile = NULL; unsigned short port=0; int foreground = 0; int dont_route = 0; int config_test_only = 0; int debug = 0; unsigned int logflags = 0; dropprivs(""); while( (c=getopt(argc, argv, "rdfc:vht:l:p:o")) != -1 ) { switch(c) { case 'r': dont_route = 1; break; /* #ifdef _DEBUG */ case 'd': debug = 1; break; /* #endif */ case 'o': debug = 1; config_test_only = 1; break; case 'f': foreground = 1; break; case 'c': cfgfile = optarg; break; case 'v': fprintf( stderr, "HTun " VERSION "\n" "(c) 2002 Moshe Jacobson <*****@*****.**>\n" " and Ola Nordstrom <*****@*****.**>\n" "This program is distributed under the GNU General Public License.\n" "for details, see the LICENSE file packaged with htun.\n"); return EXIT_SUCCESS; case 't': tunfile = optarg; break; case 'l': logfile = optarg; break; case 'p': port = htons((unsigned short)strtoul(optarg,NULL,0)); if(!port) { fprintf( stderr, "Invalid port specified: %s.\n", argv[optind] ); return EXIT_FAILURE; } break; case 'h': usage(); return EXIT_SUCCESS; default: usage(); return EXIT_FAILURE; } } /* Read the config */ if( (config=read_config(cfgfile)) == NULL ) { fprintf( stderr, "Fatal: Reading cfgfile \"%s\": %s\n", cfgfile, strerror(errno) ); return EXIT_FAILURE; } if( config_test_only == 1 ) { log = log_open("-", LOG_STDERR|LOG_NODATE|LOG_DEBUG|LOG_NOLVL|LOG_FUNC); print_config(config); log_close(log); return EXIT_SUCCESS; } /* Now override config values with cmdline values */ strcpy(config->cfgfile,cfgfile); if( tunfile ) strncpy(config->tunfile,tunfile,PATH_MAX); if( logfile ) strncpy(config->logfile,logfile,PATH_MAX); if( port ) { if( config->is_server ) config->u.s.server_ports[0] = htons(port); else config->u.c.proxy_port = (port); } if( dont_route ) config->u.c.do_routing = 0; if( debug ) config->debug = 1; /* Open the log file */ if( config->debug ) logflags |= LOG_DEBUG; logflags |= LOG_FUNC; getprivs("opening logfile"); if( !(log=log_open( config->logfile, logflags )) ) { fprintf( stderr, "Warning: Could not open logfile %s: %s\n", config->logfile, strerror(errno) ); } dropprivs("logfile opened"); /* Daemonize unless -f or -l - have been specified */ if( !foreground && strcmp(config->logfile,"-") ) daemonize(); lprintf( log, INFO, "HTun " VERSION " started." ); init_signames(); /* Ignore SIGPIPE. We will use errno=EPIPE instead. */ signal(SIGPIPE,SIG_IGN); signal(SIGWINCH,SIG_IGN); /* Mask TERM, HUP, INT, and USR1 */ sigemptyset(&newmask); sigaddset(&newmask, SIGINT); sigaddset(&newmask, SIGHUP); sigaddset(&newmask, SIGTERM); sigaddset(&newmask, SIGUSR1); sigaddset(&newmask, SIGUSR2); sigaddset(&newmask, SIGALRM); sigaddset(&newmask, SIGCONT); sigaddset(&newmask, SIGTSTP); sigaddset(&newmask, SIGWINCH); sigprocmask( SIG_BLOCK, &newmask, NULL ); return config->is_server ? server_main() : client_main(); }
int main(int argc, char *argv[]) { struct quotause *qup, *protoprivs, *curprivs; u_int id, protoid; int quotatype, tmpfd; char *protoname = NULL; int ch; int tflag = 0, pflag = 0; if (argc < 2) usage(); if (getuid()) errx(1, "%s", strerror(EPERM)); quotatype = USRQUOTA; while ((ch = getopt(argc, argv, "ugtp:")) != -1) { switch(ch) { case 'p': protoname = optarg; pflag++; break; case 'g': quotatype = GRPQUOTA; break; case 'u': quotatype = USRQUOTA; break; case 't': tflag++; break; default: usage(); } } argc -= optind; argv += optind; if (pflag) { if (getentry(protoname, quotatype, &protoid) == -1) exit(1); protoprivs = getprivs(protoid, quotatype); for (qup = protoprivs; qup; qup = qup->next) { qup->dqblk.dqb_btime = 0; qup->dqblk.dqb_itime = 0; } while (argc-- > 0) { if (getentry(*argv++, quotatype, &id) == -1) continue; putprivs(id, quotatype, protoprivs); } exit(0); } if ((tmpfd = mkstemp(tmpfil)) == -1) errx(1, "%s", tmpfil); if (tflag) { protoprivs = getprivs(0, quotatype); if (writetimes(protoprivs, tmpfd, quotatype) == 0) { unlink(tmpfil); exit(1); } if (editit(tmpfil) == -1) { unlink(tmpfil); err(1, "error starting editor"); } if (readtimes(protoprivs, tmpfd)) putprivs(0, quotatype, protoprivs); freeprivs(protoprivs); unlink(tmpfil); exit(0); } for ( ; argc > 0; argc--, argv++) { if (getentry(*argv, quotatype, &id) == -1) continue; curprivs = getprivs(id, quotatype); if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0) continue; if (editit(tmpfil) == -1) { warn("error starting editor"); continue; } if (readprivs(curprivs, tmpfd)) putprivs(id, quotatype, curprivs); freeprivs(curprivs); } close(tmpfd); unlink(tmpfil); exit(0); }
int main(int argc, char *argv[]) { struct quotause *qup, *protoprivs, *curprivs; long id, protoid; int i, quotatype, range, tmpfd; uid_t startuid, enduid; uint64_t lim; char *protoname, *cp, *endpt, *oldoptarg; int eflag = 0, tflag = 0, pflag = 0, ch; char *fspath = NULL; char buf[MAXLOGNAME]; if (argc < 2) usage(); if (getuid()) errx(1, "permission denied"); quotatype = USRQUOTA; protoprivs = NULL; curprivs = NULL; protoname = NULL; while ((ch = getopt(argc, argv, "ughtf:p:e:")) != -1) { switch(ch) { case 'f': fspath = optarg; break; case 'p': if (eflag) { warnx("cannot specify both -e and -p"); usage(); /* not reached */ } protoname = optarg; pflag++; break; case 'g': quotatype = GRPQUOTA; break; case 'h': hflag++; break; case 'u': quotatype = USRQUOTA; break; case 't': tflag++; break; case 'e': if (pflag) { warnx("cannot specify both -e and -p"); usage(); /* not reached */ } if ((qup = calloc(1, sizeof(*qup))) == NULL) errx(2, "out of memory"); oldoptarg = optarg; for (i = 0, cp = optarg; (cp = strsep(&optarg, ":")) != NULL; i++) { if (cp != oldoptarg) *(cp - 1) = ':'; if (i > 0 && !isdigit(*cp)) { warnx("incorrect quota specification: " "%s", oldoptarg); usage(); /* Not Reached */ } switch (i) { case 0: strlcpy(qup->fsname, cp, sizeof(qup->fsname)); break; case 1: lim = strtoll(cp, &endpt, 10); qup->dqblk.dqb_bsoftlimit = cvtblkval(lim, *endpt, "block soft limit"); continue; case 2: lim = strtoll(cp, &endpt, 10); qup->dqblk.dqb_bhardlimit = cvtblkval(lim, *endpt, "block hard limit"); continue; case 3: lim = strtoll(cp, &endpt, 10); qup->dqblk.dqb_isoftlimit = cvtinoval(lim, *endpt, "inode soft limit"); continue; case 4: lim = strtoll(cp, &endpt, 10); qup->dqblk.dqb_ihardlimit = cvtinoval(lim, *endpt, "inode hard limit"); continue; default: warnx("incorrect quota specification: " "%s", oldoptarg); usage(); /* Not Reached */ } } if (protoprivs == NULL) { protoprivs = curprivs = qup; } else { curprivs->next = qup; curprivs = qup; } eflag++; break; default: usage(); /* Not Reached */ } } argc -= optind; argv += optind; if (pflag || eflag) { if (pflag) { if ((protoid = getentry(protoname, quotatype)) == -1) exit(1); protoprivs = getprivs(protoid, quotatype, fspath); if (protoprivs == NULL) exit(0); for (qup = protoprivs; qup; qup = qup->next) { qup->dqblk.dqb_btime = 0; qup->dqblk.dqb_itime = 0; } } for (; argc-- > 0; argv++) { if (strspn(*argv, "0123456789-") == strlen(*argv) && (cp = strchr(*argv, '-')) != NULL) { *cp++ = '\0'; startuid = atoi(*argv); enduid = atoi(cp); if (enduid < startuid) errx(1, "ending uid (%d) must be >= starting uid (%d) when using uid ranges", enduid, startuid); range = 1; } else { startuid = enduid = 0; range = 0; } for ( ; startuid <= enduid; startuid++) { if (range) snprintf(buf, sizeof(buf), "%d", startuid); else snprintf(buf, sizeof(buf), "%s", *argv); if ((id = getentry(buf, quotatype)) < 0) continue; if (pflag) { putprivs(id, protoprivs); continue; } for (qup = protoprivs; qup; qup = qup->next) { curprivs = getprivs(id, quotatype, qup->fsname); if (curprivs == NULL) continue; curprivs->dqblk = qup->dqblk; putprivs(id, curprivs); freeprivs(curprivs); } } } if (pflag) freeprivs(protoprivs); exit(0); } tmpfd = mkstemp(tmpfil); fchown(tmpfd, getuid(), getgid()); if (tflag) { if ((protoprivs = getprivs(0, quotatype, fspath)) != NULL) { if (writetimes(protoprivs, tmpfd, quotatype) != 0 && editit(tmpfil) && readtimes(protoprivs, tmpfil)) putprivs(0L, protoprivs); freeprivs(protoprivs); } close(tmpfd); unlink(tmpfil); exit(0); } for ( ; argc > 0; argc--, argv++) { if ((id = getentry(*argv, quotatype)) == -1) continue; if ((curprivs = getprivs(id, quotatype, fspath)) == NULL) exit(1); if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0) continue; if (editit(tmpfil) && readprivs(curprivs, tmpfil)) putprivs(id, curprivs); freeprivs(curprivs); } close(tmpfd); unlink(tmpfil); exit(0); }