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