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