2014-03-18 15:00:20 +04:00
|
|
|
/* $NetBSD: rquotad.c,v 1.33 2014/03/18 11:00:20 gson Exp $ */
|
1996-08-31 00:15:06 +04:00
|
|
|
|
1994-06-24 11:42:26 +04:00
|
|
|
/*
|
2002-06-09 21:10:44 +04:00
|
|
|
* by Manuel Bouyer (bouyer@ensta.fr). Public domain.
|
1994-06-24 11:42:26 +04:00
|
|
|
*/
|
|
|
|
|
1997-10-07 15:15:16 +04:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
2014-03-18 15:00:20 +04:00
|
|
|
__RCSID("$NetBSD: rquotad.c,v 1.33 2014/03/18 11:00:20 gson 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 <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
|
|
|
|
2012-01-09 19:37:34 +04:00
|
|
|
#include <quota.h>
|
|
|
|
|
2011-08-27 19:46:59 +04:00
|
|
|
static void rquota_service(struct svc_req *request, SVCXPRT *transp);
|
|
|
|
static void ext_rquota_service(struct svc_req *request, SVCXPRT *transp);
|
|
|
|
static void sendquota(struct svc_req *request, int vers, SVCXPRT *transp);
|
|
|
|
__dead static void cleanup(int);
|
1994-06-24 11:42:26 +04:00
|
|
|
|
2011-08-27 19:46:59 +04:00
|
|
|
static int from_inetd = 1;
|
1995-01-13 22:16:45 +03:00
|
|
|
|
2011-08-27 19:46:59 +04:00
|
|
|
static 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);
|
2003-02-14 17:55:58 +03:00
|
|
|
(void)rpcb_unset(RQUOTAPROG, EXT_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;
|
2006-05-10 00:18:05 +04:00
|
|
|
socklen_t 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) {
|
2000-06-04 00:57:40 +04:00
|
|
|
(void) rpcb_unset(RQUOTAPROG, RQUOTAVERS, NULL);
|
2003-02-14 17:55:58 +03:00
|
|
|
(void) rpcb_unset(RQUOTAPROG, EXT_RQUOTAVERS, NULL);
|
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);
|
|
|
|
}
|
2003-02-14 17:55:58 +03:00
|
|
|
if (!svc_reg(transp, RQUOTAPROG, EXT_RQUOTAVERS,
|
|
|
|
ext_rquota_service, NULL)) {
|
|
|
|
syslog(LOG_ERR,
|
|
|
|
"unable to register (RQUOTAPROG, EXT_RQUOTAVERS).");
|
|
|
|
exit(1);
|
|
|
|
}
|
2000-06-04 00:52:18 +04:00
|
|
|
} else {
|
|
|
|
if (!svc_create(rquota_service, RQUOTAPROG, RQUOTAVERS, "udp")){
|
|
|
|
syslog(LOG_ERR,
|
|
|
|
"unable to create (RQUOTAPROG, RQUOTAVERS).");
|
|
|
|
exit(1);
|
|
|
|
}
|
2003-02-14 17:55:58 +03:00
|
|
|
if (!svc_create(ext_rquota_service, RQUOTAPROG,
|
|
|
|
EXT_RQUOTAVERS, "udp")){
|
|
|
|
syslog(LOG_ERR,
|
|
|
|
"unable to create (RQUOTAPROG, EXT_RQUOTAVERS).");
|
|
|
|
exit(1);
|
|
|
|
}
|
1994-06-24 11:42:26 +04:00
|
|
|
}
|
1995-01-13 22:16:45 +03:00
|
|
|
|
2014-03-18 15:00:20 +04:00
|
|
|
if (!from_inetd) {
|
|
|
|
daemon(0, 0);
|
|
|
|
(void) signal(SIGINT, cleanup);
|
|
|
|
(void) signal(SIGTERM, cleanup);
|
|
|
|
(void) signal(SIGHUP, cleanup);
|
|
|
|
}
|
1994-06-24 11:42:26 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-08-27 19:46:59 +04:00
|
|
|
static 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:
|
2011-09-16 20:13:16 +04:00
|
|
|
(void)svc_sendreply(transp, (xdrproc_t)xdr_void, 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:
|
2003-02-14 17:55:58 +03:00
|
|
|
sendquota(request, RQUOTAVERS, transp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
svcerr_noproc(transp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (from_inetd)
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2011-08-27 19:46:59 +04:00
|
|
|
static void
|
2003-02-14 17:55:58 +03:00
|
|
|
ext_rquota_service(struct svc_req *request, SVCXPRT *transp)
|
|
|
|
{
|
|
|
|
switch (request->rq_proc) {
|
|
|
|
case NULLPROC:
|
2011-09-16 20:13:16 +04:00
|
|
|
(void)svc_sendreply(transp, (xdrproc_t)xdr_void, NULL);
|
2003-02-14 17:55:58 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RQUOTAPROC_GETQUOTA:
|
|
|
|
case RQUOTAPROC_GETACTIVEQUOTA:
|
|
|
|
sendquota(request, EXT_RQUOTAVERS, 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
|
|
|
}
|
|
|
|
|
2012-01-09 19:37:34 +04:00
|
|
|
/*
|
|
|
|
* Convert a limit to rquota representation (where 0 == unlimited).
|
|
|
|
* Clamp the result into a uint32_t.
|
|
|
|
*/
|
|
|
|
static uint32_t
|
|
|
|
limit_to_rquota(uint64_t lim)
|
|
|
|
{
|
|
|
|
if (lim == QUOTA_NOLIMIT || lim > 0xfffffffeUL)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return (lim + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert a time to rquota representation.
|
|
|
|
*/
|
|
|
|
static uint32_t
|
|
|
|
time_to_rquota(time_t when, time_t now)
|
|
|
|
{
|
|
|
|
if (when == QUOTA_NOTIME) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return when - now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert to rquota representation.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
quotavals_to_rquota(const struct quotaval *blocks,
|
|
|
|
const struct quotaval *files,
|
|
|
|
struct rquota *rq)
|
|
|
|
{
|
|
|
|
struct timeval now;
|
|
|
|
|
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
|
|
|
|
rq->rq_active = TRUE;
|
|
|
|
rq->rq_bsize = DEV_BSIZE;
|
|
|
|
|
|
|
|
rq->rq_bhardlimit = limit_to_rquota(blocks->qv_hardlimit);
|
|
|
|
rq->rq_bsoftlimit = limit_to_rquota(blocks->qv_softlimit);
|
|
|
|
rq->rq_curblocks = blocks->qv_usage;
|
|
|
|
rq->rq_btimeleft = time_to_rquota(blocks->qv_expiretime, now.tv_sec);
|
|
|
|
|
|
|
|
rq->rq_fhardlimit = limit_to_rquota(files->qv_hardlimit);
|
|
|
|
rq->rq_fsoftlimit = limit_to_rquota(files->qv_softlimit);
|
|
|
|
rq->rq_curfiles = files->qv_usage;
|
|
|
|
rq->rq_ftimeleft = time_to_rquota(files->qv_expiretime, now.tv_sec);
|
|
|
|
}
|
|
|
|
|
1994-06-24 11:42:26 +04:00
|
|
|
/* read quota for the specified id, and send it */
|
2011-08-27 19:46:59 +04:00
|
|
|
static void
|
2003-02-14 17:55:58 +03:00
|
|
|
sendquota(struct svc_req *request, int vers, SVCXPRT *transp)
|
1994-06-24 11:42:26 +04:00
|
|
|
{
|
|
|
|
struct getquota_args getq_args;
|
2003-02-14 17:55:58 +03:00
|
|
|
struct ext_getquota_args ext_getq_args;
|
1994-06-24 11:42:26 +04:00
|
|
|
struct getquota_rslt getq_rslt;
|
2012-01-09 19:37:34 +04:00
|
|
|
struct quotahandle *qh;
|
|
|
|
struct quotakey qk;
|
|
|
|
struct quotaval blocks, files;
|
|
|
|
int idtype;
|
1994-06-24 11:42:26 +04:00
|
|
|
|
1998-08-10 06:57:23 +04:00
|
|
|
memset((char *)&getq_args, 0, sizeof(getq_args));
|
2003-09-21 02:25:29 +04:00
|
|
|
memset((char *)&ext_getq_args, 0, sizeof(ext_getq_args));
|
2003-02-14 17:55:58 +03:00
|
|
|
switch (vers) {
|
|
|
|
case RQUOTAVERS:
|
|
|
|
if (!svc_getargs(transp, xdr_getquota_args,
|
|
|
|
(caddr_t)&getq_args)) {
|
|
|
|
svcerr_decode(transp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ext_getq_args.gqa_pathp = getq_args.gqa_pathp;
|
|
|
|
ext_getq_args.gqa_id = getq_args.gqa_uid;
|
|
|
|
ext_getq_args.gqa_type = RQUOTA_USRQUOTA;
|
|
|
|
break;
|
|
|
|
case EXT_RQUOTAVERS:
|
|
|
|
if (!svc_getargs(transp, xdr_ext_getquota_args,
|
|
|
|
(caddr_t)&ext_getq_args)) {
|
|
|
|
svcerr_decode(transp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
1994-06-24 11:42:26 +04:00
|
|
|
}
|
2011-03-06 20:08:10 +03:00
|
|
|
switch (ext_getq_args.gqa_type) {
|
|
|
|
case RQUOTA_USRQUOTA:
|
2012-01-09 19:37:34 +04:00
|
|
|
idtype = QUOTA_IDTYPE_USER;
|
2011-03-06 20:08:10 +03:00
|
|
|
break;
|
|
|
|
case RQUOTA_GRPQUOTA:
|
2012-01-09 19:37:34 +04:00
|
|
|
idtype = QUOTA_IDTYPE_GROUP;
|
2011-03-06 20:08:10 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
getq_rslt.status = Q_NOQUOTA;
|
|
|
|
goto out;
|
|
|
|
}
|
1994-06-24 11:42:26 +04:00
|
|
|
if (request->rq_cred.oa_flavor != AUTH_UNIX) {
|
|
|
|
/* bad auth */
|
|
|
|
getq_rslt.status = Q_EPERM;
|
2012-01-09 19:37:34 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX validate the path...
|
|
|
|
*/
|
|
|
|
|
|
|
|
qh = quota_open(ext_getq_args.gqa_pathp);
|
|
|
|
if (qh == NULL) {
|
|
|
|
/*
|
|
|
|
* There are only three possible responses: success,
|
|
|
|
* permission denied, and "no quota", so we return
|
|
|
|
* the last for essentially all errors.
|
|
|
|
*/
|
|
|
|
if (errno == EPERM || errno == EACCES) {
|
|
|
|
getq_rslt.status = Q_EPERM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
getq_rslt.status = Q_NOQUOTA;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
qk.qk_id = ext_getq_args.gqa_id;
|
|
|
|
qk.qk_idtype = idtype;
|
|
|
|
qk.qk_objtype = QUOTA_OBJTYPE_BLOCKS;
|
|
|
|
if (quota_get(qh, &qk, &blocks) < 0) {
|
1995-01-13 22:16:45 +03:00
|
|
|
/* failed, return noquota */
|
2012-01-09 19:37:34 +04:00
|
|
|
quota_close(qh);
|
1995-01-13 22:16:45 +03:00
|
|
|
getq_rslt.status = Q_NOQUOTA;
|
2012-01-09 19:37:34 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
qk.qk_objtype = QUOTA_OBJTYPE_FILES;
|
|
|
|
if (quota_get(qh, &qk, &files) < 0) {
|
|
|
|
/* failed, return noquota */
|
|
|
|
quota_close(qh);
|
|
|
|
getq_rslt.status = Q_NOQUOTA;
|
|
|
|
goto out;
|
1994-06-24 11:42:26 +04:00
|
|
|
}
|
2012-01-09 19:37:34 +04:00
|
|
|
|
|
|
|
quota_close(qh);
|
|
|
|
|
|
|
|
quotavals_to_rquota(&blocks, &files,
|
|
|
|
&getq_rslt.getquota_rslt_u.gqr_rquota);
|
|
|
|
getq_rslt.status = Q_OK;
|
|
|
|
|
2011-03-06 20:08:10 +03:00
|
|
|
out:
|
2011-08-30 21:06:20 +04:00
|
|
|
if (!svc_sendreply(transp, (xdrproc_t)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
|
|
|
}
|