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