s3:smbd: rename has_ctdb_public_ip to has_cluster_movable_ip
[Samba.git] / source3 / lib / sysquotas.c
blob6553cac40dae11e110d13d8440ea9945f1404c1c
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"
23 #include "lib/util/smb_strtox.h"
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_QUOTA
28 #ifdef HAVE_SYS_QUOTAS
30 #if defined(HAVE_QUOTACTL_4A)
32 /*#endif HAVE_QUOTACTL_4A */
33 #elif defined(HAVE_QUOTACTL_4B)
35 /*#endif HAVE_QUOTACTL_4B */
36 #elif defined(HAVE_QUOTACTL_3)
38 #error HAVE_QUOTACTL_3 not implemented
40 /* #endif HAVE_QUOTACTL_3 */
41 #else /* NO_QUOTACTL_USED */
43 #endif /* NO_QUOTACTL_USED */
45 #if defined(HAVE_MNTENT) && defined(HAVE_REALPATH)
46 static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
48 int ret = -1;
49 SMB_STRUCT_STAT S;
50 FILE *fp;
51 struct mntent *mnt = NULL;
52 SMB_DEV_T devno;
53 char *stat_mntpath = NULL;
54 char *p;
56 /* find the block device file */
57 (*mntpath) = NULL;
58 (*bdev) = NULL;
59 (*fs) = NULL;
61 if (sys_stat(path, &S, false) != 0) {
62 return -1;
65 devno = S.st_ex_dev ;
67 stat_mntpath = sys_realpath(path);
68 if (stat_mntpath == NULL) {
69 DBG_WARNING("realpath(%s) failed - %s\n", path,
70 strerror(errno));
71 goto out;
74 if (sys_stat(stat_mntpath, &S, false) != 0) {
75 DBG_WARNING("cannot stat real path %s - %s\n", stat_mntpath,
76 strerror(errno));
77 goto out;
80 if (S.st_ex_dev != devno) {
81 DBG_WARNING("device on real path has changed\n");
82 goto out;
85 while (true) {
86 char save_ch;
88 p = strrchr(stat_mntpath, '/');
89 if (p == NULL) {
90 DBG_ERR("realpath for %s does not begin with a '/'\n",
91 path);
92 goto out;
95 if (p == stat_mntpath) {
96 ++p;
99 save_ch = *p;
100 *p = 0;
101 if (sys_stat(stat_mntpath, &S, false) != 0) {
102 DBG_WARNING("cannot stat real path component %s - %s\n",
103 stat_mntpath, strerror(errno));
104 goto out;
106 if (S.st_ex_dev != devno) {
107 *p = save_ch;
108 break;
111 if (p <= stat_mntpath + 1) {
112 break;
116 fp = setmntent(MOUNTED,"r");
117 if (fp == NULL) {
118 goto out;
121 while ((mnt = getmntent(fp))) {
122 if (!strequal(mnt->mnt_dir, stat_mntpath)) {
123 continue;
126 if ( sys_stat(mnt->mnt_dir, &S, false) == -1 )
127 continue ;
129 if (S.st_ex_dev == devno) {
130 (*mntpath) = SMB_STRDUP(mnt->mnt_dir);
131 (*bdev) = SMB_STRDUP(mnt->mnt_fsname);
132 (*fs) = SMB_STRDUP(mnt->mnt_type);
133 if ((*mntpath)&&(*bdev)&&(*fs)) {
134 ret = 0;
135 } else {
136 SAFE_FREE(*mntpath);
137 SAFE_FREE(*bdev);
138 SAFE_FREE(*fs);
139 ret = -1;
142 break;
146 endmntent(fp) ;
148 out:
149 SAFE_FREE(stat_mntpath);
150 return ret;
152 /* #endif HAVE_MNTENT */
153 #elif defined(HAVE_DEVNM)
155 /* we have this on HPUX, ... */
156 static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
158 int ret = -1;
159 char dev_disk[256];
160 SMB_STRUCT_STAT S;
162 if (!path||!mntpath||!bdev||!fs)
163 smb_panic("sys_path_to_bdev: called with NULL pointer");
165 (*mntpath) = NULL;
166 (*bdev) = NULL;
167 (*fs) = NULL;
169 /* find the block device file */
171 if ((ret=sys_stat(path, &S, false))!=0) {
172 return ret;
175 if ((ret=devnm(S_IFBLK, S.st_ex_dev, dev_disk, 256, 1))!=0) {
176 return ret;
179 /* we should get the mntpath right...
180 * but I don't know how
181 * --metze
183 (*mntpath) = SMB_STRDUP(path);
184 (*bdev) = SMB_STRDUP(dev_disk);
185 if ((*mntpath)&&(*bdev)) {
186 ret = 0;
187 } else {
188 SAFE_FREE(*mntpath);
189 SAFE_FREE(*bdev);
190 ret = -1;
194 return ret;
197 /* #endif HAVE_DEVNM */
198 #else
199 /* we should fake this up...*/
200 static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
202 int ret = -1;
204 if (!path||!mntpath||!bdev||!fs)
205 smb_panic("sys_path_to_bdev: called with NULL pointer");
207 (*mntpath) = NULL;
208 (*bdev) = NULL;
209 (*fs) = NULL;
211 (*mntpath) = SMB_STRDUP(path);
212 if (*mntpath) {
213 ret = 0;
214 } else {
215 SAFE_FREE(*mntpath);
216 ret = -1;
219 return ret;
221 #endif
223 /*********************************************************************
224 Now the list of all filesystem specific quota systems we have found
225 **********************************************************************/
226 static struct {
227 const char *name;
228 int (*get_quota)(const char *path, const char *bdev, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp);
229 int (*set_quota)(const char *path, const char *bdev, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp);
230 } sys_quota_backends[] = {
231 #ifdef HAVE_JFS_QUOTA_H
232 {"jfs2", sys_get_jfs2_quota, sys_set_jfs2_quota},
233 #endif
234 #if defined HAVE_XFS_QUOTAS
235 {"xfs", sys_get_xfs_quota, sys_set_xfs_quota},
236 {"gfs", sys_get_xfs_quota, sys_set_xfs_quota},
237 {"gfs2", sys_get_xfs_quota, sys_set_xfs_quota},
238 #endif /* HAVE_XFS_QUOTAS */
239 #ifdef HAVE_NFS_QUOTAS
240 {"nfs", sys_get_nfs_quota, sys_set_nfs_quota},
241 {"nfs4", sys_get_nfs_quota, sys_set_nfs_quota},
242 #endif /* HAVE_NFS_QUOTAS */
243 {NULL, NULL, NULL}
246 static int command_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
248 const struct loadparm_substitution *lp_sub =
249 loadparm_s3_global_substitution();
250 const char *get_quota_command;
251 char **lines = NULL;
253 get_quota_command = lp_get_quota_command(talloc_tos(), lp_sub);
254 if (get_quota_command && *get_quota_command) {
255 const char *p;
256 char *p2;
257 int _id = -1;
258 int error = 0;
259 char **argl = NULL;
261 switch(qtype) {
262 case SMB_USER_QUOTA_TYPE:
263 case SMB_USER_FS_QUOTA_TYPE:
264 _id = id.uid;
265 break;
266 case SMB_GROUP_QUOTA_TYPE:
267 case SMB_GROUP_FS_QUOTA_TYPE:
268 _id = id.gid;
269 break;
270 default:
271 DEBUG(0,("invalid quota type.\n"));
272 return -1;
275 argl = talloc_zero_array(talloc_tos(), char *, 5);
276 if (argl == NULL) {
277 return -1;
279 argl[0] = talloc_strdup(argl, get_quota_command);
280 if (argl[0] == NULL) {
281 TALLOC_FREE(argl);
282 return -1;
284 argl[1] = talloc_strdup(argl, path);
285 if (argl[1] == NULL) {
286 TALLOC_FREE(argl);
287 return -1;
289 argl[2] = talloc_asprintf(argl, "%d", qtype);
290 if (argl[2] == NULL) {
291 TALLOC_FREE(argl);
292 return -1;
294 argl[3] = talloc_asprintf(argl, "%d", _id);
295 if (argl[3] == NULL) {
296 TALLOC_FREE(argl);
297 return -1;
299 argl[4] = NULL;
301 DBG_NOTICE("Running command %s %s %d %d\n",
302 get_quota_command,
303 path,
304 qtype,
305 _id);
307 lines = file_lines_ploadv(talloc_tos(), argl, NULL);
308 TALLOC_FREE(argl);
310 if (lines) {
311 char *line = lines[0];
313 DEBUG (3, ("Read output from get_quota, \"%s\"\n", line));
315 /* we need to deal with long long unsigned here, if supported */
317 dp->qflags = smb_strtoul(line,
318 &p2,
320 &error,
321 SMB_STR_STANDARD);
322 if (error != 0) {
323 goto invalid_param;
326 p = p2;
327 while (p && *p && isspace(*p)) {
328 p++;
331 if (p && *p) {
332 dp->curblocks = STR_TO_SMB_BIG_UINT(p, &p);
333 } else {
334 goto invalid_param;
337 while (p && *p && isspace(*p)) {
338 p++;
341 if (p && *p) {
342 dp->softlimit = STR_TO_SMB_BIG_UINT(p, &p);
343 } else {
344 goto invalid_param;
347 while (p && *p && isspace(*p)) {
348 p++;
351 if (p && *p) {
352 dp->hardlimit = STR_TO_SMB_BIG_UINT(p, &p);
353 } else {
354 goto invalid_param;
357 while (p && *p && isspace(*p)) {
358 p++;
361 if (p && *p) {
362 dp->curinodes = STR_TO_SMB_BIG_UINT(p, &p);
363 } else {
364 goto invalid_param;
367 while (p && *p && isspace(*p)) {
368 p++;
371 if (p && *p) {
372 dp->isoftlimit = STR_TO_SMB_BIG_UINT(p, &p);
373 } else {
374 goto invalid_param;
377 while (p && *p && isspace(*p)) {
378 p++;
381 if (p && *p) {
382 dp->ihardlimit = STR_TO_SMB_BIG_UINT(p, &p);
383 } else {
384 goto invalid_param;
387 while (p && *p && isspace(*p)) {
388 p++;
391 if (p && *p) {
392 dp->bsize = STR_TO_SMB_BIG_UINT(p, NULL);
393 } else {
394 dp->bsize = 1024;
397 TALLOC_FREE(lines);
398 lines = NULL;
400 DEBUG (3, ("Parsed output of get_quota, ...\n"));
402 DEBUGADD (5,(
403 "qflags:%u curblocks:%llu softlimit:%llu hardlimit:%llu\n"
404 "curinodes:%llu isoftlimit:%llu ihardlimit:%llu bsize:%llu\n",
405 dp->qflags,(long long unsigned)dp->curblocks,
406 (long long unsigned)dp->softlimit,(long long unsigned)dp->hardlimit,
407 (long long unsigned)dp->curinodes,
408 (long long unsigned)dp->isoftlimit,(long long unsigned)dp->ihardlimit,
409 (long long unsigned)dp->bsize));
410 return 0;
413 DEBUG (0, ("get_quota_command failed!\n"));
414 return -1;
417 errno = ENOSYS;
418 return -1;
420 invalid_param:
422 TALLOC_FREE(lines);
423 DEBUG(0,("The output of get_quota_command is invalid!\n"));
424 return -1;
427 static int command_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
429 const struct loadparm_substitution *lp_sub =
430 loadparm_s3_global_substitution();
431 const char *set_quota_command;
433 set_quota_command = lp_set_quota_command(talloc_tos(), lp_sub);
434 if (set_quota_command && *set_quota_command) {
435 char **lines = NULL;
436 int _id = -1;
437 char **argl = NULL;
439 switch(qtype) {
440 case SMB_USER_QUOTA_TYPE:
441 case SMB_USER_FS_QUOTA_TYPE:
442 _id = id.uid;
443 break;
444 case SMB_GROUP_QUOTA_TYPE:
445 case SMB_GROUP_FS_QUOTA_TYPE:
446 _id = id.gid;
447 break;
448 default:
449 return -1;
452 argl = talloc_zero_array(talloc_tos(), char *, 11);
453 if (argl == NULL) {
454 return -1;
456 argl[0] = talloc_strdup(argl, set_quota_command);
457 if (argl[0] == NULL) {
458 TALLOC_FREE(argl);
459 return -1;
461 argl[1] = talloc_strdup(argl, path);
462 if (argl[1] == NULL) {
463 TALLOC_FREE(argl);
464 return -1;
466 argl[2] = talloc_asprintf(argl, "%d", qtype);
467 if (argl[2] == NULL) {
468 TALLOC_FREE(argl);
469 return -1;
471 argl[3] = talloc_asprintf(argl, "%d", _id);
472 if (argl[3] == NULL) {
473 TALLOC_FREE(argl);
474 return -1;
476 argl[4] = talloc_asprintf(argl, "%u", dp->qflags);
477 if (argl[4] == NULL) {
478 TALLOC_FREE(argl);
479 return -1;
481 argl[5] = talloc_asprintf(argl, "%llu",
482 (long long unsigned)dp->softlimit);
483 if (argl[5] == NULL) {
484 TALLOC_FREE(argl);
485 return -1;
487 argl[6] = talloc_asprintf(argl, "%llu",
488 (long long unsigned)dp->hardlimit);
489 if (argl[6] == NULL) {
490 TALLOC_FREE(argl);
491 return -1;
493 argl[7] = talloc_asprintf(argl, "%llu",
494 (long long unsigned)dp->isoftlimit);
495 if (argl[7] == NULL) {
496 TALLOC_FREE(argl);
497 return -1;
499 argl[8] = talloc_asprintf(argl, "%llu",
500 (long long unsigned)dp->ihardlimit);
501 if (argl[8] == NULL) {
502 TALLOC_FREE(argl);
503 return -1;
505 argl[9] = talloc_asprintf(argl, "%llu",
506 (long long unsigned)dp->bsize);
507 if (argl[9] == NULL) {
508 TALLOC_FREE(argl);
509 return -1;
511 argl[10] = NULL;
513 DBG_NOTICE("Running command "
514 "%s %s %d %d "
515 "%u %llu %llu "
516 "%llu %llu %llu ",
517 set_quota_command,
518 path,
519 qtype,
520 _id,
521 dp->qflags,
522 (long long unsigned)dp->softlimit,
523 (long long unsigned)dp->hardlimit,
524 (long long unsigned)dp->isoftlimit,
525 (long long unsigned)dp->ihardlimit,
526 (long long unsigned)dp->bsize);
528 lines = file_lines_ploadv(talloc_tos(), argl, NULL);
529 TALLOC_FREE(argl);
530 if (lines) {
531 char *line = lines[0];
533 DEBUG (3, ("Read output from set_quota, \"%s\"\n", line));
535 TALLOC_FREE(lines);
537 return 0;
539 DEBUG (0, ("set_quota_command failed!\n"));
540 return -1;
543 errno = ENOSYS;
544 return -1;
547 int sys_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
549 int ret = -1;
550 int i;
551 bool ready = False;
552 char *mntpath = NULL;
553 char *bdev = NULL;
554 char *fs = NULL;
556 if (!path||!dp)
557 smb_panic("sys_get_quota: called with NULL pointer");
559 if (command_get_quota(path, qtype, id, dp)==0) {
560 return 0;
561 } else if (errno != ENOSYS) {
562 return -1;
565 if ((ret=sys_path_to_bdev(path,&mntpath,&bdev,&fs))!=0) {
566 DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path));
567 return ret;
570 errno = 0;
571 DEBUG(10,("sys_get_quota() uid(%u, %u), fs(%s)\n", (unsigned)getuid(), (unsigned)geteuid(), fs));
573 for (i=0;(fs && sys_quota_backends[i].name && sys_quota_backends[i].get_quota);i++) {
574 if (strcmp(fs,sys_quota_backends[i].name)==0) {
575 ret = sys_quota_backends[i].get_quota(mntpath, bdev, qtype, id, dp);
576 if (ret!=0) {
577 DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
578 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
579 } else {
580 DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
581 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
583 ready = True;
584 break;
588 if (!ready) {
589 /* use the default vfs quota functions */
590 ret=sys_get_vfs_quota(mntpath, bdev, qtype, id, dp);
591 if (ret!=0) {
592 DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s\n",
593 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
594 } else {
595 DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
596 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
600 SAFE_FREE(mntpath);
601 SAFE_FREE(bdev);
602 SAFE_FREE(fs);
604 return ret;
607 int sys_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
609 int ret = -1;
610 int i;
611 bool ready = False;
612 char *mntpath = NULL;
613 char *bdev = NULL;
614 char *fs = NULL;
616 /* find the block device file */
618 if (!path||!dp)
619 smb_panic("get_smb_quota: called with NULL pointer");
621 if (command_set_quota(path, qtype, id, dp)==0) {
622 return 0;
623 } else if (errno != ENOSYS) {
624 return -1;
627 if ((ret=sys_path_to_bdev(path,&mntpath,&bdev,&fs))!=0) {
628 DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path));
629 return ret;
632 errno = 0;
633 DEBUG(10,("sys_set_quota() uid(%u, %u)\n", (unsigned)getuid(), (unsigned)geteuid()));
635 for (i=0;(fs && sys_quota_backends[i].name && sys_quota_backends[i].set_quota);i++) {
636 if (strcmp(fs,sys_quota_backends[i].name)==0) {
637 ret = sys_quota_backends[i].set_quota(mntpath, bdev, qtype, id, dp);
638 if (ret!=0) {
639 DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
640 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
641 } else {
642 DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
643 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
645 ready = True;
646 break;
650 if (!ready) {
651 /* use the default vfs quota functions */
652 ret=sys_set_vfs_quota(mntpath, bdev, qtype, id, dp);
653 if (ret!=0) {
654 DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
655 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
656 } else {
657 DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
658 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
662 SAFE_FREE(mntpath);
663 SAFE_FREE(bdev);
664 SAFE_FREE(fs);
666 return ret;
669 #else /* HAVE_SYS_QUOTAS */
670 void dummy_sysquotas_c(void);
672 void dummy_sysquotas_c(void)
674 return;
676 #endif /* HAVE_SYS_QUOTAS */