Home | History | Annotate | Line # | Download | only in rpc.rquotad
rquotad.c revision 1.24.2.2
      1  1.24.2.2   bouyer /*	$NetBSD: rquotad.c,v 1.24.2.2 2011/02/14 20:35:24 bouyer Exp $	*/
      2       1.7  thorpej 
      3       1.1  deraadt /*
      4      1.19   bouyer  * by Manuel Bouyer (bouyer (at) ensta.fr). Public domain.
      5       1.1  deraadt  */
      6       1.1  deraadt 
      7       1.9      mrg #include <sys/cdefs.h>
      8       1.9      mrg #ifndef lint
      9  1.24.2.2   bouyer __RCSID("$NetBSD: rquotad.c,v 1.24.2.2 2011/02/14 20:35:24 bouyer Exp $");
     10       1.9      mrg #endif
     11       1.9      mrg 
     12       1.1  deraadt #include <sys/param.h>
     13       1.1  deraadt #include <sys/types.h>
     14       1.1  deraadt #include <sys/mount.h>
     15       1.1  deraadt #include <sys/file.h>
     16       1.1  deraadt #include <sys/stat.h>
     17       1.9      mrg #include <sys/socket.h>
     18       1.5      jtc #include <signal.h>
     19       1.1  deraadt 
     20       1.1  deraadt #include <stdio.h>
     21       1.1  deraadt #include <fstab.h>
     22       1.1  deraadt #include <ctype.h>
     23       1.1  deraadt #include <stdlib.h>
     24       1.1  deraadt #include <string.h>
     25       1.1  deraadt #include <pwd.h>
     26       1.1  deraadt #include <grp.h>
     27       1.1  deraadt #include <errno.h>
     28       1.9      mrg #include <unistd.h>
     29       1.1  deraadt 
     30       1.1  deraadt #include <syslog.h>
     31       1.1  deraadt 
     32  1.24.2.1   bouyer #include <ufs/ufs/quota2_prop.h>
     33  1.24.2.2   bouyer #include <ufs/ufs/quota1.h>
     34       1.1  deraadt #include <rpc/rpc.h>
     35       1.1  deraadt #include <rpcsvc/rquota.h>
     36       1.2      cgd #include <arpa/inet.h>
     37       1.1  deraadt 
     38  1.24.2.1   bouyer #include <getvfsquota.h>
     39  1.24.2.1   bouyer 
     40      1.15     fvdl void rquota_service(struct svc_req *request, SVCXPRT *transp);
     41      1.20   bouyer void ext_rquota_service(struct svc_req *request, SVCXPRT *transp);
     42      1.20   bouyer void sendquota(struct svc_req *request, int vers, SVCXPRT *transp);
     43      1.15     fvdl void cleanup(int);
     44      1.15     fvdl int main(int, char *[]);
     45       1.1  deraadt 
     46       1.4  mycroft int from_inetd = 1;
     47       1.4  mycroft 
     48       1.4  mycroft void
     49      1.15     fvdl cleanup(int dummy)
     50       1.4  mycroft {
     51      1.11      mrg 
     52      1.15     fvdl 	(void)rpcb_unset(RQUOTAPROG, RQUOTAVERS, NULL);
     53      1.20   bouyer 	(void)rpcb_unset(RQUOTAPROG, EXT_RQUOTAVERS, NULL);
     54       1.4  mycroft 	exit(0);
     55       1.4  mycroft }
     56       1.4  mycroft 
     57       1.1  deraadt int
     58      1.15     fvdl main(int argc, char *argv[])
     59       1.1  deraadt {
     60       1.4  mycroft 	SVCXPRT *transp;
     61      1.15     fvdl 	struct sockaddr_storage from;
     62      1.23      mrg 	socklen_t fromlen;
     63       1.1  deraadt 
     64       1.3  mycroft 	fromlen = sizeof(from);
     65      1.15     fvdl 	if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0)
     66       1.1  deraadt 		from_inetd = 0;
     67       1.3  mycroft 
     68       1.1  deraadt 	if (!from_inetd) {
     69       1.1  deraadt 		daemon(0, 0);
     70       1.3  mycroft 
     71      1.16     fvdl 		(void) rpcb_unset(RQUOTAPROG, RQUOTAVERS, NULL);
     72      1.20   bouyer 		(void) rpcb_unset(RQUOTAPROG, EXT_RQUOTAVERS, NULL);
     73       1.4  mycroft 		(void) signal(SIGINT, cleanup);
     74       1.4  mycroft 		(void) signal(SIGTERM, cleanup);
     75       1.4  mycroft 		(void) signal(SIGHUP, cleanup);
     76       1.1  deraadt 	}
     77       1.3  mycroft 
     78      1.13      mrg 	openlog("rpc.rquotad", LOG_PID, LOG_DAEMON);
     79       1.1  deraadt 
     80       1.1  deraadt 	/* create and register the service */
     81      1.15     fvdl 	if (from_inetd) {
     82      1.15     fvdl 		transp = svc_dg_create(0, 0, 0);
     83      1.15     fvdl 		if (transp == NULL) {
     84      1.15     fvdl 			syslog(LOG_ERR, "couldn't create udp service.");
     85      1.15     fvdl 			exit(1);
     86      1.15     fvdl 		}
     87      1.15     fvdl 		if (!svc_reg(transp, RQUOTAPROG, RQUOTAVERS, rquota_service,
     88      1.15     fvdl 		    NULL)) {
     89      1.15     fvdl 			syslog(LOG_ERR,
     90      1.15     fvdl 			    "unable to register (RQUOTAPROG, RQUOTAVERS).");
     91      1.15     fvdl 			exit(1);
     92      1.15     fvdl 		}
     93      1.20   bouyer 		if (!svc_reg(transp, RQUOTAPROG, EXT_RQUOTAVERS,
     94      1.20   bouyer 		    ext_rquota_service, NULL)) {
     95      1.20   bouyer 			syslog(LOG_ERR,
     96      1.20   bouyer 			    "unable to register (RQUOTAPROG, EXT_RQUOTAVERS).");
     97      1.20   bouyer 			exit(1);
     98      1.20   bouyer 		}
     99      1.15     fvdl 	} else {
    100      1.15     fvdl 		if (!svc_create(rquota_service, RQUOTAPROG, RQUOTAVERS, "udp")){
    101      1.15     fvdl 			syslog(LOG_ERR,
    102      1.15     fvdl 			    "unable to create (RQUOTAPROG, RQUOTAVERS).");
    103      1.15     fvdl 			exit(1);
    104      1.15     fvdl 		}
    105      1.20   bouyer 		if (!svc_create(ext_rquota_service, RQUOTAPROG,
    106      1.20   bouyer 		    EXT_RQUOTAVERS, "udp")){
    107      1.20   bouyer 			syslog(LOG_ERR,
    108      1.20   bouyer 			    "unable to create (RQUOTAPROG, EXT_RQUOTAVERS).");
    109      1.20   bouyer 			exit(1);
    110      1.20   bouyer 		}
    111       1.1  deraadt 	}
    112       1.4  mycroft 
    113       1.1  deraadt 	svc_run();
    114       1.4  mycroft 	syslog(LOG_ERR, "svc_run returned");
    115       1.4  mycroft 	exit(1);
    116       1.1  deraadt }
    117       1.1  deraadt 
    118       1.1  deraadt void
    119      1.15     fvdl rquota_service(struct svc_req *request, SVCXPRT *transp)
    120       1.1  deraadt {
    121       1.1  deraadt 	switch (request->rq_proc) {
    122       1.1  deraadt 	case NULLPROC:
    123       1.4  mycroft 		(void)svc_sendreply(transp, xdr_void, (char *)NULL);
    124       1.1  deraadt 		break;
    125       1.4  mycroft 
    126       1.1  deraadt 	case RQUOTAPROC_GETQUOTA:
    127       1.1  deraadt 	case RQUOTAPROC_GETACTIVEQUOTA:
    128      1.20   bouyer 		sendquota(request, RQUOTAVERS, transp);
    129      1.20   bouyer 		break;
    130      1.20   bouyer 
    131      1.20   bouyer 	default:
    132      1.20   bouyer 		svcerr_noproc(transp);
    133      1.20   bouyer 		break;
    134      1.20   bouyer 	}
    135      1.20   bouyer 	if (from_inetd)
    136      1.20   bouyer 		exit(0);
    137      1.20   bouyer }
    138      1.20   bouyer 
    139      1.20   bouyer void
    140      1.20   bouyer ext_rquota_service(struct svc_req *request, SVCXPRT *transp)
    141      1.20   bouyer {
    142      1.20   bouyer 	switch (request->rq_proc) {
    143      1.20   bouyer 	case NULLPROC:
    144      1.20   bouyer 		(void)svc_sendreply(transp, xdr_void, (char *)NULL);
    145      1.20   bouyer 		break;
    146      1.20   bouyer 
    147      1.20   bouyer 	case RQUOTAPROC_GETQUOTA:
    148      1.20   bouyer 	case RQUOTAPROC_GETACTIVEQUOTA:
    149      1.20   bouyer 		sendquota(request, EXT_RQUOTAVERS, transp);
    150       1.1  deraadt 		break;
    151       1.4  mycroft 
    152       1.1  deraadt 	default:
    153       1.4  mycroft 		svcerr_noproc(transp);
    154       1.1  deraadt 		break;
    155       1.1  deraadt 	}
    156       1.4  mycroft 	if (from_inetd)
    157       1.4  mycroft 		exit(0);
    158       1.1  deraadt }
    159       1.1  deraadt 
    160       1.1  deraadt /* read quota for the specified id, and send it */
    161       1.1  deraadt void
    162      1.20   bouyer sendquota(struct svc_req *request, int vers, SVCXPRT *transp)
    163       1.1  deraadt {
    164       1.1  deraadt 	struct getquota_args getq_args;
    165      1.20   bouyer 	struct ext_getquota_args ext_getq_args;
    166       1.1  deraadt 	struct getquota_rslt getq_rslt;
    167  1.24.2.1   bouyer 	struct quota2_entry q2e;
    168  1.24.2.2   bouyer 	struct dqblk dqblk;
    169  1.24.2.1   bouyer 	int type;
    170  1.24.2.1   bouyer 	int8_t version;
    171       1.1  deraadt 	struct timeval timev;
    172       1.1  deraadt 
    173      1.12    perry 	memset((char *)&getq_args, 0, sizeof(getq_args));
    174      1.21   bouyer 	memset((char *)&ext_getq_args, 0, sizeof(ext_getq_args));
    175      1.20   bouyer 	switch (vers) {
    176      1.20   bouyer 	case RQUOTAVERS:
    177      1.20   bouyer 		if (!svc_getargs(transp, xdr_getquota_args,
    178      1.20   bouyer 		    (caddr_t)&getq_args)) {
    179      1.20   bouyer 			svcerr_decode(transp);
    180      1.20   bouyer 			return;
    181      1.20   bouyer 		}
    182      1.20   bouyer 		ext_getq_args.gqa_pathp = getq_args.gqa_pathp;
    183      1.20   bouyer 		ext_getq_args.gqa_id = getq_args.gqa_uid;
    184      1.20   bouyer 		ext_getq_args.gqa_type = RQUOTA_USRQUOTA;
    185      1.20   bouyer 		break;
    186      1.20   bouyer 	case EXT_RQUOTAVERS:
    187      1.20   bouyer 		if (!svc_getargs(transp, xdr_ext_getquota_args,
    188      1.20   bouyer 		    (caddr_t)&ext_getq_args)) {
    189      1.20   bouyer 			svcerr_decode(transp);
    190      1.20   bouyer 			return;
    191      1.20   bouyer 		}
    192      1.20   bouyer 		break;
    193       1.1  deraadt 	}
    194  1.24.2.1   bouyer 	switch (ext_getq_args.gqa_type) {
    195  1.24.2.1   bouyer 	case RQUOTA_USRQUOTA:
    196  1.24.2.1   bouyer 		type = USRQUOTA;
    197  1.24.2.1   bouyer 		break;
    198  1.24.2.1   bouyer 	case RQUOTA_GRPQUOTA:
    199  1.24.2.1   bouyer 		type = GRPQUOTA;
    200  1.24.2.1   bouyer 		break;
    201  1.24.2.1   bouyer 	default:
    202  1.24.2.1   bouyer 		getq_rslt.status = Q_NOQUOTA;
    203  1.24.2.1   bouyer 		goto out;
    204  1.24.2.1   bouyer 	}
    205       1.1  deraadt 	if (request->rq_cred.oa_flavor != AUTH_UNIX) {
    206       1.1  deraadt 		/* bad auth */
    207       1.1  deraadt 		getq_rslt.status = Q_EPERM;
    208  1.24.2.1   bouyer 	} else if (!getvfsquota(ext_getq_args.gqa_pathp, &q2e, &version, ext_getq_args.gqa_id, ext_getq_args.gqa_type, 0, 0)) {
    209       1.4  mycroft 		/* failed, return noquota */
    210       1.4  mycroft 		getq_rslt.status = Q_NOQUOTA;
    211       1.1  deraadt 	} else {
    212  1.24.2.2   bouyer 		q2e2dqblk(&q2e, &dqblk);
    213       1.1  deraadt 		gettimeofday(&timev, NULL);
    214       1.1  deraadt 		getq_rslt.status = Q_OK;
    215       1.1  deraadt 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_active = TRUE;
    216       1.1  deraadt 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize = DEV_BSIZE;
    217       1.1  deraadt 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit =
    218  1.24.2.2   bouyer 		    dqblk.dqb_bhardlimit;
    219       1.1  deraadt 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit =
    220  1.24.2.2   bouyer 		    dqblk.dqb_bsoftlimit;
    221       1.1  deraadt 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks =
    222  1.24.2.2   bouyer 		    dqblk.dqb_curblocks;
    223       1.1  deraadt 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit =
    224  1.24.2.2   bouyer 		    dqblk.dqb_ihardlimit;
    225       1.1  deraadt 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit =
    226  1.24.2.2   bouyer 		    dqblk.dqb_isoftlimit;
    227       1.1  deraadt 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles =
    228  1.24.2.2   bouyer 		    dqblk.dqb_curinodes;
    229       1.1  deraadt 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft =
    230  1.24.2.2   bouyer 		    dqblk.dqb_btime;
    231       1.1  deraadt 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft =
    232  1.24.2.2   bouyer 		    dqblk.dqb_itime;
    233       1.1  deraadt 	}
    234  1.24.2.1   bouyer out:
    235      1.10    lukem 	if (!svc_sendreply(transp, xdr_getquota_rslt, (char *)&getq_rslt))
    236       1.4  mycroft 		svcerr_systemerr(transp);
    237       1.4  mycroft 	if (!svc_freeargs(transp, xdr_getquota_args, (caddr_t)&getq_args)) {
    238       1.4  mycroft 		syslog(LOG_ERR, "unable to free arguments");
    239       1.4  mycroft 		exit(1);
    240       1.4  mycroft 	}
    241       1.1  deraadt }
    242