Correct comments and minor variable naming and sysctl issues.
[dragonfly.git] / libexec / rpc.rquotad / rquotad.c
blobfcfe081cfaf69dd021040af089d5392e13cd2642
1 /*
2 * by Manuel Bouyer (bouyer@ensta.fr)
3 *
4 * There is no copyright, you can use it as you want.
6 * $FreeBSD: src/libexec/rpc.rquotad/rquotad.c,v 1.3.2.1 2001/07/02 23:46:27 mikeh Exp $
7 * $DragonFly: src/libexec/rpc.rquotad/rquotad.c,v 1.5 2006/04/03 01:59:27 dillon Exp $
8 */
10 #include <sys/param.h>
11 #include <sys/types.h>
12 #include <sys/mount.h>
13 #include <sys/file.h>
14 #include <sys/stat.h>
15 #include <sys/socket.h>
16 #include <signal.h>
18 #include <ctype.h>
19 #include <errno.h>
20 #include <fstab.h>
21 #include <grp.h>
22 #include <pwd.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
28 #include <syslog.h>
29 #include <varargs.h>
31 #include <vfs/ufs/quota.h>
32 #include <rpc/rpc.h>
33 #include <rpc/pmap_clnt.h>
34 #include <rpcsvc/rquota.h>
35 #include <arpa/inet.h>
37 void rquota_service (struct svc_req *request, SVCXPRT *transp);
38 void sendquota (struct svc_req *request, SVCXPRT *transp);
39 void printerr_reply (SVCXPRT *transp);
40 void initfs (void);
41 int getfsquota (long id, char *path, struct ufs_dqblk *dqblk);
42 int hasquota (struct fstab *fs, char **qfnamep);
45 * structure containing informations about ufs filesystems
46 * initialised by initfs()
48 struct fs_stat {
49 struct fs_stat *fs_next; /* next element */
50 char *fs_file; /* mount point of the filesystem */
51 char *qfpathname; /* pathname of the quota file */
52 dev_t st_dev; /* device of the filesystem */
53 } fs_stat;
54 struct fs_stat *fs_begin = NULL;
56 int from_inetd = 1;
58 void
59 cleanup()
61 (void) pmap_unset(RQUOTAPROG, RQUOTAVERS);
62 exit(0);
65 int
66 main(argc, argv)
67 int argc;
68 char *argv[];
70 SVCXPRT *transp;
71 int sock = 0;
72 int proto = 0;
73 struct sockaddr_in from;
74 int fromlen;
76 fromlen = sizeof(from);
77 if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
78 from_inetd = 0;
79 sock = RPC_ANYSOCK;
80 proto = IPPROTO_UDP;
83 if (!from_inetd) {
84 daemon(0, 0);
86 (void) pmap_unset(RQUOTAPROG, RQUOTAVERS);
88 (void) signal(SIGINT, cleanup);
89 (void) signal(SIGTERM, cleanup);
90 (void) signal(SIGHUP, cleanup);
93 openlog("rpc.rquotad", LOG_CONS|LOG_PID, LOG_DAEMON);
95 /* create and register the service */
96 transp = svcudp_create(sock);
97 if (transp == NULL) {
98 syslog(LOG_ERR, "couldn't create udp service");
99 exit(1);
101 if (!svc_register(transp, RQUOTAPROG, RQUOTAVERS, rquota_service, proto)) {
102 syslog(LOG_ERR, "unable to register (RQUOTAPROG, RQUOTAVERS, %s)", proto?"udp":"(inetd)");
103 exit(1);
106 initfs(); /* init the fs_stat list */
107 svc_run();
108 syslog(LOG_ERR, "svc_run returned");
109 exit(1);
112 void
113 rquota_service(request, transp)
114 struct svc_req *request;
115 SVCXPRT *transp;
117 switch (request->rq_proc) {
118 case NULLPROC:
119 (void)svc_sendreply(transp, xdr_void, (char *)NULL);
120 break;
122 case RQUOTAPROC_GETQUOTA:
123 case RQUOTAPROC_GETACTIVEQUOTA:
124 sendquota(request, transp);
125 break;
127 default:
128 svcerr_noproc(transp);
129 break;
131 if (from_inetd)
132 exit(0);
135 /* read quota for the specified id, and send it */
136 void
137 sendquota(request, transp)
138 struct svc_req *request;
139 SVCXPRT *transp;
141 struct getquota_args getq_args;
142 struct getquota_rslt getq_rslt;
143 struct ufs_dqblk dqblk;
144 struct timeval timev;
146 bzero((char *)&getq_args, sizeof(getq_args));
147 if (!svc_getargs(transp, xdr_getquota_args, (caddr_t)&getq_args)) {
148 svcerr_decode(transp);
149 return;
151 if (request->rq_cred.oa_flavor != AUTH_UNIX) {
152 /* bad auth */
153 getq_rslt.status = Q_EPERM;
154 } else if (!getfsquota(getq_args.gqa_uid, getq_args.gqa_pathp, &dqblk)) {
155 /* failed, return noquota */
156 getq_rslt.status = Q_NOQUOTA;
157 } else {
158 gettimeofday(&timev, NULL);
159 getq_rslt.status = Q_OK;
160 getq_rslt.getquota_rslt_u.gqr_rquota.rq_active = TRUE;
161 getq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize = DEV_BSIZE;
162 getq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit =
163 dqblk.dqb_bhardlimit;
164 getq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit =
165 dqblk.dqb_bsoftlimit;
166 getq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks =
167 dqblk.dqb_curblocks;
168 getq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit =
169 dqblk.dqb_ihardlimit;
170 getq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit =
171 dqblk.dqb_isoftlimit;
172 getq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles =
173 dqblk.dqb_curinodes;
174 getq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft =
175 dqblk.dqb_btime - timev.tv_sec;
176 getq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft =
177 dqblk.dqb_itime - timev.tv_sec;
179 if (!svc_sendreply(transp, xdr_getquota_rslt, (char *)&getq_rslt)) {
180 svcerr_systemerr(transp);
182 if (!svc_freeargs(transp, xdr_getquota_args, (caddr_t)&getq_args)) {
183 syslog(LOG_ERR, "unable to free arguments");
184 exit(1);
188 void
189 printerr_reply(transp) /* when a reply to a request failed */
190 SVCXPRT *transp;
192 char *name;
193 struct sockaddr_in *caller;
194 int save_errno;
196 save_errno = errno;
198 caller = svc_getcaller(transp);
199 name = (char *)inet_ntoa(caller->sin_addr);
200 errno = save_errno;
201 if (errno == 0)
202 syslog(LOG_ERR, "couldn't send reply to %s", name);
203 else
204 syslog(LOG_ERR, "couldn't send reply to %s: %m", name);
207 /* initialise the fs_tab list from entries in /etc/fstab */
208 void
209 initfs()
211 struct fs_stat *fs_current = NULL;
212 struct fs_stat *fs_next = NULL;
213 char *qfpathname;
214 struct fstab *fs;
215 struct stat st;
217 setfsent();
218 while ((fs = getfsent())) {
219 if (strcmp(fs->fs_vfstype, "ufs"))
220 continue;
221 if (!hasquota(fs, &qfpathname))
222 continue;
224 fs_current = (struct fs_stat *) malloc(sizeof(struct fs_stat));
225 fs_current->fs_next = fs_next; /* next element */
227 fs_current->fs_file = malloc(sizeof(char) * (strlen(fs->fs_file) + 1));
228 strcpy(fs_current->fs_file, fs->fs_file);
230 fs_current->qfpathname = malloc(sizeof(char) * (strlen(qfpathname) + 1));
231 strcpy(fs_current->qfpathname, qfpathname);
233 stat(fs_current->fs_file, &st);
234 fs_current->st_dev = st.st_dev;
236 fs_next = fs_current;
238 endfsent();
239 fs_begin = fs_current;
243 * gets the quotas for id, filesystem path.
244 * Return 0 if fail, 1 otherwise
247 getfsquota(id, path, dqblk)
248 long id;
249 char *path;
250 struct ufs_dqblk *dqblk;
252 struct stat st_path;
253 struct fs_stat *fs;
254 int qcmd, fd, ret = 0;
256 if (stat(path, &st_path) < 0)
257 return (0);
259 qcmd = QCMD(Q_GETQUOTA, USRQUOTA);
261 for (fs = fs_begin; fs != NULL; fs = fs->fs_next) {
262 /* where the devise is the same as path */
263 if (fs->st_dev != st_path.st_dev)
264 continue;
266 /* find the specified filesystem. get and return quota */
267 if (quotactl(fs->fs_file, qcmd, id, dqblk) == 0)
268 return (1);
270 if ((fd = open(fs->qfpathname, O_RDONLY)) < 0) {
271 syslog(LOG_ERR, "open error: %s: %m", fs->qfpathname);
272 return (0);
274 if (lseek(fd, (off_t)(id * sizeof(struct ufs_dqblk)), L_SET) == (off_t)-1) {
275 close(fd);
276 return (1);
278 switch (read(fd, dqblk, sizeof(struct ufs_dqblk))) {
279 case 0:
281 * Convert implicit 0 quota (EOF)
282 * into an explicit one (zero'ed dqblk)
284 bzero((caddr_t) dqblk, sizeof(struct ufs_dqblk));
285 ret = 1;
286 break;
287 case sizeof(struct ufs_dqblk): /* OK */
288 ret = 1;
289 break;
290 default: /* ERROR */
291 syslog(LOG_ERR, "read error: %s: %m", fs->qfpathname);
292 close(fd);
293 return (0);
295 close(fd);
297 return (ret);
301 * Check to see if a particular quota is to be enabled.
302 * Comes from quota.c, NetBSD 0.9
305 hasquota(fs, qfnamep)
306 struct fstab *fs;
307 char **qfnamep;
309 static char initname, usrname[100];
310 static char buf[BUFSIZ];
311 char *opt, *cp;
312 char *qfextension[] = INITQFNAMES;
314 if (!initname) {
315 sprintf(usrname, "%s%s", qfextension[USRQUOTA], QUOTAFILENAME);
316 initname = 1;
318 strcpy(buf, fs->fs_mntops);
319 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
320 if ((cp = index(opt, '=')))
321 *cp++ = '\0';
322 if (strcmp(opt, usrname) == 0)
323 break;
325 if (!opt)
326 return (0);
327 if (cp) {
328 *qfnamep = cp;
329 return (1);
331 sprintf(buf, "%s/%s.%s", fs->fs_file, QUOTAFILENAME, qfextension[USRQUOTA]);
332 *qfnamep = buf;
333 return (1);