2 * by Manuel Bouyer (bouyer@ensta.fr)
4 * There is no copyright, you can use it as you want.
8 static const char rcsid[] =
9 "$FreeBSD: src/libexec/rpc.rquotad/rquotad.c,v 1.3.2.1 2001/07/02 23:46:27 mikeh Exp $";
12 #include <sys/param.h>
13 #include <sys/types.h>
14 #include <sys/mount.h>
17 #include <sys/socket.h>
33 #include <ufs/ufs/quota.h>
35 #include <rpc/pmap_clnt.h>
36 #include <rpcsvc/rquota.h>
37 #include <arpa/inet.h>
39 void rquota_service __P((struct svc_req *request, SVCXPRT *transp));
40 void sendquota __P((struct svc_req *request, SVCXPRT *transp));
41 void printerr_reply __P((SVCXPRT *transp));
42 void initfs __P((void));
43 int getfsquota __P((long id, char *path, struct dqblk *dqblk));
44 int hasquota __P((struct fstab *fs, char **qfnamep));
47 * structure containing informations about ufs filesystems
48 * initialised by initfs()
51 struct fs_stat *fs_next; /* next element */
52 char *fs_file; /* mount point of the filesystem */
53 char *qfpathname; /* pathname of the quota file */
54 dev_t st_dev; /* device of the filesystem */
56 struct fs_stat *fs_begin = NULL;
63 (void) pmap_unset(RQUOTAPROG, RQUOTAVERS);
75 struct sockaddr_in from;
78 fromlen = sizeof(from);
79 if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
88 (void) pmap_unset(RQUOTAPROG, RQUOTAVERS);
90 (void) signal(SIGINT, cleanup);
91 (void) signal(SIGTERM, cleanup);
92 (void) signal(SIGHUP, cleanup);
95 openlog("rpc.rquotad", LOG_CONS|LOG_PID, LOG_DAEMON);
97 /* create and register the service */
98 transp = svcudp_create(sock);
100 syslog(LOG_ERR, "couldn't create udp service");
103 if (!svc_register(transp, RQUOTAPROG, RQUOTAVERS, rquota_service, proto)) {
104 syslog(LOG_ERR, "unable to register (RQUOTAPROG, RQUOTAVERS, %s)", proto?"udp":"(inetd)");
108 initfs(); /* init the fs_stat list */
110 syslog(LOG_ERR, "svc_run returned");
115 rquota_service(request, transp)
116 struct svc_req *request;
119 switch (request->rq_proc) {
121 (void)svc_sendreply(transp, xdr_void, (char *)NULL);
124 case RQUOTAPROC_GETQUOTA:
125 case RQUOTAPROC_GETACTIVEQUOTA:
126 sendquota(request, transp);
130 svcerr_noproc(transp);
137 /* read quota for the specified id, and send it */
139 sendquota(request, transp)
140 struct svc_req *request;
143 struct getquota_args getq_args;
144 struct getquota_rslt getq_rslt;
146 struct timeval timev;
148 bzero((char *)&getq_args, sizeof(getq_args));
149 if (!svc_getargs(transp, xdr_getquota_args, (caddr_t)&getq_args)) {
150 svcerr_decode(transp);
153 if (request->rq_cred.oa_flavor != AUTH_UNIX) {
155 getq_rslt.status = Q_EPERM;
156 } else if (!getfsquota(getq_args.gqa_uid, getq_args.gqa_pathp, &dqblk)) {
157 /* failed, return noquota */
158 getq_rslt.status = Q_NOQUOTA;
160 gettimeofday(&timev, NULL);
161 getq_rslt.status = Q_OK;
162 getq_rslt.getquota_rslt_u.gqr_rquota.rq_active = TRUE;
163 getq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize = DEV_BSIZE;
164 getq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit =
165 dqblk.dqb_bhardlimit;
166 getq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit =
167 dqblk.dqb_bsoftlimit;
168 getq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks =
170 getq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit =
171 dqblk.dqb_ihardlimit;
172 getq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit =
173 dqblk.dqb_isoftlimit;
174 getq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles =
176 getq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft =
177 dqblk.dqb_btime - timev.tv_sec;
178 getq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft =
179 dqblk.dqb_itime - timev.tv_sec;
181 if (!svc_sendreply(transp, xdr_getquota_rslt, (char *)&getq_rslt)) {
182 svcerr_systemerr(transp);
184 if (!svc_freeargs(transp, xdr_getquota_args, (caddr_t)&getq_args)) {
185 syslog(LOG_ERR, "unable to free arguments");
191 printerr_reply(transp) /* when a reply to a request failed */
195 struct sockaddr_in *caller;
200 caller = svc_getcaller(transp);
201 name = (char *)inet_ntoa(caller->sin_addr);
204 syslog(LOG_ERR, "couldn't send reply to %s", name);
206 syslog(LOG_ERR, "couldn't send reply to %s: %m", name);
209 /* initialise the fs_tab list from entries in /etc/fstab */
213 struct fs_stat *fs_current = NULL;
214 struct fs_stat *fs_next = NULL;
220 while ((fs = getfsent())) {
221 if (strcmp(fs->fs_vfstype, "ufs"))
223 if (!hasquota(fs, &qfpathname))
226 fs_current = (struct fs_stat *) malloc(sizeof(struct fs_stat));
227 fs_current->fs_next = fs_next; /* next element */
229 fs_current->fs_file = malloc(sizeof(char) * (strlen(fs->fs_file) + 1));
230 strcpy(fs_current->fs_file, fs->fs_file);
232 fs_current->qfpathname = malloc(sizeof(char) * (strlen(qfpathname) + 1));
233 strcpy(fs_current->qfpathname, qfpathname);
235 stat(fs_current->fs_file, &st);
236 fs_current->st_dev = st.st_dev;
238 fs_next = fs_current;
241 fs_begin = fs_current;
245 * gets the quotas for id, filesystem path.
246 * Return 0 if fail, 1 otherwise
249 getfsquota(id, path, dqblk)
256 int qcmd, fd, ret = 0;
258 if (stat(path, &st_path) < 0)
261 qcmd = QCMD(Q_GETQUOTA, USRQUOTA);
263 for (fs = fs_begin; fs != NULL; fs = fs->fs_next) {
264 /* where the devise is the same as path */
265 if (fs->st_dev != st_path.st_dev)
268 /* find the specified filesystem. get and return quota */
269 if (quotactl(fs->fs_file, qcmd, id, dqblk) == 0)
272 if ((fd = open(fs->qfpathname, O_RDONLY)) < 0) {
273 syslog(LOG_ERR, "open error: %s: %m", fs->qfpathname);
276 if (lseek(fd, (off_t)(id * sizeof(struct dqblk)), L_SET) == (off_t)-1) {
280 switch (read(fd, dqblk, sizeof(struct dqblk))) {
283 * Convert implicit 0 quota (EOF)
284 * into an explicit one (zero'ed dqblk)
286 bzero((caddr_t) dqblk, sizeof(struct dqblk));
289 case sizeof(struct dqblk): /* OK */
293 syslog(LOG_ERR, "read error: %s: %m", fs->qfpathname);
303 * Check to see if a particular quota is to be enabled.
304 * Comes from quota.c, NetBSD 0.9
307 hasquota(fs, qfnamep)
311 static char initname, usrname[100];
312 static char buf[BUFSIZ];
314 char *qfextension[] = INITQFNAMES;
317 sprintf(usrname, "%s%s", qfextension[USRQUOTA], QUOTAFILENAME);
320 strcpy(buf, fs->fs_mntops);
321 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
322 if ((cp = index(opt, '=')))
324 if (strcmp(opt, usrname) == 0)
333 sprintf(buf, "%s/%s.%s", fs->fs_file, QUOTAFILENAME, qfextension[USRQUOTA]);