source: branches/samba-3.5.x/source3/lib/sysquotas.c@ 1075

Last change on this file since 1075 was 739, checked in by Silvan Scherrer, 13 years ago

Samba Server 3.5: update branche to 3.5.19

File size: 13.4 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 System QUOTA function wrappers
4 Copyright (C) Stefan (metze) Metzmacher 2003
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20
21#include "includes.h"
22
23#undef DBGC_CLASS
24#define DBGC_CLASS DBGC_QUOTA
25
26#ifdef HAVE_SYS_QUOTAS
27
28#if defined(HAVE_QUOTACTL_4A)
29
30/*#endif HAVE_QUOTACTL_4A */
31#elif defined(HAVE_QUOTACTL_4B)
32
33#error HAVE_QUOTACTL_4B not implemeted
34
35/*#endif HAVE_QUOTACTL_4B */
36#elif defined(HAVE_QUOTACTL_3)
37
38#error HAVE_QUOTACTL_3 not implemented
39
40/* #endif HAVE_QUOTACTL_3 */
41#else /* NO_QUOTACTL_USED */
42
43#endif /* NO_QUOTACTL_USED */
44
45#ifdef HAVE_MNTENT
46static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
47{
48 int ret = -1;
49 SMB_STRUCT_STAT S;
50 FILE *fp;
51 struct mntent *mnt;
52 SMB_DEV_T devno;
53
54 /* find the block device file */
55
56 if (!path||!mntpath||!bdev||!fs)
57 smb_panic("sys_path_to_bdev: called with NULL pointer");
58
59 (*mntpath) = NULL;
60 (*bdev) = NULL;
61 (*fs) = NULL;
62
63 if ( sys_stat(path, &S, false) == -1 )
64 return (-1);
65
66 devno = S.st_ex_dev ;
67
68 fp = setmntent(MOUNTED,"r");
69 if (fp == NULL) {
70 return -1;
71 }
72
73 while ((mnt = getmntent(fp))) {
74 if ( sys_stat(mnt->mnt_dir, &S, false) == -1 )
75 continue ;
76
77 if (S.st_ex_dev == devno) {
78 (*mntpath) = SMB_STRDUP(mnt->mnt_dir);
79 (*bdev) = SMB_STRDUP(mnt->mnt_fsname);
80 (*fs) = SMB_STRDUP(mnt->mnt_type);
81 if ((*mntpath)&&(*bdev)&&(*fs)) {
82 ret = 0;
83 } else {
84 SAFE_FREE(*mntpath);
85 SAFE_FREE(*bdev);
86 SAFE_FREE(*fs);
87 ret = -1;
88 }
89
90 break;
91 }
92 }
93
94 endmntent(fp) ;
95
96 return ret;
97}
98/* #endif HAVE_MNTENT */
99#elif defined(HAVE_DEVNM)
100
101/* we have this on HPUX, ... */
102static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
103{
104 int ret = -1;
105 char dev_disk[256];
106 SMB_STRUCT_STAT S;
107
108 if (!path||!mntpath||!bdev||!fs)
109 smb_panic("sys_path_to_bdev: called with NULL pointer");
110
111 (*mntpath) = NULL;
112 (*bdev) = NULL;
113 (*fs) = NULL;
114
115 /* find the block device file */
116
117 if ((ret=sys_stat(path, &S, false))!=0) {
118 return ret;
119 }
120
121 if ((ret=devnm(S_IFBLK, S.st_ex_dev, dev_disk, 256, 1))!=0) {
122 return ret;
123 }
124
125 /* we should get the mntpath right...
126 * but I don't know how
127 * --metze
128 */
129 (*mntpath) = SMB_STRDUP(path);
130 (*bdev) = SMB_STRDUP(dev_disk);
131 if ((*mntpath)&&(*bdev)) {
132 ret = 0;
133 } else {
134 SAFE_FREE(*mntpath);
135 SAFE_FREE(*bdev);
136 ret = -1;
137 }
138
139
140 return ret;
141}
142
143/* #endif HAVE_DEVNM */
144#else
145/* we should fake this up...*/
146static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
147{
148 int ret = -1;
149
150 if (!path||!mntpath||!bdev||!fs)
151 smb_panic("sys_path_to_bdev: called with NULL pointer");
152
153 (*mntpath) = NULL;
154 (*bdev) = NULL;
155 (*fs) = NULL;
156
157 (*mntpath) = SMB_STRDUP(path);
158 if (*mntpath) {
159 ret = 0;
160 } else {
161 SAFE_FREE(*mntpath);
162 ret = -1;
163 }
164
165 return ret;
166}
167#endif
168
169/*********************************************************************
170 Now the list of all filesystem specific quota systems we have found
171**********************************************************************/
172static struct {
173 const char *name;
174 int (*get_quota)(const char *path, const char *bdev, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp);
175 int (*set_quota)(const char *path, const char *bdev, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp);
176} sys_quota_backends[] = {
177#ifdef HAVE_XFS_QUOTAS
178 {"xfs", sys_get_xfs_quota, sys_set_xfs_quota},
179 {"gfs", sys_get_xfs_quota, sys_set_xfs_quota},
180 {"gfs2", sys_get_xfs_quota, sys_set_xfs_quota},
181#endif /* HAVE_XFS_QUOTAS */
182 {NULL, NULL, NULL}
183};
184
185static int command_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
186{
187 const char *get_quota_command;
188 char **lines = NULL;
189
190 get_quota_command = lp_get_quota_command();
191 if (get_quota_command && *get_quota_command) {
192 const char *p;
193 char *p2;
194 char *syscmd = NULL;
195 int _id = -1;
196
197 switch(qtype) {
198 case SMB_USER_QUOTA_TYPE:
199 case SMB_USER_FS_QUOTA_TYPE:
200 _id = id.uid;
201 break;
202 case SMB_GROUP_QUOTA_TYPE:
203 case SMB_GROUP_FS_QUOTA_TYPE:
204 _id = id.gid;
205 break;
206 default:
207 DEBUG(0,("invalid quota type.\n"));
208 return -1;
209 }
210
211 if (asprintf(&syscmd, "%s \"%s\" %d %d",
212 get_quota_command, path, qtype, _id) < 0) {
213 return -1;
214 }
215
216 DEBUG (3, ("get_quota: Running command %s\n", syscmd));
217
218 lines = file_lines_pload(syscmd, NULL);
219 SAFE_FREE(syscmd);
220
221 if (lines) {
222 char *line = lines[0];
223
224 DEBUG (3, ("Read output from get_quota, \"%s\"\n", line));
225
226 /* we need to deal with long long unsigned here, if supported */
227
228 dp->qflags = (enum SMB_QUOTA_TYPE)strtoul(line, &p2, 10);
229 p = p2;
230 while (p && *p && isspace(*p)) {
231 p++;
232 }
233
234 if (p && *p) {
235 dp->curblocks = STR_TO_SMB_BIG_UINT(p, &p);
236 } else {
237 goto invalid_param;
238 }
239
240 while (p && *p && isspace(*p)) {
241 p++;
242 }
243
244 if (p && *p) {
245 dp->softlimit = STR_TO_SMB_BIG_UINT(p, &p);
246 } else {
247 goto invalid_param;
248 }
249
250 while (p && *p && isspace(*p)) {
251 p++;
252 }
253
254 if (p && *p) {
255 dp->hardlimit = STR_TO_SMB_BIG_UINT(p, &p);
256 } else {
257 goto invalid_param;
258 }
259
260 while (p && *p && isspace(*p)) {
261 p++;
262 }
263
264 if (p && *p) {
265 dp->curinodes = STR_TO_SMB_BIG_UINT(p, &p);
266 } else {
267 goto invalid_param;
268 }
269
270 while (p && *p && isspace(*p)) {
271 p++;
272 }
273
274 if (p && *p) {
275 dp->isoftlimit = STR_TO_SMB_BIG_UINT(p, &p);
276 } else {
277 goto invalid_param;
278 }
279
280 while (p && *p && isspace(*p)) {
281 p++;
282 }
283
284 if (p && *p) {
285 dp->ihardlimit = STR_TO_SMB_BIG_UINT(p, &p);
286 } else {
287 goto invalid_param;
288 }
289
290 while (p && *p && isspace(*p)) {
291 p++;
292 }
293
294 if (p && *p) {
295 dp->bsize = STR_TO_SMB_BIG_UINT(p, NULL);
296 } else {
297 dp->bsize = 1024;
298 }
299
300 TALLOC_FREE(lines);
301 lines = NULL;
302
303 DEBUG (3, ("Parsed output of get_quota, ...\n"));
304
305#ifdef LARGE_SMB_OFF_T
306 DEBUGADD (5,(
307 "qflags:%u curblocks:%llu softlimit:%llu hardlimit:%llu\n"
308 "curinodes:%llu isoftlimit:%llu ihardlimit:%llu bsize:%llu\n",
309 dp->qflags,(long long unsigned)dp->curblocks,
310 (long long unsigned)dp->softlimit,(long long unsigned)dp->hardlimit,
311 (long long unsigned)dp->curinodes,
312 (long long unsigned)dp->isoftlimit,(long long unsigned)dp->ihardlimit,
313 (long long unsigned)dp->bsize));
314#else /* LARGE_SMB_OFF_T */
315 DEBUGADD (5,(
316 "qflags:%u curblocks:%lu softlimit:%lu hardlimit:%lu\n"
317 "curinodes:%lu isoftlimit:%lu ihardlimit:%lu bsize:%lu\n",
318 dp->qflags,(long unsigned)dp->curblocks,
319 (long unsigned)dp->softlimit,(long unsigned)dp->hardlimit,
320 (long unsigned)dp->curinodes,
321 (long unsigned)dp->isoftlimit,(long unsigned)dp->ihardlimit,
322 (long unsigned)dp->bsize));
323#endif /* LARGE_SMB_OFF_T */
324 return 0;
325 }
326
327 DEBUG (0, ("get_quota_command failed!\n"));
328 return -1;
329 }
330
331 errno = ENOSYS;
332 return -1;
333
334invalid_param:
335
336 TALLOC_FREE(lines);
337 DEBUG(0,("The output of get_quota_command is invalid!\n"));
338 return -1;
339}
340
341static int command_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
342{
343 const char *set_quota_command;
344
345 set_quota_command = lp_set_quota_command();
346 if (set_quota_command && *set_quota_command) {
347 char **lines = NULL;
348 char *syscmd = NULL;
349 int _id = -1;
350
351 switch(qtype) {
352 case SMB_USER_QUOTA_TYPE:
353 case SMB_USER_FS_QUOTA_TYPE:
354 _id = id.uid;
355 break;
356 case SMB_GROUP_QUOTA_TYPE:
357 case SMB_GROUP_FS_QUOTA_TYPE:
358 _id = id.gid;
359 break;
360 default:
361 return -1;
362 }
363
364#ifdef LARGE_SMB_OFF_T
365 if (asprintf(&syscmd,
366 "%s \"%s\" %d %d "
367 "%u %llu %llu "
368 "%llu %llu %llu ",
369 set_quota_command, path, qtype, _id, dp->qflags,
370 (long long unsigned)dp->softlimit,(long long unsigned)dp->hardlimit,
371 (long long unsigned)dp->isoftlimit,(long long unsigned)dp->ihardlimit,
372 (long long unsigned)dp->bsize) < 0) {
373 return -1;
374 }
375#else /* LARGE_SMB_OFF_T */
376 if (asprintf(&syscmd,
377 "%s \"%s\" %d %d "
378 "%u %lu %lu "
379 "%lu %lu %lu ",
380 set_quota_command, path, qtype, _id, dp->qflags,
381 (long unsigned)dp->softlimit,(long unsigned)dp->hardlimit,
382 (long unsigned)dp->isoftlimit,(long unsigned)dp->ihardlimit,
383 (long unsigned)dp->bsize) < 0) {
384 return -1;
385 }
386#endif /* LARGE_SMB_OFF_T */
387
388 DEBUG (3, ("get_quota: Running command %s\n", syscmd));
389
390 lines = file_lines_pload(syscmd, NULL);
391 SAFE_FREE(syscmd);
392 if (lines) {
393 char *line = lines[0];
394
395 DEBUG (3, ("Read output from set_quota, \"%s\"\n", line));
396
397 TALLOC_FREE(lines);
398
399 return 0;
400 }
401 DEBUG (0, ("set_quota_command failed!\n"));
402 return -1;
403 }
404
405 errno = ENOSYS;
406 return -1;
407}
408
409int sys_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
410{
411 int ret = -1;
412 int i;
413 bool ready = False;
414 char *mntpath = NULL;
415 char *bdev = NULL;
416 char *fs = NULL;
417
418 if (!path||!dp)
419 smb_panic("sys_get_quota: called with NULL pointer");
420
421 if (command_get_quota(path, qtype, id, dp)==0) {
422 return 0;
423 } else if (errno != ENOSYS) {
424 return -1;
425 }
426
427 if ((ret=sys_path_to_bdev(path,&mntpath,&bdev,&fs))!=0) {
428 DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path));
429 return ret;
430 }
431
432 errno = 0;
433 DEBUG(10,("sys_get_quota() uid(%u, %u)\n", (unsigned)getuid(), (unsigned)geteuid()));
434
435 for (i=0;(fs && sys_quota_backends[i].name && sys_quota_backends[i].get_quota);i++) {
436 if (strcmp(fs,sys_quota_backends[i].name)==0) {
437 ret = sys_quota_backends[i].get_quota(mntpath, bdev, qtype, id, dp);
438 if (ret!=0) {
439 DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
440 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
441 } else {
442 DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
443 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
444 }
445 ready = True;
446 break;
447 }
448 }
449
450 if (!ready) {
451 /* use the default vfs quota functions */
452 ret=sys_get_vfs_quota(mntpath, bdev, qtype, id, dp);
453 if (ret!=0) {
454 DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s\n",
455 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
456 } else {
457 DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
458 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
459 }
460 }
461
462 SAFE_FREE(mntpath);
463 SAFE_FREE(bdev);
464 SAFE_FREE(fs);
465
466 if ((ret!=0)&& (errno == EDQUOT)) {
467 DEBUG(10,("sys_get_quota() warning over quota!\n"));
468 return 0;
469 }
470
471 return ret;
472}
473
474int sys_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
475{
476 int ret = -1;
477 int i;
478 bool ready = False;
479 char *mntpath = NULL;
480 char *bdev = NULL;
481 char *fs = NULL;
482
483 /* find the block device file */
484
485 if (!path||!dp)
486 smb_panic("get_smb_quota: called with NULL pointer");
487
488 if (command_set_quota(path, qtype, id, dp)==0) {
489 return 0;
490 } else if (errno != ENOSYS) {
491 return -1;
492 }
493
494 if ((ret=sys_path_to_bdev(path,&mntpath,&bdev,&fs))!=0) {
495 DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path));
496 return ret;
497 }
498
499 errno = 0;
500 DEBUG(10,("sys_set_quota() uid(%u, %u)\n", (unsigned)getuid(), (unsigned)geteuid()));
501
502 for (i=0;(fs && sys_quota_backends[i].name && sys_quota_backends[i].set_quota);i++) {
503 if (strcmp(fs,sys_quota_backends[i].name)==0) {
504 ret = sys_quota_backends[i].set_quota(mntpath, bdev, qtype, id, dp);
505 if (ret!=0) {
506 DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
507 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
508 } else {
509 DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
510 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
511 }
512 ready = True;
513 break;
514 }
515 }
516
517 if (!ready) {
518 /* use the default vfs quota functions */
519 ret=sys_set_vfs_quota(mntpath, bdev, qtype, id, dp);
520 if (ret!=0) {
521 DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
522 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
523 } else {
524 DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
525 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
526 }
527 }
528
529 SAFE_FREE(mntpath);
530 SAFE_FREE(bdev);
531 SAFE_FREE(fs);
532
533 if ((ret!=0)&& (errno == EDQUOT)) {
534 DEBUG(10,("sys_set_quota() warning over quota!\n"));
535 return 0;
536 }
537
538 return ret;
539}
540
541#else /* HAVE_SYS_QUOTAS */
542 void dummy_sysquotas_c(void);
543
544 void dummy_sysquotas_c(void)
545{
546 return;
547}
548#endif /* HAVE_SYS_QUOTAS */
549
Note: See TracBrowser for help on using the repository browser.