Home | History | Annotate | Line # | Download | only in rpc.rquotad
rquotad.c revision 1.13.6.1
      1  1.13.6.1  wrstuden /*	$NetBSD: rquotad.c,v 1.13.6.1 1999/12/27 18:30:17 wrstuden Exp $	*/
      2       1.7   thorpej 
      3       1.1   deraadt /*
      4       1.1   deraadt  * by Manuel Bouyer (bouyer (at) ensta.fr)
      5       1.1   deraadt  *
      6       1.1   deraadt  * There is no copyright, you can use it as you want.
      7       1.1   deraadt  */
      8       1.1   deraadt 
      9       1.9       mrg #include <sys/cdefs.h>
     10       1.9       mrg #ifndef lint
     11  1.13.6.1  wrstuden __RCSID("$NetBSD: rquotad.c,v 1.13.6.1 1999/12/27 18:30:17 wrstuden Exp $");
     12       1.9       mrg #endif
     13       1.9       mrg 
     14       1.1   deraadt #include <sys/param.h>
     15       1.1   deraadt #include <sys/types.h>
     16       1.1   deraadt #include <sys/mount.h>
     17       1.1   deraadt #include <sys/file.h>
     18       1.1   deraadt #include <sys/stat.h>
     19       1.9       mrg #include <sys/socket.h>
     20       1.5       jtc #include <signal.h>
     21       1.1   deraadt 
     22       1.1   deraadt #include <stdio.h>
     23       1.1   deraadt #include <fstab.h>
     24       1.1   deraadt #include <ctype.h>
     25       1.1   deraadt #include <stdlib.h>
     26       1.1   deraadt #include <string.h>
     27       1.1   deraadt #include <pwd.h>
     28       1.1   deraadt #include <grp.h>
     29       1.1   deraadt #include <errno.h>
     30       1.9       mrg #include <unistd.h>
     31       1.1   deraadt 
     32       1.1   deraadt #include <syslog.h>
     33       1.1   deraadt 
     34       1.1   deraadt #include <ufs/ufs/quota.h>
     35       1.1   deraadt #include <rpc/rpc.h>
     36       1.1   deraadt #include <rpcsvc/rquota.h>
     37       1.2       cgd #include <arpa/inet.h>
     38       1.1   deraadt 
     39       1.4   mycroft void rquota_service __P((struct svc_req *request, SVCXPRT *transp));
     40       1.4   mycroft void sendquota __P((struct svc_req *request, SVCXPRT *transp));
     41       1.4   mycroft void printerr_reply __P((SVCXPRT *transp));
     42       1.1   deraadt void initfs __P((void));
     43       1.1   deraadt int getfsquota __P((long id, char *path, struct dqblk *dqblk));
     44       1.1   deraadt int hasquota __P((struct fstab *fs, char **qfnamep));
     45       1.9       mrg void cleanup __P((int));
     46       1.9       mrg int main __P((int, char *[]));
     47       1.1   deraadt 
     48       1.1   deraadt /*
     49       1.1   deraadt  * structure containing informations about ufs filesystems
     50       1.1   deraadt  * initialised by initfs()
     51       1.1   deraadt  */
     52       1.1   deraadt struct fs_stat {
     53       1.1   deraadt 	struct fs_stat *fs_next;	/* next element */
     54       1.1   deraadt 	char   *fs_file;		/* mount point of the filesystem */
     55       1.1   deraadt 	char   *qfpathname;		/* pathname of the quota file */
     56       1.1   deraadt 	dev_t   st_dev;			/* device of the filesystem */
     57       1.1   deraadt } fs_stat;
     58       1.1   deraadt struct fs_stat *fs_begin = NULL;
     59       1.1   deraadt 
     60       1.9       mrg char *qfextension[] = INITQFNAMES;
     61       1.4   mycroft int from_inetd = 1;
     62       1.4   mycroft 
     63       1.4   mycroft void
     64       1.9       mrg cleanup(dummy)
     65       1.9       mrg 	int dummy;
     66       1.4   mycroft {
     67      1.11       mrg 
     68      1.11       mrg 	(void)pmap_unset(RQUOTAPROG, RQUOTAVERS);
     69       1.4   mycroft 	exit(0);
     70       1.4   mycroft }
     71       1.4   mycroft 
     72       1.1   deraadt int
     73       1.1   deraadt main(argc, argv)
     74       1.1   deraadt 	int     argc;
     75       1.1   deraadt 	char   *argv[];
     76       1.1   deraadt {
     77       1.4   mycroft 	SVCXPRT *transp;
     78       1.4   mycroft 	int sock = 0;
     79       1.4   mycroft 	int proto = 0;
     80       1.1   deraadt 	struct sockaddr_in from;
     81       1.4   mycroft 	int fromlen;
     82       1.1   deraadt 
     83       1.3   mycroft 	fromlen = sizeof(from);
     84       1.3   mycroft 	if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
     85       1.1   deraadt 		from_inetd = 0;
     86       1.1   deraadt 		sock = RPC_ANYSOCK;
     87       1.1   deraadt 		proto = IPPROTO_UDP;
     88       1.1   deraadt 	}
     89       1.3   mycroft 
     90       1.1   deraadt 	if (!from_inetd) {
     91       1.1   deraadt 		daemon(0, 0);
     92       1.3   mycroft 
     93       1.4   mycroft 		(void) pmap_unset(RQUOTAPROG, RQUOTAVERS);
     94       1.3   mycroft 
     95       1.4   mycroft 		(void) signal(SIGINT, cleanup);
     96       1.4   mycroft 		(void) signal(SIGTERM, cleanup);
     97       1.4   mycroft 		(void) signal(SIGHUP, cleanup);
     98       1.1   deraadt 	}
     99       1.3   mycroft 
    100      1.13       mrg 	openlog("rpc.rquotad", LOG_PID, LOG_DAEMON);
    101       1.1   deraadt 
    102       1.1   deraadt 	/* create and register the service */
    103       1.4   mycroft 	transp = svcudp_create(sock);
    104       1.4   mycroft 	if (transp == NULL) {
    105       1.4   mycroft 		syslog(LOG_ERR, "couldn't create udp service.");
    106       1.1   deraadt 		exit(1);
    107       1.1   deraadt 	}
    108      1.10     lukem 	if (!svc_register(transp, RQUOTAPROG, RQUOTAVERS, rquota_service,
    109      1.10     lukem 	    proto)) {
    110      1.10     lukem 		syslog(LOG_ERR,
    111      1.10     lukem 		    "unable to register (RQUOTAPROG, RQUOTAVERS, %s).",
    112      1.10     lukem 		    proto ? "udp" : "(inetd)");
    113       1.1   deraadt 		exit(1);
    114       1.1   deraadt 	}
    115       1.4   mycroft 
    116       1.1   deraadt 	initfs();		/* init the fs_stat list */
    117       1.1   deraadt 	svc_run();
    118       1.4   mycroft 	syslog(LOG_ERR, "svc_run returned");
    119       1.4   mycroft 	exit(1);
    120       1.1   deraadt }
    121       1.1   deraadt 
    122       1.1   deraadt void
    123       1.4   mycroft rquota_service(request, transp)
    124       1.1   deraadt 	struct svc_req *request;
    125       1.4   mycroft 	SVCXPRT *transp;
    126       1.1   deraadt {
    127       1.1   deraadt 	switch (request->rq_proc) {
    128       1.1   deraadt 	case NULLPROC:
    129       1.4   mycroft 		(void)svc_sendreply(transp, xdr_void, (char *)NULL);
    130       1.1   deraadt 		break;
    131       1.4   mycroft 
    132       1.1   deraadt 	case RQUOTAPROC_GETQUOTA:
    133       1.1   deraadt 	case RQUOTAPROC_GETACTIVEQUOTA:
    134       1.4   mycroft 		sendquota(request, transp);
    135       1.1   deraadt 		break;
    136       1.4   mycroft 
    137       1.1   deraadt 	default:
    138       1.4   mycroft 		svcerr_noproc(transp);
    139       1.1   deraadt 		break;
    140       1.1   deraadt 	}
    141       1.4   mycroft 	if (from_inetd)
    142       1.4   mycroft 		exit(0);
    143       1.1   deraadt }
    144       1.1   deraadt 
    145       1.1   deraadt /* read quota for the specified id, and send it */
    146       1.1   deraadt void
    147       1.4   mycroft sendquota(request, transp)
    148       1.1   deraadt 	struct svc_req *request;
    149       1.4   mycroft 	SVCXPRT *transp;
    150       1.1   deraadt {
    151       1.1   deraadt 	struct getquota_args getq_args;
    152       1.1   deraadt 	struct getquota_rslt getq_rslt;
    153       1.1   deraadt 	struct dqblk dqblk;
    154       1.1   deraadt 	struct timeval timev;
    155       1.1   deraadt 
    156      1.12     perry 	memset((char *)&getq_args, 0, sizeof(getq_args));
    157       1.4   mycroft 	if (!svc_getargs(transp, xdr_getquota_args, (caddr_t)&getq_args)) {
    158       1.4   mycroft 		svcerr_decode(transp);
    159       1.1   deraadt 		return;
    160       1.1   deraadt 	}
    161       1.1   deraadt 	if (request->rq_cred.oa_flavor != AUTH_UNIX) {
    162       1.1   deraadt 		/* bad auth */
    163       1.1   deraadt 		getq_rslt.status = Q_EPERM;
    164      1.10     lukem 	} else if (!getfsquota(getq_args.gqa_uid, getq_args.gqa_pathp,
    165      1.10     lukem 	    &dqblk)) {
    166       1.4   mycroft 		/* failed, return noquota */
    167       1.4   mycroft 		getq_rslt.status = Q_NOQUOTA;
    168       1.1   deraadt 	} else {
    169       1.1   deraadt 		gettimeofday(&timev, NULL);
    170       1.1   deraadt 		getq_rslt.status = Q_OK;
    171       1.1   deraadt 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_active = TRUE;
    172       1.1   deraadt 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize = DEV_BSIZE;
    173       1.1   deraadt 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit =
    174       1.1   deraadt 		    dqblk.dqb_bhardlimit;
    175       1.1   deraadt 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit =
    176       1.1   deraadt 		    dqblk.dqb_bsoftlimit;
    177       1.1   deraadt 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks =
    178       1.1   deraadt 		    dqblk.dqb_curblocks;
    179       1.1   deraadt 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit =
    180       1.1   deraadt 		    dqblk.dqb_ihardlimit;
    181       1.1   deraadt 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit =
    182       1.1   deraadt 		    dqblk.dqb_isoftlimit;
    183       1.1   deraadt 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles =
    184       1.1   deraadt 		    dqblk.dqb_curinodes;
    185       1.1   deraadt 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft =
    186       1.1   deraadt 		    dqblk.dqb_btime - timev.tv_sec;
    187       1.1   deraadt 		getq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft =
    188       1.1   deraadt 		    dqblk.dqb_itime - timev.tv_sec;
    189       1.1   deraadt 	}
    190      1.10     lukem 	if (!svc_sendreply(transp, xdr_getquota_rslt, (char *)&getq_rslt))
    191       1.4   mycroft 		svcerr_systemerr(transp);
    192       1.4   mycroft 	if (!svc_freeargs(transp, xdr_getquota_args, (caddr_t)&getq_args)) {
    193       1.4   mycroft 		syslog(LOG_ERR, "unable to free arguments");
    194       1.4   mycroft 		exit(1);
    195       1.4   mycroft 	}
    196       1.1   deraadt }
    197       1.1   deraadt 
    198       1.1   deraadt void
    199       1.4   mycroft printerr_reply(transp)	/* when a reply to a request failed */
    200       1.4   mycroft 	SVCXPRT *transp;
    201       1.1   deraadt {
    202       1.1   deraadt 	char   *name;
    203       1.1   deraadt 	struct sockaddr_in *caller;
    204       1.1   deraadt 	int     save_errno;
    205       1.1   deraadt 
    206       1.1   deraadt 	save_errno = errno;
    207       1.1   deraadt 
    208       1.4   mycroft 	caller = svc_getcaller(transp);
    209       1.1   deraadt 	name = (char *)inet_ntoa(caller->sin_addr);
    210       1.1   deraadt 	errno = save_errno;
    211       1.1   deraadt 	if (errno == 0)
    212       1.1   deraadt 		syslog(LOG_ERR, "couldn't send reply to %s", name);
    213       1.1   deraadt 	else
    214       1.1   deraadt 		syslog(LOG_ERR, "couldn't send reply to %s: %m", name);
    215       1.1   deraadt }
    216       1.1   deraadt 
    217       1.1   deraadt /* initialise the fs_tab list from entries in /etc/fstab */
    218       1.1   deraadt void
    219       1.1   deraadt initfs()
    220       1.1   deraadt {
    221       1.1   deraadt 	struct fs_stat *fs_current = NULL;
    222       1.1   deraadt 	struct fs_stat *fs_next = NULL;
    223       1.1   deraadt 	char *qfpathname;
    224       1.1   deraadt 	struct fstab *fs;
    225       1.1   deraadt 	struct stat st;
    226       1.1   deraadt 
    227       1.1   deraadt 	setfsent();
    228       1.9       mrg 	while ((fs = getfsent())) {
    229      1.11       mrg 		if (strcmp(fs->fs_vfstype, MOUNT_FFS))
    230       1.1   deraadt 			continue;
    231       1.1   deraadt 		if (!hasquota(fs, &qfpathname))
    232       1.1   deraadt 			continue;
    233       1.1   deraadt 
    234       1.1   deraadt 		fs_current = (struct fs_stat *) malloc(sizeof(struct fs_stat));
    235      1.10     lukem 		if (fs_current == NULL) {
    236      1.10     lukem 			syslog(LOG_ERR, "can't malloc: %m");
    237      1.10     lukem 			exit(1);
    238      1.10     lukem 		}
    239       1.1   deraadt 		fs_current->fs_next = fs_next;	/* next element */
    240       1.1   deraadt 
    241      1.10     lukem 		fs_current->fs_file = strdup(fs->fs_file);
    242      1.10     lukem 		if (fs_current->fs_file == NULL) {
    243      1.10     lukem 			syslog(LOG_ERR, "can't strdup: %m");
    244      1.10     lukem 			exit(1);
    245      1.10     lukem 		}
    246       1.1   deraadt 
    247      1.10     lukem 		fs_current->qfpathname = strdup(qfpathname);
    248      1.10     lukem 		if (fs_current->qfpathname == NULL) {
    249      1.10     lukem 			syslog(LOG_ERR, "can't strdup: %m");
    250      1.10     lukem 			exit(1);
    251      1.10     lukem 		}
    252       1.1   deraadt 
    253      1.10     lukem 		stat(fs->fs_file, &st);
    254       1.1   deraadt 		fs_current->st_dev = st.st_dev;
    255       1.1   deraadt 
    256       1.1   deraadt 		fs_next = fs_current;
    257       1.1   deraadt 	}
    258       1.1   deraadt 	endfsent();
    259       1.1   deraadt 	fs_begin = fs_current;
    260       1.1   deraadt }
    261       1.1   deraadt 
    262       1.1   deraadt /*
    263       1.1   deraadt  * gets the quotas for id, filesystem path.
    264       1.1   deraadt  * Return 0 if fail, 1 otherwise
    265       1.1   deraadt  */
    266       1.1   deraadt int
    267       1.1   deraadt getfsquota(id, path, dqblk)
    268       1.1   deraadt 	long id;
    269       1.1   deraadt 	char   *path;
    270       1.1   deraadt 	struct dqblk *dqblk;
    271       1.1   deraadt {
    272       1.1   deraadt 	struct stat st_path;
    273       1.1   deraadt 	struct fs_stat *fs;
    274       1.1   deraadt 	int	qcmd, fd, ret = 0;
    275       1.1   deraadt 
    276       1.1   deraadt 	if (stat(path, &st_path) < 0)
    277       1.1   deraadt 		return (0);
    278       1.1   deraadt 
    279       1.1   deraadt 	qcmd = QCMD(Q_GETQUOTA, USRQUOTA);
    280       1.1   deraadt 
    281       1.1   deraadt 	for (fs = fs_begin; fs != NULL; fs = fs->fs_next) {
    282      1.10     lukem 		/* where the device is the same as path */
    283       1.1   deraadt 		if (fs->st_dev != st_path.st_dev)
    284       1.1   deraadt 			continue;
    285       1.1   deraadt 
    286       1.1   deraadt 		/* find the specified filesystem. get and return quota */
    287       1.1   deraadt 		if (quotactl(fs->fs_file, qcmd, id, dqblk) == 0)
    288       1.1   deraadt 			return (1);
    289       1.1   deraadt 
    290       1.1   deraadt 		if ((fd = open(fs->qfpathname, O_RDONLY)) < 0) {
    291       1.4   mycroft 			syslog(LOG_ERR, "open error: %s: %m", fs->qfpathname);
    292       1.1   deraadt 			return (0);
    293       1.1   deraadt 		}
    294       1.8    kleink 		if (lseek(fd, (off_t)(id * sizeof(struct dqblk)), SEEK_SET)
    295       1.8    kleink 		    == (off_t)-1) {
    296       1.1   deraadt 			close(fd);
    297       1.1   deraadt 			return (1);
    298       1.1   deraadt 		}
    299       1.1   deraadt 		switch (read(fd, dqblk, sizeof(struct dqblk))) {
    300       1.1   deraadt 		case 0:
    301       1.1   deraadt 			/*
    302       1.1   deraadt                          * Convert implicit 0 quota (EOF)
    303       1.1   deraadt                          * into an explicit one (zero'ed dqblk)
    304       1.1   deraadt                          */
    305      1.12     perry 			memset((caddr_t) dqblk, 0, sizeof(struct dqblk));
    306       1.1   deraadt 			ret = 1;
    307       1.1   deraadt 			break;
    308       1.1   deraadt 		case sizeof(struct dqblk):	/* OK */
    309       1.1   deraadt 			ret = 1;
    310       1.1   deraadt 			break;
    311       1.1   deraadt 		default:	/* ERROR */
    312       1.1   deraadt 			syslog(LOG_ERR, "read error: %s: %m", fs->qfpathname);
    313       1.1   deraadt 			close(fd);
    314       1.1   deraadt 			return (0);
    315       1.1   deraadt 		}
    316       1.1   deraadt 		close(fd);
    317       1.1   deraadt 	}
    318       1.1   deraadt 	return (ret);
    319       1.1   deraadt }
    320       1.1   deraadt 
    321       1.1   deraadt /*
    322       1.1   deraadt  * Check to see if a particular quota is to be enabled.
    323       1.1   deraadt  * Comes from quota.c, NetBSD 0.9
    324       1.1   deraadt  */
    325       1.1   deraadt int
    326       1.1   deraadt hasquota(fs, qfnamep)
    327       1.1   deraadt 	struct fstab *fs;
    328       1.1   deraadt 	char  **qfnamep;
    329       1.1   deraadt {
    330       1.1   deraadt 	static char initname, usrname[100];
    331       1.9       mrg 	static char buf[MAXPATHLEN];
    332       1.9       mrg 	char	*opt, *cp = NULL;
    333       1.1   deraadt 
    334       1.1   deraadt 	if (!initname) {
    335       1.9       mrg 		(void)snprintf(usrname, sizeof usrname, "%s%s",
    336       1.9       mrg 		    qfextension[USRQUOTA], QUOTAFILENAME);
    337       1.1   deraadt 		initname = 1;
    338       1.1   deraadt 	}
    339       1.9       mrg 	strncpy(buf, fs->fs_mntops, sizeof(buf) - 1);
    340       1.9       mrg 	buf[sizeof(buf) - 1] = '\0';
    341       1.1   deraadt 	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
    342      1.10     lukem 		if ((cp = strchr(opt, '=')))
    343       1.1   deraadt 			*cp++ = '\0';
    344       1.1   deraadt 		if (strcmp(opt, usrname) == 0)
    345       1.1   deraadt 			break;
    346       1.1   deraadt 	}
    347       1.1   deraadt 	if (!opt)
    348       1.1   deraadt 		return (0);
    349       1.1   deraadt 	if (cp) {
    350       1.1   deraadt 		*qfnamep = cp;
    351       1.1   deraadt 		return (1);
    352       1.1   deraadt 	}
    353       1.9       mrg 	(void)snprintf(buf, sizeof buf, "%s/%s.%s", fs->fs_file, QUOTAFILENAME,
    354       1.9       mrg 	    qfextension[USRQUOTA]);
    355       1.1   deraadt 	*qfnamep = buf;
    356       1.1   deraadt 	return (1);
    357       1.1   deraadt }
    358