Add two flags to allow for handling of Extended Signatures (Session Key Protection...
[Samba/gebeck_regimport.git] / source3 / lib / sysquotas.c
blob08f8444e2f1de4ff5829098ce159bdc38125fe6b
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"
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_QUOTA
26 #ifdef HAVE_SYS_QUOTAS
28 #if defined(HAVE_QUOTACTL_4A)
30 /*#endif HAVE_QUOTACTL_4A */
31 #elif defined(HAVE_QUOTACTL_4B)
33 #error HAVE_QUOTACTL_4B not implemeted
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 #ifdef HAVE_MNTENT
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;
52 SMB_DEV_T devno;
54 /* find the block device file */
56 if (!path||!mntpath||!bdev||!fs)
57 smb_panic("sys_path_to_bdev: called with NULL pointer");
59 (*mntpath) = NULL;
60 (*bdev) = NULL;
61 (*fs) = NULL;
63 if ( sys_stat(path, &S, false) == -1 )
64 return (-1);
66 devno = S.st_ex_dev ;
68 fp = setmntent(MOUNTED,"r");
69 if (fp == NULL) {
70 return -1;
73 while ((mnt = getmntent(fp))) {
74 if ( sys_stat(mnt->mnt_dir, &S, false) == -1 )
75 continue ;
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;
90 break;
94 endmntent(fp) ;
96 return ret;
98 /* #endif HAVE_MNTENT */
99 #elif defined(HAVE_DEVNM)
101 /* we have this on HPUX, ... */
102 static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
104 int ret = -1;
105 char dev_disk[256];
106 SMB_STRUCT_STAT S;
108 if (!path||!mntpath||!bdev||!fs)
109 smb_panic("sys_path_to_bdev: called with NULL pointer");
111 (*mntpath) = NULL;
112 (*bdev) = NULL;
113 (*fs) = NULL;
115 /* find the block device file */
117 if ((ret=sys_stat(path, &S, false))!=0) {
118 return ret;
121 if ((ret=devnm(S_IFBLK, S.st_ex_dev, dev_disk, 256, 1))!=0) {
122 return ret;
125 /* we should get the mntpath right...
126 * but I don't know how
127 * --metze
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;
140 return ret;
143 /* #endif HAVE_DEVNM */
144 #else
145 /* we should fake this up...*/
146 static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
148 int ret = -1;
150 if (!path||!mntpath||!bdev||!fs)
151 smb_panic("sys_path_to_bdev: called with NULL pointer");
153 (*mntpath) = NULL;
154 (*bdev) = NULL;
155 (*fs) = NULL;
157 (*mntpath) = SMB_STRDUP(path);
158 if (*mntpath) {
159 ret = 0;
160 } else {
161 SAFE_FREE(*mntpath);
162 ret = -1;
165 return ret;
167 #endif
169 /*********************************************************************
170 Now the list of all filesystem specific quota systems we have found
171 **********************************************************************/
172 static 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 #endif /* HAVE_XFS_QUOTAS */
180 #ifdef HAVE_NFS_QUOTAS
181 {"nfs", sys_get_nfs_quota, sys_set_nfs_quota},
182 #endif /* HAVE_NFS_QUOTAS */
183 {NULL, NULL, NULL}
186 static int command_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
188 const char *get_quota_command;
189 char **lines = NULL;
191 get_quota_command = lp_get_quota_command(talloc_tos());
192 if (get_quota_command && *get_quota_command) {
193 const char *p;
194 char *p2;
195 char *syscmd = NULL;
196 int _id = -1;
198 switch(qtype) {
199 case SMB_USER_QUOTA_TYPE:
200 case SMB_USER_FS_QUOTA_TYPE:
201 _id = id.uid;
202 break;
203 case SMB_GROUP_QUOTA_TYPE:
204 case SMB_GROUP_FS_QUOTA_TYPE:
205 _id = id.gid;
206 break;
207 default:
208 DEBUG(0,("invalid quota type.\n"));
209 return -1;
212 if (asprintf(&syscmd, "%s \"%s\" %d %d",
213 get_quota_command, path, qtype, _id) < 0) {
214 return -1;
217 DEBUG (3, ("get_quota: Running command %s\n", syscmd));
219 lines = file_lines_pload(syscmd, NULL);
220 SAFE_FREE(syscmd);
222 if (lines) {
223 char *line = lines[0];
225 DEBUG (3, ("Read output from get_quota, \"%s\"\n", line));
227 /* we need to deal with long long unsigned here, if supported */
229 dp->qflags = (enum SMB_QUOTA_TYPE)strtoul(line, &p2, 10);
230 p = p2;
231 while (p && *p && isspace(*p)) {
232 p++;
235 if (p && *p) {
236 dp->curblocks = STR_TO_SMB_BIG_UINT(p, &p);
237 } else {
238 goto invalid_param;
241 while (p && *p && isspace(*p)) {
242 p++;
245 if (p && *p) {
246 dp->softlimit = STR_TO_SMB_BIG_UINT(p, &p);
247 } else {
248 goto invalid_param;
251 while (p && *p && isspace(*p)) {
252 p++;
255 if (p && *p) {
256 dp->hardlimit = STR_TO_SMB_BIG_UINT(p, &p);
257 } else {
258 goto invalid_param;
261 while (p && *p && isspace(*p)) {
262 p++;
265 if (p && *p) {
266 dp->curinodes = STR_TO_SMB_BIG_UINT(p, &p);
267 } else {
268 goto invalid_param;
271 while (p && *p && isspace(*p)) {
272 p++;
275 if (p && *p) {
276 dp->isoftlimit = STR_TO_SMB_BIG_UINT(p, &p);
277 } else {
278 goto invalid_param;
281 while (p && *p && isspace(*p)) {
282 p++;
285 if (p && *p) {
286 dp->ihardlimit = STR_TO_SMB_BIG_UINT(p, &p);
287 } else {
288 goto invalid_param;
291 while (p && *p && isspace(*p)) {
292 p++;
295 if (p && *p) {
296 dp->bsize = STR_TO_SMB_BIG_UINT(p, NULL);
297 } else {
298 dp->bsize = 1024;
301 TALLOC_FREE(lines);
302 lines = NULL;
304 DEBUG (3, ("Parsed output of get_quota, ...\n"));
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 return 0;
317 DEBUG (0, ("get_quota_command failed!\n"));
318 return -1;
321 errno = ENOSYS;
322 return -1;
324 invalid_param:
326 TALLOC_FREE(lines);
327 DEBUG(0,("The output of get_quota_command is invalid!\n"));
328 return -1;
331 static int command_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
333 const char *set_quota_command;
335 set_quota_command = lp_set_quota_command(talloc_tos());
336 if (set_quota_command && *set_quota_command) {
337 char **lines = NULL;
338 char *syscmd = NULL;
339 int _id = -1;
341 switch(qtype) {
342 case SMB_USER_QUOTA_TYPE:
343 case SMB_USER_FS_QUOTA_TYPE:
344 _id = id.uid;
345 break;
346 case SMB_GROUP_QUOTA_TYPE:
347 case SMB_GROUP_FS_QUOTA_TYPE:
348 _id = id.gid;
349 break;
350 default:
351 return -1;
354 if (asprintf(&syscmd,
355 "%s \"%s\" %d %d "
356 "%u %llu %llu "
357 "%llu %llu %llu ",
358 set_quota_command, path, qtype, _id, dp->qflags,
359 (long long unsigned)dp->softlimit,(long long unsigned)dp->hardlimit,
360 (long long unsigned)dp->isoftlimit,(long long unsigned)dp->ihardlimit,
361 (long long unsigned)dp->bsize) < 0) {
362 return -1;
365 DEBUG (3, ("get_quota: Running command %s\n", syscmd));
367 lines = file_lines_pload(syscmd, NULL);
368 SAFE_FREE(syscmd);
369 if (lines) {
370 char *line = lines[0];
372 DEBUG (3, ("Read output from set_quota, \"%s\"\n", line));
374 TALLOC_FREE(lines);
376 return 0;
378 DEBUG (0, ("set_quota_command failed!\n"));
379 return -1;
382 errno = ENOSYS;
383 return -1;
386 int sys_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
388 int ret = -1;
389 int i;
390 bool ready = False;
391 char *mntpath = NULL;
392 char *bdev = NULL;
393 char *fs = NULL;
395 if (!path||!dp)
396 smb_panic("sys_get_quota: called with NULL pointer");
398 if (command_get_quota(path, qtype, id, dp)==0) {
399 return 0;
400 } else if (errno != ENOSYS) {
401 return -1;
404 if ((ret=sys_path_to_bdev(path,&mntpath,&bdev,&fs))!=0) {
405 DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path));
406 return ret;
409 errno = 0;
410 DEBUG(10,("sys_get_quota() uid(%u, %u)\n", (unsigned)getuid(), (unsigned)geteuid()));
412 for (i=0;(fs && sys_quota_backends[i].name && sys_quota_backends[i].get_quota);i++) {
413 if (strcmp(fs,sys_quota_backends[i].name)==0) {
414 ret = sys_quota_backends[i].get_quota(mntpath, bdev, qtype, id, dp);
415 if (ret!=0) {
416 DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
417 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
418 } else {
419 DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
420 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
422 ready = True;
423 break;
427 if (!ready) {
428 /* use the default vfs quota functions */
429 ret=sys_get_vfs_quota(mntpath, bdev, qtype, id, dp);
430 if (ret!=0) {
431 DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s\n",
432 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
433 } else {
434 DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
435 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
439 SAFE_FREE(mntpath);
440 SAFE_FREE(bdev);
441 SAFE_FREE(fs);
443 if ((ret!=0)&& (errno == EDQUOT)) {
444 DEBUG(10,("sys_get_quota() warning over quota!\n"));
445 return 0;
448 return ret;
451 int sys_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
453 int ret = -1;
454 int i;
455 bool ready = False;
456 char *mntpath = NULL;
457 char *bdev = NULL;
458 char *fs = NULL;
460 /* find the block device file */
462 if (!path||!dp)
463 smb_panic("get_smb_quota: called with NULL pointer");
465 if (command_set_quota(path, qtype, id, dp)==0) {
466 return 0;
467 } else if (errno != ENOSYS) {
468 return -1;
471 if ((ret=sys_path_to_bdev(path,&mntpath,&bdev,&fs))!=0) {
472 DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path));
473 return ret;
476 errno = 0;
477 DEBUG(10,("sys_set_quota() uid(%u, %u)\n", (unsigned)getuid(), (unsigned)geteuid()));
479 for (i=0;(fs && sys_quota_backends[i].name && sys_quota_backends[i].set_quota);i++) {
480 if (strcmp(fs,sys_quota_backends[i].name)==0) {
481 ret = sys_quota_backends[i].set_quota(mntpath, bdev, qtype, id, dp);
482 if (ret!=0) {
483 DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
484 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
485 } else {
486 DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
487 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
489 ready = True;
490 break;
494 if (!ready) {
495 /* use the default vfs quota functions */
496 ret=sys_set_vfs_quota(mntpath, bdev, qtype, id, dp);
497 if (ret!=0) {
498 DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
499 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
500 } else {
501 DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
502 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
506 SAFE_FREE(mntpath);
507 SAFE_FREE(bdev);
508 SAFE_FREE(fs);
510 if ((ret!=0)&& (errno == EDQUOT)) {
511 DEBUG(10,("sys_set_quota() warning over quota!\n"));
512 return 0;
515 return ret;
518 #else /* HAVE_SYS_QUOTAS */
519 void dummy_sysquotas_c(void);
521 void dummy_sysquotas_c(void)
523 return;
525 #endif /* HAVE_SYS_QUOTAS */