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