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