2001-01-10 04:50:05 +03:00
|
|
|
/* $NetBSD: rquotad.c,v 1.17 2001/01/10 01:50:05 lukem Exp $ */
|
1996-08-31 00:15:06 +04:00
|
|
|
|
1994-06-24 11:42:26 +04:00
|
|
|
/*
|
|
|
|
* by Manuel Bouyer (bouyer@ensta.fr)
|
|
|
|
*
|
|
|
|
* There is no copyright, you can use it as you want.
|
|
|
|
*/
|
|
|
|
|
1997-10-07 15:15:16 +04:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
2001-01-10 04:50:05 +03:00
|
|
|
__RCSID("$NetBSD: rquotad.c,v 1.17 2001/01/10 01:50:05 lukem Exp $");
|
1997-10-07 15:15:16 +04:00
|
|
|
#endif
|
|
|
|
|
1994-06-24 11:42:26 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/stat.h>
|
1997-10-07 15:15:16 +04:00
|
|
|
#include <sys/socket.h>
|
1995-04-12 04:41:40 +04:00
|
|
|
#include <signal.h>
|
1994-06-24 11:42:26 +04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fstab.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <errno.h>
|
1997-10-07 15:15:16 +04:00
|
|
|
#include <unistd.h>
|
1994-06-24 11:42:26 +04:00
|
|
|
|
|
|
|
#include <syslog.h>
|
|
|
|
|
|
|
|
#include <ufs/ufs/quota.h>
|
|
|
|
#include <rpc/rpc.h>
|
|
|
|
#include <rpcsvc/rquota.h>
|
1994-12-23 17:29:31 +03:00
|
|
|
#include <arpa/inet.h>
|
1994-06-24 11:42:26 +04:00
|
|
|
|
2000-06-04 00:52:18 +04:00
|
|
|
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);
|
|
|
|
void cleanup(int);
|
|
|
|
int main(int, char *[]);
|
1994-06-24 11:42:26 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
1997-10-07 15:15:16 +04:00
|
|
|
char *qfextension[] = INITQFNAMES;
|
1995-01-13 22:16:45 +03:00
|
|
|
int from_inetd = 1;
|
|
|
|
|
|
|
|
void
|
2000-06-04 00:52:18 +04:00
|
|
|
cleanup(int dummy)
|
1995-01-13 22:16:45 +03:00
|
|
|
{
|
1998-07-03 15:48:58 +04:00
|
|
|
|
2000-06-04 00:52:18 +04:00
|
|
|
(void)rpcb_unset(RQUOTAPROG, RQUOTAVERS, NULL);
|
1995-01-13 22:16:45 +03:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
1994-06-24 11:42:26 +04:00
|
|
|
int
|
2000-06-04 00:52:18 +04:00
|
|
|
main(int argc, char *argv[])
|
1994-06-24 11:42:26 +04:00
|
|
|
{
|
1995-01-13 22:16:45 +03:00
|
|
|
SVCXPRT *transp;
|
2000-06-04 00:52:18 +04:00
|
|
|
struct sockaddr_storage from;
|
1995-01-13 22:16:45 +03:00
|
|
|
int fromlen;
|
1994-06-24 11:42:26 +04:00
|
|
|
|
1995-01-13 09:14:28 +03:00
|
|
|
fromlen = sizeof(from);
|
2000-06-04 00:52:18 +04:00
|
|
|
if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0)
|
1994-06-24 11:42:26 +04:00
|
|
|
from_inetd = 0;
|
1995-01-13 09:14:28 +03:00
|
|
|
|
1994-06-24 11:42:26 +04:00
|
|
|
if (!from_inetd) {
|
|
|
|
daemon(0, 0);
|
1995-01-13 09:14:28 +03:00
|
|
|
|
2000-06-04 00:57:40 +04:00
|
|
|
(void) rpcb_unset(RQUOTAPROG, RQUOTAVERS, NULL);
|
1995-01-13 09:14:28 +03:00
|
|
|
|
1995-01-13 22:16:45 +03:00
|
|
|
(void) signal(SIGINT, cleanup);
|
|
|
|
(void) signal(SIGTERM, cleanup);
|
|
|
|
(void) signal(SIGHUP, cleanup);
|
1994-06-24 11:42:26 +04:00
|
|
|
}
|
|
|
|
|
1999-01-31 11:51:53 +03:00
|
|
|
openlog("rpc.rquotad", LOG_PID, LOG_DAEMON);
|
1995-01-13 09:14:28 +03:00
|
|
|
|
1994-06-24 11:42:26 +04:00
|
|
|
/* create and register the service */
|
2000-06-04 00:52:18 +04:00
|
|
|
if (from_inetd) {
|
|
|
|
transp = svc_dg_create(0, 0, 0);
|
|
|
|
if (transp == NULL) {
|
|
|
|
syslog(LOG_ERR, "couldn't create udp service.");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (!svc_reg(transp, RQUOTAPROG, RQUOTAVERS, rquota_service,
|
|
|
|
NULL)) {
|
|
|
|
syslog(LOG_ERR,
|
|
|
|
"unable to register (RQUOTAPROG, RQUOTAVERS).");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!svc_create(rquota_service, RQUOTAPROG, RQUOTAVERS, "udp")){
|
|
|
|
syslog(LOG_ERR,
|
|
|
|
"unable to create (RQUOTAPROG, RQUOTAVERS).");
|
|
|
|
exit(1);
|
|
|
|
}
|
1994-06-24 11:42:26 +04:00
|
|
|
}
|
1995-01-13 22:16:45 +03:00
|
|
|
|
1994-06-24 11:42:26 +04:00
|
|
|
initfs(); /* init the fs_stat list */
|
|
|
|
svc_run();
|
1995-01-13 22:16:45 +03:00
|
|
|
syslog(LOG_ERR, "svc_run returned");
|
|
|
|
exit(1);
|
1994-06-24 11:42:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-06-04 00:52:18 +04:00
|
|
|
rquota_service(struct svc_req *request, SVCXPRT *transp)
|
1994-06-24 11:42:26 +04:00
|
|
|
{
|
|
|
|
switch (request->rq_proc) {
|
|
|
|
case NULLPROC:
|
1995-01-13 22:16:45 +03:00
|
|
|
(void)svc_sendreply(transp, xdr_void, (char *)NULL);
|
1994-06-24 11:42:26 +04:00
|
|
|
break;
|
1995-01-13 22:16:45 +03:00
|
|
|
|
1994-06-24 11:42:26 +04:00
|
|
|
case RQUOTAPROC_GETQUOTA:
|
|
|
|
case RQUOTAPROC_GETACTIVEQUOTA:
|
1995-01-13 22:16:45 +03:00
|
|
|
sendquota(request, transp);
|
1994-06-24 11:42:26 +04:00
|
|
|
break;
|
1995-01-13 22:16:45 +03:00
|
|
|
|
1994-06-24 11:42:26 +04:00
|
|
|
default:
|
1995-01-13 22:16:45 +03:00
|
|
|
svcerr_noproc(transp);
|
1994-06-24 11:42:26 +04:00
|
|
|
break;
|
|
|
|
}
|
1995-01-13 22:16:45 +03:00
|
|
|
if (from_inetd)
|
|
|
|
exit(0);
|
1994-06-24 11:42:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* read quota for the specified id, and send it */
|
|
|
|
void
|
2000-06-04 00:52:18 +04:00
|
|
|
sendquota(struct svc_req *request, SVCXPRT *transp)
|
1994-06-24 11:42:26 +04:00
|
|
|
{
|
|
|
|
struct getquota_args getq_args;
|
|
|
|
struct getquota_rslt getq_rslt;
|
|
|
|
struct dqblk dqblk;
|
|
|
|
struct timeval timev;
|
|
|
|
|
1998-08-10 06:57:23 +04:00
|
|
|
memset((char *)&getq_args, 0, sizeof(getq_args));
|
1995-01-13 22:16:45 +03:00
|
|
|
if (!svc_getargs(transp, xdr_getquota_args, (caddr_t)&getq_args)) {
|
|
|
|
svcerr_decode(transp);
|
1994-06-24 11:42:26 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (request->rq_cred.oa_flavor != AUTH_UNIX) {
|
|
|
|
/* bad auth */
|
|
|
|
getq_rslt.status = Q_EPERM;
|
1998-01-21 14:37:16 +03:00
|
|
|
} else if (!getfsquota(getq_args.gqa_uid, getq_args.gqa_pathp,
|
|
|
|
&dqblk)) {
|
1995-01-13 22:16:45 +03:00
|
|
|
/* failed, return noquota */
|
|
|
|
getq_rslt.status = Q_NOQUOTA;
|
1994-06-24 11:42:26 +04:00
|
|
|
} 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;
|
|
|
|
}
|
1998-01-21 14:37:16 +03:00
|
|
|
if (!svc_sendreply(transp, xdr_getquota_rslt, (char *)&getq_rslt))
|
1995-01-13 22:16:45 +03:00
|
|
|
svcerr_systemerr(transp);
|
|
|
|
if (!svc_freeargs(transp, xdr_getquota_args, (caddr_t)&getq_args)) {
|
|
|
|
syslog(LOG_ERR, "unable to free arguments");
|
|
|
|
exit(1);
|
|
|
|
}
|
1994-06-24 11:42:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-06-04 00:52:18 +04:00
|
|
|
printerr_reply(SVCXPRT *transp) /* when a reply to a request failed */
|
1994-06-24 11:42:26 +04:00
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
struct sockaddr_in *caller;
|
|
|
|
int save_errno;
|
|
|
|
|
|
|
|
save_errno = errno;
|
|
|
|
|
1995-01-13 22:16:45 +03:00
|
|
|
caller = svc_getcaller(transp);
|
1994-06-24 11:42:26 +04:00
|
|
|
name = (char *)inet_ntoa(caller->sin_addr);
|
|
|
|
errno = save_errno;
|
|
|
|
if (errno == 0)
|
2001-01-10 04:50:05 +03:00
|
|
|
syslog(LOG_WARNING, "couldn't send reply to %s", name);
|
1994-06-24 11:42:26 +04:00
|
|
|
else
|
2001-01-10 04:50:05 +03:00
|
|
|
syslog(LOG_WARNING, "couldn't send reply to %s: %m", name);
|
1994-06-24 11:42:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* initialise the fs_tab list from entries in /etc/fstab */
|
|
|
|
void
|
|
|
|
initfs()
|
|
|
|
{
|
|
|
|
struct fs_stat *fs_current = NULL;
|
|
|
|
struct fs_stat *fs_next = NULL;
|
|
|
|
char *qfpathname;
|
|
|
|
struct fstab *fs;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
setfsent();
|
1997-10-07 15:15:16 +04:00
|
|
|
while ((fs = getfsent())) {
|
1998-07-03 15:48:58 +04:00
|
|
|
if (strcmp(fs->fs_vfstype, MOUNT_FFS))
|
1994-06-24 11:42:26 +04:00
|
|
|
continue;
|
|
|
|
if (!hasquota(fs, &qfpathname))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
fs_current = (struct fs_stat *) malloc(sizeof(struct fs_stat));
|
1998-01-21 14:37:16 +03:00
|
|
|
if (fs_current == NULL) {
|
|
|
|
syslog(LOG_ERR, "can't malloc: %m");
|
|
|
|
exit(1);
|
|
|
|
}
|
1994-06-24 11:42:26 +04:00
|
|
|
fs_current->fs_next = fs_next; /* next element */
|
|
|
|
|
1998-01-21 14:37:16 +03:00
|
|
|
fs_current->fs_file = strdup(fs->fs_file);
|
|
|
|
if (fs_current->fs_file == NULL) {
|
|
|
|
syslog(LOG_ERR, "can't strdup: %m");
|
|
|
|
exit(1);
|
|
|
|
}
|
1994-06-24 11:42:26 +04:00
|
|
|
|
1998-01-21 14:37:16 +03:00
|
|
|
fs_current->qfpathname = strdup(qfpathname);
|
|
|
|
if (fs_current->qfpathname == NULL) {
|
|
|
|
syslog(LOG_ERR, "can't strdup: %m");
|
|
|
|
exit(1);
|
|
|
|
}
|
1994-06-24 11:42:26 +04:00
|
|
|
|
1998-01-21 14:37:16 +03:00
|
|
|
stat(fs->fs_file, &st);
|
1994-06-24 11:42:26 +04:00
|
|
|
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
|
2000-06-04 00:52:18 +04:00
|
|
|
getfsquota(long id, char *path, struct dqblk *dqblk)
|
1994-06-24 11:42:26 +04:00
|
|
|
{
|
|
|
|
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) {
|
1998-01-21 14:37:16 +03:00
|
|
|
/* where the device is the same as path */
|
1994-06-24 11:42:26 +04:00
|
|
|
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) {
|
2001-01-10 04:50:05 +03:00
|
|
|
syslog(LOG_WARNING, "open error: %s: %m",
|
|
|
|
fs->qfpathname);
|
1994-06-24 11:42:26 +04:00
|
|
|
return (0);
|
|
|
|
}
|
1997-08-25 23:31:43 +04:00
|
|
|
if (lseek(fd, (off_t)(id * sizeof(struct dqblk)), SEEK_SET)
|
|
|
|
== (off_t)-1) {
|
1994-06-24 11:42:26 +04:00
|
|
|
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)
|
|
|
|
*/
|
1998-08-10 06:57:23 +04:00
|
|
|
memset((caddr_t) dqblk, 0, sizeof(struct dqblk));
|
1994-06-24 11:42:26 +04:00
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
case sizeof(struct dqblk): /* OK */
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
default: /* ERROR */
|
2001-01-10 04:50:05 +03:00
|
|
|
syslog(LOG_WARNING, "read error: %s: %m",
|
|
|
|
fs->qfpathname);
|
1994-06-24 11:42:26 +04:00
|
|
|
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
|
2000-06-04 00:52:18 +04:00
|
|
|
hasquota(struct fstab *fs, char **qfnamep)
|
1994-06-24 11:42:26 +04:00
|
|
|
{
|
|
|
|
static char initname, usrname[100];
|
1997-10-07 15:15:16 +04:00
|
|
|
static char buf[MAXPATHLEN];
|
|
|
|
char *opt, *cp = NULL;
|
1994-06-24 11:42:26 +04:00
|
|
|
|
|
|
|
if (!initname) {
|
1997-10-07 15:15:16 +04:00
|
|
|
(void)snprintf(usrname, sizeof usrname, "%s%s",
|
|
|
|
qfextension[USRQUOTA], QUOTAFILENAME);
|
1994-06-24 11:42:26 +04:00
|
|
|
initname = 1;
|
|
|
|
}
|
1997-10-07 15:15:16 +04:00
|
|
|
strncpy(buf, fs->fs_mntops, sizeof(buf) - 1);
|
|
|
|
buf[sizeof(buf) - 1] = '\0';
|
1994-06-24 11:42:26 +04:00
|
|
|
for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
|
1998-01-21 14:37:16 +03:00
|
|
|
if ((cp = strchr(opt, '=')))
|
1994-06-24 11:42:26 +04:00
|
|
|
*cp++ = '\0';
|
|
|
|
if (strcmp(opt, usrname) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!opt)
|
|
|
|
return (0);
|
|
|
|
if (cp) {
|
|
|
|
*qfnamep = cp;
|
|
|
|
return (1);
|
|
|
|
}
|
1997-10-07 15:15:16 +04:00
|
|
|
(void)snprintf(buf, sizeof buf, "%s/%s.%s", fs->fs_file, QUOTAFILENAME,
|
|
|
|
qfextension[USRQUOTA]);
|
1994-06-24 11:42:26 +04:00
|
|
|
*qfnamep = buf;
|
|
|
|
return (1);
|
|
|
|
}
|