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