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