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