/* $NetBSD: edquota.c,v 1.54 2023/08/01 08:47:25 mrg Exp $ */
/*
* Copyright (c) 1980, 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Robert Elz at The University of Melbourne.
*
* 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. 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.
*/
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
The Regents of the University of California. All rights reserved.");
#endif /* not lint */
enum sources {
SRC_EDITED, /* values came from user */
SRC_QUOTA, /* values came from a specific quota entry */
SRC_DEFAULT, /* values were copied from the default quota entry */
SRC_CLEAR, /* values arose by zeroing out a quota entry */
};
struct quotause {
struct quotause *next;
unsigned found:1, /* found after running editor */
xgrace:1, /* grace periods are per-id */
isdefault:1;
/* more compact form of constants */
#define QO_BLK QUOTA_OBJTYPE_BLOCKS
#define QO_FL QUOTA_OBJTYPE_FILES
////////////////////////////////////////////////////////////
// support code
/*
* This routine converts a name for a particular quota class to
* an identifier. This routine must agree with the kernel routine
* getinoquota as to the interpretation of quota classes.
*/
static int
getidbyname(const char *name, int idtype)
{
struct passwd *pw;
struct group *gr;
if (alldigits(name))
return atoi(name);
switch (idtype) {
case QUOTA_IDTYPE_USER:
if ((pw = getpwnam(name)) != NULL)
return pw->pw_uid;
warnx("%s: no such user", name);
break;
case QUOTA_IDTYPE_GROUP:
if ((gr = getgrnam(name)) != NULL)
return gr->gr_gid;
warnx("%s: no such group", name);
break;
default:
warnx("%d: unknown quota type", idtype);
break;
}
sleep(1);
return -1;
}
/*
* check if a source is "real" (reflects actual data) or not
*/
static bool
source_is_real(enum sources source)
{
switch (source) {
case SRC_EDITED:
case SRC_QUOTA:
return true;
case SRC_DEFAULT:
case SRC_CLEAR:
return false;
}
assert(!"encountered invalid source");
return false;
}
////////////////////////////////////////////////////////////
// quota format switch
/*
* Collect the requested quota information.
*/
static struct quotalist *
getprivs(long id, int defaultq, int idtype, const char *filesys)
{
struct statvfs *fst;
int nfst, i;
struct quotalist *qlist;
struct quotause *qup;
int seenany = 0;
qlist = quotalist_create();
nfst = getmntinfo(&fst, MNT_WAIT);
if (nfst == 0)
errx(1, "no filesystems mounted!");
for (i = 0; i < nfst; i++) {
if ((fst[i].f_flag & ST_QUOTA) == 0)
continue;
seenany = 1;
if (filesys &&
strcmp(fst[i].f_mntonname, filesys) != 0 &&
strcmp(fst[i].f_mntfromname, filesys) != 0)
continue;
qup = getprivs2(id, idtype, fst[i].f_mntonname, defaultq,
&qlist->idtypename);
if (qup == NULL) {
/* XXX the atf tests demand failing silently */
/*warn("Reading quotas failed for id %ld", id);*/
continue;
}
quotalist_append(qlist, qup);
}
if (!seenany) {
errx(1, "No mounted filesystems have quota support");
}
#if 0
if (filesys && quotalist_empty(qlist)) {
if (defaultq)
errx(1, "no default quota for version 1");
/* if we get there, filesys is not mounted. try the old way */
qup = getprivs1(id, idtype, filesys);
if (qup == NULL) {
warnx("getprivs1 failed");
return qlist;
}
quotalist_append(qlist, qup);
}
#endif
return qlist;
}
/*
* Store the requested quota information.
*/
static void
putprivs(uint32_t id, int idtype, struct quotalist *qlist)
{
struct quotause *qup;
/*
* Take a list of privileges and get it edited.
*/
static int
editit(const char *ltmpfile)
{
pid_t pid;
int lst;
char p[MAX_TMPSTR];
const char *ed;
sigset_t s, os;
sigemptyset(&s);
sigaddset(&s, SIGINT);
sigaddset(&s, SIGQUIT);
sigaddset(&s, SIGHUP);
if (sigprocmask(SIG_BLOCK, &s, &os) == -1)
err(1, "sigprocmask");
top:
switch ((pid = fork())) {
case -1:
if (errno == EPROCLIM) {
warnx("You have too many processes");
return 0;
}
if (errno == EAGAIN) {
sleep(1);
goto top;
}
warn("fork");
return 0;
case 0:
if (sigprocmask(SIG_SETMASK, &os, NULL) == -1)
err(1, "sigprocmask");
setgid(getgid());
setuid(getuid());
if ((ed = getenv("EDITOR")) == (char *)0)
ed = _PATH_VI;
if ((size_t)snprintf(p, sizeof(p), "%s %s", ed, ltmpfile) >=
sizeof(p)) {
errx(1, "%s", "editor or filename too long");
}
execlp(_PATH_BSHELL, _PATH_BSHELL, "-c", p, NULL);
err(1, "%s", ed);
default:
if (waitpid(pid, &lst, 0) == -1)
err(1, "waitpid");
if (sigprocmask(SIG_SETMASK, &os, NULL) == -1)
err(1, "sigprocmask");
if (!WIFEXITED(lst) || WEXITSTATUS(lst) != 0)
return 0;
return 1;
}
}
/*
* Convert a quotause list to an ASCII file.
*/
static int
writeprivs(struct quotalist *qlist, int outfd, const char *name,
int idtype, const char *idtypename)
{
struct quotause *qup;
FILE *fd;
char b0[32], b1[32], b2[32], b3[32];
const char *comm;
while (fgets(line, sizeof(line), fd)) {
lineno++;
if (!seenheader) {
if ((!strncmp(line, "Default ", 8) && dflag) ||
(!strncmp(line, "Quotas for ", 11) && !dflag)) {
/* ok. */
seenheader = 1;
continue;
}
warnx("Header line missing");
goto fail;
}
len = strlen(line);
if (len == 0) {
/* ? */
continue;
}
if (line[len - 1] != '\n') {
warnx("Line %u too long", lineno);
goto fail;
}
line[--len] = '\0';
if (line[len - 1] == '\r') {
line[--len] = '\0';
}
if (len == 0) {
/* blank line */
continue;
}
/*
* If the line has: it is:
* two tabs values
* one tab the next objtype
* no tabs the next filesystem
*/
if (line[0] == '\t' && line[1] == '\t') {
if (qup == NULL) {
warnx("Line %u: values with no filesystem",
lineno);
goto fail;
}
if (!haveobjtype) {
warnx("Line %u: values with no object type",
lineno);
goto fail;
}
qv = &qup->qv[objtype];
text = line + 2;
if (*text == '#') {
/* commented out; ignore */
continue;
}
else if (!strncmp(text, "usage:", 6)) {
/* usage: %llu */
text += 6;
t = skipws(text);
if (intrd(t, ¤t, objtypeflags) != 0) {
warnx("Line %u: Bad number %s",
lineno, t);
goto fail;
}
/*
* Because the humanization can lead
* to roundoff, check if the two
* values produce the same humanized
* string, rather than if they're the
* same number. Sigh.
*/
intprt(b0, 21, current,
HN_NOSPACE | objtypeflags, Hflag);
intprt(b1, 21, qv->qv_usage,
HN_NOSPACE | objtypeflags, Hflag);
if (strcmp(b0, b1)) {
warnx("Line %u: cannot change usage",
lineno);
}
continue;
t = skipws(text);
t = skipword(t);
t = skipws(t);
if (intrd(t, &soft, objtypeflags) != 0) {
warnx("Line %u: Bad number %s",
lineno, t);
goto fail;
}
t = skipws(text2);
t = skipword(t);
t = skipws(t);
if (intrd(t, &hard, objtypeflags) != 0) {
warnx("Line %u: Bad number %s",
lineno, t);
goto fail;
}
/*
* Cause time limit to be reset when the quota
* is next used if previously had no soft limit
* or were under it, but now have a soft limit
* and are over it.
*/
if (qv->qv_usage && qv->qv_usage >= soft &&
(qv->qv_softlimit == 0 ||
qv->qv_usage < qv->qv_softlimit)) {
qv->qv_expiretime = 0;
}
if (soft != qv->qv_softlimit ||
hard != qv->qv_hardlimit) {
qv->qv_softlimit = soft;
qv->qv_hardlimit = hard;
qup->source[objtype] = SRC_EDITED;
}
qup->found = 1;
} else if (!strncmp(text, "grace:", 6)) {
text += 6;
/* grace: %llu */
t = skipws(text);
if (timeprd(t, &grace) != 0) {
warnx("Line %u: Bad number %s",
lineno, t);
goto fail;
}
if (qup->isdefault || qup->xgrace) {
if (grace != qv->qv_grace) {
qv->qv_grace = grace;
qup->source[objtype] =
SRC_EDITED;
}
qup->found = 1;
} else {
warnx("Line %u: Cannot set individual "
"grace time on this filesystem",
lineno);
goto fail;
}
} else {
warnx("Line %u: Unknown/unexpected value line",
lineno);
goto fail;
}
} else if (line[0] == '\t') {
text = line + 1;
if (*text == '#') {
/* commented out; ignore */
continue;
}
else if (!strncmp(text, "blocks:", 7)) {
objtype = QUOTA_OBJTYPE_BLOCKS;
objtypeflags = HN_B;
haveobjtype = true;
} else if (!strncmp(text, "inodes:", 7)) {
objtype = QUOTA_OBJTYPE_FILES;
objtypeflags = 0;
haveobjtype = true;
} else {
warnx("Line %u: Unknown/unexpected object "
"type (%s)", lineno, text);
goto fail;
}
} else {
t = strchr(line, ' ');
if (t == NULL) {
t = strchr(line, ':');
if (t == NULL) {
t = line + len;
}
}
*t = '\0';
for (qup = qlist->head; qup; qup = qup->next) {
if (!strcmp(line, qup->fsname))
break;
}
if (qup == NULL) {
warnx("Line %u: Filesystem %s invalid or has "
"no quota support", lineno, line);
goto fail;
}
haveobjtype = false;
}
}
fclose(fd);
/*
* Disable quotas for any filesystems that we didn't see,
* because they must have been deleted in the editor.
*
* XXX this should be improved so it results in
* quota_delete(), not just writing out a blank quotaval.
*/
for (qup = qlist->head; qup; qup = qup->next) {
if (qup->found) {
qup->found = 0;
continue;
}
if (source_is_real(qup->source[QUOTA_OBJTYPE_BLOCKS])) {
quotaval_clear(&qup->qv[QUOTA_OBJTYPE_BLOCKS]);
qup->source[QUOTA_OBJTYPE_BLOCKS] = SRC_EDITED;
}
int
main(int argc, char *argv[])
{
int idtype;
char *protoname;
char *soft = NULL, *hard = NULL, *grace = NULL;
char *fs = NULL;
int ch;
int pflag = 0;
int cflag = 0;
int dflag = 0;
if (argc < 2)
usage();
if (getuid())
errx(1, "permission denied");
protoname = NULL;
idtype = QUOTA_IDTYPE_USER;
while ((ch = getopt(argc, argv, "Hcdugp:s:h:t:f:")) != -1) {
switch(ch) {
case 'H':
Hflag++;
break;
case 'c':
cflag++;
break;
case 'd':
dflag++;
break;
case 'p':
protoname = optarg;
pflag++;
break;
case 'g':
idtype = QUOTA_IDTYPE_GROUP;
break;
case 'u':
idtype = QUOTA_IDTYPE_USER;
break;
case 's':
soft = optarg;
break;
case 'h':
hard = optarg;
break;
case 't':
grace = optarg;
break;
case 'f':
fs = optarg;
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (pflag) {
if (soft || hard || grace || dflag || cflag)
usage();
replicate(fs, idtype, protoname, argv, argc);
} else if (soft || hard || grace) {
if (cflag)
usage();
if (dflag) {
/* use argv[argc], which is null, to mean 'default' */
argc++;
}
assign(fs, idtype, soft, hard, grace, argv, argc);
} else if (cflag) {
if (dflag)
usage();
clear(fs, idtype, argv, argc);
} else {
if (dflag) {
/* use argv[argc], which is null, to mean 'default' */
argc++;
}
edit(fs, idtype, argv, argc);
}
return 0;
}