script/autobuild.py: add support git worktree
[Samba.git] / source3 / lib / sysquotas.c
blob64e41f740a32e312c246278917026b64491a443e
1 /*
2 Unix SMB/CIFS implementation.
3 System QUOTA function wrappers
4 Copyright (C) Stefan (metze) Metzmacher 2003
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.
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.
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/>.
21 #include "includes.h"
22 #include "lib/util_file.h"
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_QUOTA
27 #ifdef HAVE_SYS_QUOTAS
29 #if defined(HAVE_QUOTACTL_4A)
31 /*#endif HAVE_QUOTACTL_4A */
32 #elif defined(HAVE_QUOTACTL_4B)
34 /*#endif HAVE_QUOTACTL_4B */
35 #elif defined(HAVE_QUOTACTL_3)
37 #error HAVE_QUOTACTL_3 not implemented
39 /* #endif HAVE_QUOTACTL_3 */
40 #else /* NO_QUOTACTL_USED */
42 #endif /* NO_QUOTACTL_USED */
44 #if defined(HAVE_MNTENT) && defined(HAVE_REALPATH)
45 static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
47 int ret = -1;
48 SMB_STRUCT_STAT S;
49 FILE *fp;
50 struct mntent *mnt = NULL;
51 SMB_DEV_T devno;
52 char *stat_mntpath = NULL;
53 char *p;
55 /* find the block device file */
56 (*mntpath) = NULL;
57 (*bdev) = NULL;
58 (*fs) = NULL;
60 if (sys_stat(path, &S, false) != 0) {
61 return -1;
64 devno = S.st_ex_dev ;
66 stat_mntpath = sys_realpath(path);
67 if (stat_mntpath == NULL) {
68 DBG_WARNING("realpath(%s) failed - %s\n", path,
69 strerror(errno));
70 goto out;
73 if (sys_stat(stat_mntpath, &S, false) != 0) {
74 DBG_WARNING("cannot stat real path %s - %s\n", stat_mntpath,
75 strerror(errno));
76 goto out;
79 if (S.st_ex_dev != devno) {
80 DBG_WARNING("device on real path has changed\n");
81 goto out;
84 while (true) {
85 char save_ch;
87 p = strrchr(stat_mntpath, '/');
88 if (p == NULL) {
89 DBG_ERR("realpath for %s does not begin with a '/'\n",
90 path);
91 goto out;
94 if (p == stat_mntpath) {
95 ++p;
98 save_ch = *p;
99 *p = 0;
100 if (sys_stat(stat_mntpath, &S, false) != 0) {
101 DBG_WARNING("cannot stat real path component %s - %s\n",
102 stat_mntpath, strerror(errno));
103 goto out;
105 if (S.st_ex_dev != devno) {
106 *p = save_ch;
107 break;
110 if (p <= stat_mntpath + 1) {
111 break;
115 fp = setmntent(MOUNTED,"r");
116 if (fp == NULL) {
117 goto out;
120 while ((mnt = getmntent(fp))) {
121 if (!strequal(mnt->mnt_dir, stat_mntpath)) {
122 continue;
125 if ( sys_stat(mnt->mnt_dir, &S, false) == -1 )
126 continue ;
128 if (S.st_ex_dev == devno) {
129 (*mntpath) = SMB_STRDUP(mnt->mnt_dir);
130 (*bdev) = SMB_STRDUP(mnt->mnt_fsname);
131 (*fs) = SMB_STRDUP(mnt->mnt_type);
132 if ((*mntpath)&&(*bdev)&&(*fs)) {
133 ret = 0;
134 } else {
135 SAFE_FREE(*mntpath);
136 SAFE_FREE(*bdev);
137 SAFE_FREE(*fs);
138 ret = -1;
141 break;
145 endmntent(fp) ;
147 out:
148 SAFE_FREE(stat_mntpath);
149 return ret;
151 /* #endif HAVE_MNTENT */
152 #elif defined(HAVE_DEVNM)
154 /* we have this on HPUX, ... */
155 static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
157 int ret = -1;
158 char dev_disk[256];
159 SMB_STRUCT_STAT S;
161 if (!path||!mntpath||!bdev||!fs)
162 smb_panic("sys_path_to_bdev: called with NULL pointer");
164 (*mntpath) = NULL;
165 (*bdev) = NULL;
166 (*fs) = NULL;
168 /* find the block device file */
170 if ((ret=sys_stat(path, &S, false))!=0) {
171 return ret;
174 if ((ret=devnm(S_IFBLK, S.st_ex_dev, dev_disk, 256, 1))!=0) {
175 return ret;
178 /* we should get the mntpath right...
179 * but I don't know how
180 * --metze
182 (*mntpath) = SMB_STRDUP(path);
183 (*bdev) = SMB_STRDUP(dev_disk);
184 if ((*mntpath)&&(*bdev)) {
185 ret = 0;
186 } else {
187 SAFE_FREE(*mntpath);
188 SAFE_FREE(*bdev);
189 ret = -1;
193 return ret;
196 /* #endif HAVE_DEVNM */
197 #else
198 /* we should fake this up...*/
199 static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
201 int ret = -1;
203 if (!path||!mntpath||!bdev||!fs)
204 smb_panic("sys_path_to_bdev: called with NULL pointer");
206 (*mntpath) = NULL;
207 (*bdev) = NULL;
208 (*fs) = NULL;
210 (*mntpath) = SMB_STRDUP(path);
211 if (*mntpath) {
212 ret = 0;
213 } else {
214 SAFE_FREE(*mntpath);
215 ret = -1;
218 return ret;
220 #endif
222 /*********************************************************************
223 Now the list of all filesystem specific quota systems we have found
224 **********************************************************************/
225 static struct {
226 const char *name;
227 int (*get_quota)(const char *path, const char *bdev, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp);
228 int (*set_quota)(const char *path, const char *bdev, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp);
229 } sys_quota_backends[] = {
230 #ifdef HAVE_JFS_QUOTA_H
231 {"jfs2", sys_get_jfs2_quota, sys_set_jfs2_quota},
232 #endif
233 #if defined HAVE_XFS_QUOTAS
234 {"xfs", sys_get_xfs_quota, sys_set_xfs_quota},
235 {"gfs", sys_get_xfs_quota, sys_set_xfs_quota},
236 {"gfs2", sys_get_xfs_quota, sys_set_xfs_quota},
237 #endif /* HAVE_XFS_QUOTAS */
238 #ifdef HAVE_NFS_QUOTAS
239 {"nfs", sys_get_nfs_quota, sys_set_nfs_quota},
240 {"nfs4", sys_get_nfs_quota, sys_set_nfs_quota},
241 #endif /* HAVE_NFS_QUOTAS */
242 {NULL, NULL, NULL}
245 static int command_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
247 const struct loadparm_substitution *lp_sub =
248 loadparm_s3_global_substitution();
249 const char *get_quota_command;
250 char **lines = NULL;
252 get_quota_command = lp_get_quota_command(talloc_tos(), lp_sub);
253 if (get_quota_command && *get_quota_command) {
254 const char *p;
255 char *p2;
256 int _id = -1;
257 int error = 0;
258 char **argl = NULL;
260 switch(qtype) {
261 case SMB_USER_QUOTA_TYPE:
262 case SMB_USER_FS_QUOTA_TYPE:
263 _id = id.uid;
264 break;
265 case SMB_GROUP_QUOTA_TYPE:
266 case SMB_GROUP_FS_QUOTA_TYPE:
267 _id = id.gid;
268 break;
269 default:
270 DEBUG(0,("invalid quota type.\n"));
271 return -1;
274 argl = talloc_zero_array(talloc_tos(), char *, 5);
275 if (argl == NULL) {
276 return -1;
278 argl[0] = talloc_strdup(argl, get_quota_command);
279 if (argl[0] == NULL) {
280 TALLOC_FREE(argl);
281 return -1;
283 argl[1] = talloc_strdup(argl, path);
284 if (argl[1] == NULL) {
285 TALLOC_FREE(argl);
286 return -1;
288 argl[2] = talloc_asprintf(argl, "%d", qtype);
289 if (argl[2] == NULL) {
290 TALLOC_FREE(argl);
291 return -1;
293 argl[3] = talloc_asprintf(argl, "%d", _id);
294 if (argl[3] == NULL) {
295 TALLOC_FREE(argl);
296 return -1;
298 argl[4] = NULL;
300 DBG_NOTICE("Running command %s %s %d %d\n",
301 get_quota_command,
302 path,
303 qtype,
304 _id);
306 lines = file_lines_ploadv(talloc_tos(), argl, NULL);
307 TALLOC_FREE(argl);
309 if (lines) {
310 char *line = lines[0];
312 DEBUG (3, ("Read output from get_quota, \"%s\"\n", line));
314 /* we need to deal with long long unsigned here, if supported */
316 dp->qflags = smb_strtoul(line,
317 &p2,
319 &error,
320 SMB_STR_STANDARD);
321 if (error != 0) {
322 goto invalid_param;
325 p = p2;
326 while (p && *p && isspace(*p)) {
327 p++;
330 if (p && *p) {
331 dp->curblocks = STR_TO_SMB_BIG_UINT(p, &p);
332 } else {
333 goto invalid_param;
336 while (p && *p && isspace(*p)) {
337 p++;
340 if (p && *p) {
341 dp->softlimit = STR_TO_SMB_BIG_UINT(p, &p);
342 } else {
343 goto invalid_param;
346 while (p && *p && isspace(*p)) {
347 p++;
350 if (p && *p) {
351 dp->hardlimit = STR_TO_SMB_BIG_UINT(p, &p);
352 } else {
353 goto invalid_param;
356 while (p && *p && isspace(*p)) {
357 p++;
360 if (p && *p) {
361 dp->curinodes = STR_TO_SMB_BIG_UINT(p, &p);
362 } else {
363 goto invalid_param;
366 while (p && *p && isspace(*p)) {
367 p++;
370 if (p && *p) {
371 dp->isoftlimit = STR_TO_SMB_BIG_UINT(p, &p);
372 } else {
373 goto invalid_param;
376 while (p && *p && isspace(*p)) {
377 p++;
380 if (p && *p) {
381 dp->ihardlimit = STR_TO_SMB_BIG_UINT(p, &p);
382 } else {
383 goto invalid_param;
386 while (p && *p && isspace(*p)) {
387 p++;
390 if (p && *p) {
391 dp->bsize = STR_TO_SMB_BIG_UINT(p, NULL);
392 } else {
393 dp->bsize = 1024;
396 TALLOC_FREE(lines);
397 lines = NULL;
399 DEBUG (3, ("Parsed output of get_quota, ...\n"));
401 DEBUGADD (5,(
402 "qflags:%u curblocks:%llu softlimit:%llu hardlimit:%llu\n"
403 "curinodes:%llu isoftlimit:%llu ihardlimit:%llu bsize:%llu\n",
404 dp->qflags,(long long unsigned)dp->curblocks,
405 (long long unsigned)dp->softlimit,(long long unsigned)dp->hardlimit,
406 (long long unsigned)dp->curinodes,
407 (long long unsigned)dp->isoftlimit,(long long unsigned)dp->ihardlimit,
408 (long long unsigned)dp->bsize));
409 return 0;
412 DEBUG (0, ("get_quota_command failed!\n"));
413 return -1;
416 errno = ENOSYS;
417 return -1;
419 invalid_param:
421 TALLOC_FREE(lines);
422 DEBUG(0,("The output of get_quota_command is invalid!\n"));
423 return -1;
426 static int command_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
428 const struct loadparm_substitution *lp_sub =
429 loadparm_s3_global_substitution();
430 const char *set_quota_command;
432 set_quota_command = lp_set_quota_command(talloc_tos(), lp_sub);
433 if (set_quota_command && *set_quota_command) {
434 char **lines = NULL;
435 int _id = -1;
436 char **argl = NULL;
438 switch(qtype) {
439 case SMB_USER_QUOTA_TYPE:
440 case SMB_USER_FS_QUOTA_TYPE:
441 _id = id.uid;
442 break;
443 case SMB_GROUP_QUOTA_TYPE:
444 case SMB_GROUP_FS_QUOTA_TYPE:
445 _id = id.gid;
446 break;
447 default:
448 return -1;
451 argl = talloc_zero_array(talloc_tos(), char *, 11);
452 if (argl == NULL) {
453 return -1;
455 argl[0] = talloc_strdup(argl, set_quota_command);
456 if (argl[0] == NULL) {
457 TALLOC_FREE(argl);
458 return -1;
460 argl[1] = talloc_strdup(argl, path);
461 if (argl[1] == NULL) {
462 TALLOC_FREE(argl);
463 return -1;
465 argl[2] = talloc_asprintf(argl, "%d", qtype);
466 if (argl[2] == NULL) {
467 TALLOC_FREE(argl);
468 return -1;
470 argl[3] = talloc_asprintf(argl, "%d", _id);
471 if (argl[3] == NULL) {
472 TALLOC_FREE(argl);
473 return -1;
475 argl[4] = talloc_asprintf(argl, "%u", dp->qflags);
476 if (argl[4] == NULL) {
477 TALLOC_FREE(argl);
478 return -1;
480 argl[5] = talloc_asprintf(argl, "%llu",
481 (long long unsigned)dp->softlimit);
482 if (argl[5] == NULL) {
483 TALLOC_FREE(argl);
484 return -1;
486 argl[6] = talloc_asprintf(argl, "%llu",
487 (long long unsigned)dp->hardlimit);
488 if (argl[6] == NULL) {
489 TALLOC_FREE(argl);
490 return -1;
492 argl[7] = talloc_asprintf(argl, "%llu",
493 (long long unsigned)dp->isoftlimit);
494 if (argl[7] == NULL) {
495 TALLOC_FREE(argl);
496 return -1;
498 argl[8] = talloc_asprintf(argl, "%llu",
499 (long long unsigned)dp->ihardlimit);
500 if (argl[8] == NULL) {
501 TALLOC_FREE(argl);
502 return -1;
504 argl[9] = talloc_asprintf(argl, "%llu",
505 (long long unsigned)dp->bsize);
506 if (argl[9] == NULL) {
507 TALLOC_FREE(argl);
508 return -1;
510 argl[10] = NULL;
512 DBG_NOTICE("Running command "
513 "%s %s %d %d "
514 "%u %llu %llu "
515 "%llu %llu %llu ",
516 set_quota_command,
517 path,
518 qtype,
519 _id,
520 dp->qflags,
521 (long long unsigned)dp->softlimit,
522 (long long unsigned)dp->hardlimit,
523 (long long unsigned)dp->isoftlimit,
524 (long long unsigned)dp->ihardlimit,
525 (long long unsigned)dp->bsize);
527 lines = file_lines_ploadv(talloc_tos(), argl, NULL);
528 TALLOC_FREE(argl);
529 if (lines) {
530 char *line = lines[0];
532 DEBUG (3, ("Read output from set_quota, \"%s\"\n", line));
534 TALLOC_FREE(lines);
536 return 0;
538 DEBUG (0, ("set_quota_command failed!\n"));
539 return -1;
542 errno = ENOSYS;
543 return -1;
546 int sys_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
548 int ret = -1;
549 int i;
550 bool ready = False;
551 char *mntpath = NULL;
552 char *bdev = NULL;
553 char *fs = NULL;
555 if (!path||!dp)
556 smb_panic("sys_get_quota: called with NULL pointer");
558 if (command_get_quota(path, qtype, id, dp)==0) {
559 return 0;
560 } else if (errno != ENOSYS) {
561 return -1;
564 if ((ret=sys_path_to_bdev(path,&mntpath,&bdev,&fs))!=0) {
565 DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path));
566 return ret;
569 errno = 0;
570 DEBUG(10,("sys_get_quota() uid(%u, %u), fs(%s)\n", (unsigned)getuid(), (unsigned)geteuid(), fs));
572 for (i=0;(fs && sys_quota_backends[i].name && sys_quota_backends[i].get_quota);i++) {
573 if (strcmp(fs,sys_quota_backends[i].name)==0) {
574 ret = sys_quota_backends[i].get_quota(mntpath, bdev, qtype, id, dp);
575 if (ret!=0) {
576 DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
577 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
578 } else {
579 DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
580 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
582 ready = True;
583 break;
587 if (!ready) {
588 /* use the default vfs quota functions */
589 ret=sys_get_vfs_quota(mntpath, bdev, qtype, id, dp);
590 if (ret!=0) {
591 DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s\n",
592 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
593 } else {
594 DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
595 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
599 SAFE_FREE(mntpath);
600 SAFE_FREE(bdev);
601 SAFE_FREE(fs);
603 return ret;
606 int sys_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
608 int ret = -1;
609 int i;
610 bool ready = False;
611 char *mntpath = NULL;
612 char *bdev = NULL;
613 char *fs = NULL;
615 /* find the block device file */
617 if (!path||!dp)
618 smb_panic("get_smb_quota: called with NULL pointer");
620 if (command_set_quota(path, qtype, id, dp)==0) {
621 return 0;
622 } else if (errno != ENOSYS) {
623 return -1;
626 if ((ret=sys_path_to_bdev(path,&mntpath,&bdev,&fs))!=0) {
627 DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path));
628 return ret;
631 errno = 0;
632 DEBUG(10,("sys_set_quota() uid(%u, %u)\n", (unsigned)getuid(), (unsigned)geteuid()));
634 for (i=0;(fs && sys_quota_backends[i].name && sys_quota_backends[i].set_quota);i++) {
635 if (strcmp(fs,sys_quota_backends[i].name)==0) {
636 ret = sys_quota_backends[i].set_quota(mntpath, bdev, qtype, id, dp);
637 if (ret!=0) {
638 DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
639 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
640 } else {
641 DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
642 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
644 ready = True;
645 break;
649 if (!ready) {
650 /* use the default vfs quota functions */
651 ret=sys_set_vfs_quota(mntpath, bdev, qtype, id, dp);
652 if (ret!=0) {
653 DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
654 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
655 } else {
656 DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
657 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
661 SAFE_FREE(mntpath);
662 SAFE_FREE(bdev);
663 SAFE_FREE(fs);
665 return ret;
668 #else /* HAVE_SYS_QUOTAS */
669 void dummy_sysquotas_c(void);
671 void dummy_sysquotas_c(void)
673 return;
675 #endif /* HAVE_SYS_QUOTAS */