6abafbd768c3542a40d24fd29456d3a156c11659
[amitay/samba.git] / source3 / lib / sysquotas.c
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
46 static 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, ... */
102 static 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...*/
146 static 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 **********************************************************************/
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}
184 };
185
186 static int command_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
187 {
188         const char *get_quota_command;
189         char **lines = NULL;
190
191         get_quota_command = lp_get_quota_command();
192         if (get_quota_command && *get_quota_command) {
193                 const char *p;
194                 char *p2;
195                 char *syscmd = NULL;
196                 int _id = -1;
197
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;
210                 }
211
212                 if (asprintf(&syscmd, "%s \"%s\" %d %d",
213                         get_quota_command, path, qtype, _id) < 0) {
214                         return -1;
215                 }
216
217                 DEBUG (3, ("get_quota: Running command %s\n", syscmd));
218
219                 lines = file_lines_pload(syscmd, NULL);
220                 SAFE_FREE(syscmd);
221
222                 if (lines) {
223                         char *line = lines[0];
224
225                         DEBUG (3, ("Read output from get_quota, \"%s\"\n", line));
226
227                         /* we need to deal with long long unsigned here, if supported */
228
229                         dp->qflags = (enum SMB_QUOTA_TYPE)strtoul(line, &p2, 10);
230                         p = p2;
231                         while (p && *p && isspace(*p)) {
232                                 p++;
233                         }
234
235                         if (p && *p) {
236                                 dp->curblocks = STR_TO_SMB_BIG_UINT(p, &p);
237                         } else {
238                                 goto invalid_param;
239                         }
240
241                         while (p && *p && isspace(*p)) {
242                                 p++;
243                         }
244
245                         if (p && *p) {
246                                 dp->softlimit = STR_TO_SMB_BIG_UINT(p, &p);
247                         } else {
248                                 goto invalid_param;
249                         }
250
251                         while (p && *p && isspace(*p)) {
252                                 p++;
253                         }
254
255                         if (p && *p) {
256                                 dp->hardlimit = STR_TO_SMB_BIG_UINT(p, &p);
257                         } else {
258                                 goto invalid_param;
259                         }
260
261                         while (p && *p && isspace(*p)) {
262                                 p++;
263                         }
264
265                         if (p && *p) {
266                                 dp->curinodes = STR_TO_SMB_BIG_UINT(p, &p);
267                         } else {
268                                 goto invalid_param;
269                         }
270
271                         while (p && *p && isspace(*p)) {
272                                 p++;
273                         }
274
275                         if (p && *p) {
276                                 dp->isoftlimit = STR_TO_SMB_BIG_UINT(p, &p);
277                         } else {
278                                 goto invalid_param;
279                         }
280
281                         while (p && *p && isspace(*p)) {
282                                 p++;
283                         }
284
285                         if (p && *p) {
286                                 dp->ihardlimit = STR_TO_SMB_BIG_UINT(p, &p);
287                         } else {
288                                 goto invalid_param;     
289                         }
290
291                         while (p && *p && isspace(*p)) {
292                                 p++;
293                         }
294
295                         if (p && *p) {
296                                 dp->bsize = STR_TO_SMB_BIG_UINT(p, NULL);
297                         } else {
298                                 dp->bsize = 1024;
299                         }
300
301                         TALLOC_FREE(lines);
302                         lines = NULL;
303
304                         DEBUG (3, ("Parsed output of get_quota, ...\n"));
305
306 #ifdef LARGE_SMB_OFF_T
307                         DEBUGADD (5,( 
308                                 "qflags:%u curblocks:%llu softlimit:%llu hardlimit:%llu\n"
309                                 "curinodes:%llu isoftlimit:%llu ihardlimit:%llu bsize:%llu\n", 
310                                 dp->qflags,(long long unsigned)dp->curblocks,
311                                 (long long unsigned)dp->softlimit,(long long unsigned)dp->hardlimit,
312                                 (long long unsigned)dp->curinodes,
313                                 (long long unsigned)dp->isoftlimit,(long long unsigned)dp->ihardlimit,
314                                 (long long unsigned)dp->bsize));
315 #else /* LARGE_SMB_OFF_T */
316                         DEBUGADD (5,( 
317                                 "qflags:%u curblocks:%lu softlimit:%lu hardlimit:%lu\n"
318                                 "curinodes:%lu isoftlimit:%lu ihardlimit:%lu bsize:%lu\n", 
319                                 dp->qflags,(long unsigned)dp->curblocks,
320                                 (long unsigned)dp->softlimit,(long unsigned)dp->hardlimit,
321                                 (long unsigned)dp->curinodes,
322                                 (long unsigned)dp->isoftlimit,(long unsigned)dp->ihardlimit,
323                                 (long unsigned)dp->bsize));
324 #endif /* LARGE_SMB_OFF_T */
325                         return 0;
326                 }
327
328                 DEBUG (0, ("get_quota_command failed!\n"));
329                 return -1;
330         }
331
332         errno = ENOSYS;
333         return -1;
334
335 invalid_param:
336
337         TALLOC_FREE(lines);
338         DEBUG(0,("The output of get_quota_command is invalid!\n"));
339         return -1;
340 }
341
342 static int command_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
343 {
344         const char *set_quota_command;
345
346         set_quota_command = lp_set_quota_command();
347         if (set_quota_command && *set_quota_command) {
348                 char **lines = NULL;
349                 char *syscmd = NULL;
350                 int _id = -1;
351
352                 switch(qtype) {
353                         case SMB_USER_QUOTA_TYPE:
354                         case SMB_USER_FS_QUOTA_TYPE:
355                                 _id = id.uid;
356                                 break;
357                         case SMB_GROUP_QUOTA_TYPE:
358                         case SMB_GROUP_FS_QUOTA_TYPE:
359                                 _id = id.gid;
360                                 break;
361                         default:
362                                 return -1;
363                 }
364
365 #ifdef LARGE_SMB_OFF_T
366                 if (asprintf(&syscmd,
367                         "%s \"%s\" %d %d "
368                         "%u %llu %llu "
369                         "%llu %llu %llu ",
370                         set_quota_command, path, qtype, _id, dp->qflags,
371                         (long long unsigned)dp->softlimit,(long long unsigned)dp->hardlimit,
372                         (long long unsigned)dp->isoftlimit,(long long unsigned)dp->ihardlimit,
373                         (long long unsigned)dp->bsize) < 0) {
374                         return -1;
375                 }
376 #else /* LARGE_SMB_OFF_T */
377                 if (asprintf(&syscmd,
378                         "%s \"%s\" %d %d "
379                         "%u %lu %lu "
380                         "%lu %lu %lu ",
381                         set_quota_command, path, qtype, _id, dp->qflags,
382                         (long unsigned)dp->softlimit,(long unsigned)dp->hardlimit,
383                         (long unsigned)dp->isoftlimit,(long unsigned)dp->ihardlimit,
384                         (long unsigned)dp->bsize) < 0) {
385                         return -1;
386                 }
387 #endif /* LARGE_SMB_OFF_T */
388
389                 DEBUG (3, ("get_quota: Running command %s\n", syscmd));
390
391                 lines = file_lines_pload(syscmd, NULL);
392                 SAFE_FREE(syscmd);
393                 if (lines) {
394                         char *line = lines[0];
395
396                         DEBUG (3, ("Read output from set_quota, \"%s\"\n", line));
397
398                         TALLOC_FREE(lines);
399
400                         return 0;
401                 }
402                 DEBUG (0, ("set_quota_command failed!\n"));
403                 return -1;
404         }
405
406         errno = ENOSYS;
407         return -1;
408 }
409
410 int sys_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
411 {
412         int ret = -1;
413         int i;
414         bool ready = False;
415         char *mntpath = NULL;
416         char *bdev = NULL;
417         char *fs = NULL;
418
419         if (!path||!dp)
420                 smb_panic("sys_get_quota: called with NULL pointer");
421
422         if (command_get_quota(path, qtype, id, dp)==0) {        
423                 return 0;
424         } else if (errno != ENOSYS) {
425                 return -1;
426         }
427
428         if ((ret=sys_path_to_bdev(path,&mntpath,&bdev,&fs))!=0) {
429                 DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path));
430                 return ret;
431         }
432
433         errno = 0;
434         DEBUG(10,("sys_get_quota() uid(%u, %u)\n", (unsigned)getuid(), (unsigned)geteuid()));
435
436         for (i=0;(fs && sys_quota_backends[i].name && sys_quota_backends[i].get_quota);i++) {
437                 if (strcmp(fs,sys_quota_backends[i].name)==0) {
438                         ret = sys_quota_backends[i].get_quota(mntpath, bdev, qtype, id, dp);
439                         if (ret!=0) {
440                                 DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
441                                         fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
442                         } else {
443                                 DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
444                                         fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
445                         }
446                         ready = True;
447                         break;  
448                 }               
449         }
450
451         if (!ready) {
452                 /* use the default vfs quota functions */
453                 ret=sys_get_vfs_quota(mntpath, bdev, qtype, id, dp);
454                 if (ret!=0) {
455                         DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s\n",
456                                 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
457                 } else {
458                         DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
459                                 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
460                 }
461         }
462
463         SAFE_FREE(mntpath);
464         SAFE_FREE(bdev);
465         SAFE_FREE(fs);
466
467         if ((ret!=0)&& (errno == EDQUOT)) {
468                 DEBUG(10,("sys_get_quota() warning over quota!\n"));
469                 return 0;
470         }
471
472         return ret;
473 }
474
475 int sys_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
476 {
477         int ret = -1;
478         int i;
479         bool ready = False;
480         char *mntpath = NULL;
481         char *bdev = NULL;
482         char *fs = NULL;
483
484         /* find the block device file */
485
486         if (!path||!dp)
487                 smb_panic("get_smb_quota: called with NULL pointer");
488
489         if (command_set_quota(path, qtype, id, dp)==0) {        
490                 return 0;
491         } else if (errno != ENOSYS) {
492                 return -1;
493         }
494
495         if ((ret=sys_path_to_bdev(path,&mntpath,&bdev,&fs))!=0) {
496                 DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path));
497                 return ret;
498         }
499
500         errno = 0;
501         DEBUG(10,("sys_set_quota() uid(%u, %u)\n", (unsigned)getuid(), (unsigned)geteuid())); 
502
503         for (i=0;(fs && sys_quota_backends[i].name && sys_quota_backends[i].set_quota);i++) {
504                 if (strcmp(fs,sys_quota_backends[i].name)==0) {
505                         ret = sys_quota_backends[i].set_quota(mntpath, bdev, qtype, id, dp);
506                         if (ret!=0) {
507                                 DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
508                                         fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
509                         } else {
510                                 DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
511                                         fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
512                         }
513                         ready = True;
514                         break;
515                 }               
516         }
517
518         if (!ready) {
519                 /* use the default vfs quota functions */
520                 ret=sys_set_vfs_quota(mntpath, bdev, qtype, id, dp);
521                 if (ret!=0) {
522                         DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
523                                 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
524                 } else {
525                         DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
526                                 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
527                 }
528         }
529
530         SAFE_FREE(mntpath);
531         SAFE_FREE(bdev);
532         SAFE_FREE(fs);
533
534         if ((ret!=0)&& (errno == EDQUOT)) {
535                 DEBUG(10,("sys_set_quota() warning over quota!\n"));
536                 return 0;
537         }
538
539         return ret;             
540 }
541
542 #else /* HAVE_SYS_QUOTAS */
543  void dummy_sysquotas_c(void);
544
545  void dummy_sysquotas_c(void)
546 {
547         return;
548 }
549 #endif /* HAVE_SYS_QUOTAS */
550