nwrap: Don't leak memory from gethostbyname*() functions
[obnox/samba/samba-obnox.git] / source3 / lib / sysquotas_xfs.c
1 /* 
2    Unix SMB/CIFS implementation.
3    System QUOTA function wrappers for XFS
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 #if defined(HAVE_SYS_QUOTAS) && defined(HAVE_XFS_QUOTAS)
27
28 #ifdef HAVE_SYS_QUOTA_H
29 #include <sys/quota.h> 
30 #endif
31
32 /* this one should actually come from glibc: */
33 /* #include "samba_linux_quota.h" */
34
35 #ifdef HAVE_XFS_XQM_H
36 #include <xfs/xqm.h>
37 #endif
38
39 #define HAVE_GROUP_QUOTA
40
41 /* on IRIX */
42 #ifndef Q_XQUOTAON
43 #define Q_XQUOTAON Q_QUOTAON
44 #endif /* Q_XQUOTAON */
45 #ifndef Q_XQUOTAOFF
46 #define Q_XQUOTAOFF Q_QUOTAOFF
47 #endif /* Q_XQUOTAOFF */
48 #ifndef Q_XGETQSTAT
49 #define Q_XGETQSTAT Q_GETQSTAT
50 #endif /* Q_XGETQSTAT */
51
52 /* currently doesn't support Group and Project quotas on IRIX 
53  */
54
55 #ifndef QCMD
56 #define QCMD(x,y) x
57 #endif
58
59 /*
60  * IRIX has BBSIZE in <sys/param.h>
61  */
62 #ifndef BBSHIFT
63 #define BBSHIFT         9
64 #endif /* BBSHIFT */
65 #ifndef BBSIZE
66 #define BBSIZE          (1<<BBSHIFT)
67 #endif /* BBSIZE */
68
69 /****************************************************************************
70  Abstract out the XFS Quota Manager quota get call.
71 ****************************************************************************/
72 int sys_get_xfs_quota(const char *path, const char *bdev, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
73 {
74         int ret = -1;
75         uint32_t qflags = 0;
76         uint64_t bsize = (uint64_t)BBSIZE;
77         struct fs_disk_quota D;
78         struct fs_quota_stat F;
79         ZERO_STRUCT(D);
80         ZERO_STRUCT(F);
81
82         if (!bdev||!dp)
83                 smb_panic("sys_get_xfs_quota: called with NULL pointer");
84                 
85         ZERO_STRUCT(*dp);
86         dp->qtype = qtype;
87                 
88         switch (qtype) {
89                 case SMB_USER_QUOTA_TYPE:
90                         DEBUG(10,("sys_get_xfs_quota: path[%s] bdev[%s] SMB_USER_QUOTA_TYPE uid[%u]\n",
91                                 path, bdev, (unsigned)id.uid));
92
93                         if ((ret=quotactl(QCMD(Q_XGETQUOTA,USRQUOTA), bdev, id.uid, (caddr_t)&D)))
94                                 return ret;
95                         break;
96 #ifdef HAVE_GROUP_QUOTA
97                 case SMB_GROUP_QUOTA_TYPE:
98                         DEBUG(10,("sys_get_xfs_quota: path[%s] bdev[%s] SMB_GROUP_QUOTA_TYPE gid[%u]\n",
99                                 path, bdev, (unsigned)id.gid));
100
101                         if ((ret=quotactl(QCMD(Q_XGETQUOTA,GRPQUOTA), bdev, id.gid, (caddr_t)&D)))
102                                 return ret;
103                         break;
104 #endif /* HAVE_GROUP_QUOTA */
105                 case SMB_USER_FS_QUOTA_TYPE:
106                         DEBUG(10,("sys_get_xfs_quota: path[%s] bdev[%s] SMB_USER_FS_QUOTA_TYPE (uid[%u])\n",
107                                 path, bdev, (unsigned)id.uid));
108
109                         quotactl(QCMD(Q_XGETQSTAT,USRQUOTA), bdev, -1, (caddr_t)&F);
110
111                         if (F.qs_flags & XFS_QUOTA_UDQ_ENFD) {
112                                 qflags |= QUOTAS_DENY_DISK;
113                         }
114                         else if (F.qs_flags & XFS_QUOTA_UDQ_ACCT) {
115                                 qflags |= QUOTAS_ENABLED;
116                         }
117
118                         ret = 0;
119
120                         break;
121 #ifdef HAVE_GROUP_QUOTA
122                 case SMB_GROUP_FS_QUOTA_TYPE:
123                         DEBUG(10,("sys_get_xfs_quota: path[%s] bdev[%s] SMB_GROUP_FS_QUOTA_TYPE (gid[%u])\n",
124                                 path, bdev, (unsigned)id.gid));
125
126                         quotactl(QCMD(Q_XGETQSTAT,GRPQUOTA), bdev, -1, (caddr_t)&F);
127
128                         if (F.qs_flags & XFS_QUOTA_GDQ_ENFD) {
129                                 qflags |= QUOTAS_DENY_DISK;
130                         }
131                         else if (F.qs_flags & XFS_QUOTA_GDQ_ACCT) {
132                                 qflags |= QUOTAS_ENABLED;
133                         }
134
135                         ret = 0;
136
137                         break;
138 #endif /* HAVE_GROUP_QUOTA */
139                 default:
140                         errno = ENOSYS;
141                         return -1;
142         }
143
144         dp->bsize = bsize;
145         dp->softlimit = (uint64_t)D.d_blk_softlimit;
146         dp->hardlimit = (uint64_t)D.d_blk_hardlimit;
147         dp->ihardlimit = (uint64_t)D.d_ino_hardlimit;
148         dp->isoftlimit = (uint64_t)D.d_ino_softlimit;
149         dp->curinodes = (uint64_t)D.d_icount;
150         dp->curblocks = (uint64_t)D.d_bcount;
151         dp->qflags = qflags;
152
153         return ret;
154 }
155
156 /****************************************************************************
157  Abstract out the XFS Quota Manager quota set call.
158 ****************************************************************************/
159 int sys_set_xfs_quota(const char *path, const char *bdev, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
160 {
161         int ret = -1;
162         uint32_t qflags = 0;
163         uint64_t bsize = (uint64_t)BBSIZE;
164         struct fs_disk_quota D;
165         struct fs_quota_stat F;
166         int q_on = 0;
167         int q_off = 0;
168         ZERO_STRUCT(D);
169         ZERO_STRUCT(F);
170
171         if (!bdev||!dp)
172                 smb_panic("sys_set_xfs_quota: called with NULL pointer");
173         
174         if (bsize == dp->bsize) {
175                 D.d_blk_softlimit = dp->softlimit;
176                 D.d_blk_hardlimit = dp->hardlimit;
177                 D.d_ino_hardlimit = dp->ihardlimit;
178                 D.d_ino_softlimit = dp->isoftlimit;
179         } else {
180                 D.d_blk_softlimit = (dp->softlimit*dp->bsize)/bsize;
181                 D.d_blk_hardlimit = (dp->hardlimit*dp->bsize)/bsize;
182                 D.d_ino_hardlimit = (dp->ihardlimit*dp->bsize)/bsize;
183                 D.d_ino_softlimit = (dp->isoftlimit*dp->bsize)/bsize;           
184         }
185
186         qflags = dp->qflags;
187
188         switch (qtype) {
189                 case SMB_USER_QUOTA_TYPE:
190                         DEBUG(10,("sys_set_xfs_quota: path[%s] bdev[%s] SMB_USER_QUOTA_TYPE uid[%u]\n",
191                                 path, bdev, (unsigned)id.uid));
192
193                         D.d_fieldmask |= FS_DQ_LIMIT_MASK;
194                         ret = quotactl(QCMD(Q_XSETQLIM,USRQUOTA), bdev, id.uid, (caddr_t)&D);
195                         break;
196 #ifdef HAVE_GROUP_QUOTA
197                 case SMB_GROUP_QUOTA_TYPE:
198                         DEBUG(10,("sys_set_xfs_quota: path[%s] bdev[%s] SMB_GROUP_QUOTA_TYPE gid[%u]\n",
199                                 path, bdev, (unsigned)id.gid));
200
201                         D.d_fieldmask |= FS_DQ_LIMIT_MASK;
202                         ret = quotactl(QCMD(Q_XSETQLIM,GRPQUOTA), bdev, id.gid, (caddr_t)&D);
203                         break;
204 #endif /* HAVE_GROUP_QUOTA */
205                 case SMB_USER_FS_QUOTA_TYPE:
206                         DEBUG(10,("sys_set_xfs_quota: path[%s] bdev[%s] SMB_USER_FS_QUOTA_TYPE (uid[%u])\n",
207                                 path, bdev, (unsigned)id.uid));
208
209                         quotactl(QCMD(Q_XGETQSTAT,USRQUOTA), bdev, -1, (caddr_t)&F);
210                         
211                         if (qflags & QUOTAS_DENY_DISK) {
212                                 if (!(F.qs_flags & XFS_QUOTA_UDQ_ENFD))
213                                         q_on |= XFS_QUOTA_UDQ_ENFD;
214                                 if (!(F.qs_flags & XFS_QUOTA_UDQ_ACCT))
215                                         q_on |= XFS_QUOTA_UDQ_ACCT;
216                                 
217                                 if (q_on != 0) {
218                                         ret = quotactl(QCMD(Q_XQUOTAON,USRQUOTA),bdev, -1, (caddr_t)&q_on);
219                                 } else {
220                                         ret = 0;
221                                 }
222
223                         } else if (qflags & QUOTAS_ENABLED) {
224                                 if (F.qs_flags & XFS_QUOTA_UDQ_ENFD)
225                                         q_off |= XFS_QUOTA_UDQ_ENFD;
226
227                                 if (q_off != 0) {
228                                         ret = quotactl(QCMD(Q_XQUOTAOFF,USRQUOTA),bdev, -1, (caddr_t)&q_off);
229                                 } else {
230                                         ret = 0;
231                                 }
232
233                                 if (!(F.qs_flags & XFS_QUOTA_UDQ_ACCT))
234                                         q_on |= XFS_QUOTA_UDQ_ACCT;
235
236                                 if (q_on != 0) {
237                                         ret = quotactl(QCMD(Q_XQUOTAON,USRQUOTA),bdev, -1, (caddr_t)&q_on);
238                                 } else {
239                                         ret = 0;
240                                 }
241                         } else {
242 #if 0
243                         /* Switch on XFS_QUOTA_UDQ_ACCT didn't work!
244                          * only swittching off XFS_QUOTA_UDQ_ACCT work
245                          */
246                                 if (F.qs_flags & XFS_QUOTA_UDQ_ENFD)
247                                         q_off |= XFS_QUOTA_UDQ_ENFD;
248                                 if (F.qs_flags & XFS_QUOTA_UDQ_ACCT)
249                                         q_off |= XFS_QUOTA_UDQ_ACCT;
250
251                                 if (q_off !=0) {
252                                         ret = quotactl(QCMD(Q_XQUOTAOFF,USRQUOTA),bdev, -1, (caddr_t)&q_off);
253                                 } else {
254                                         ret = 0;
255                                 }
256 #else
257                                 ret = -1;
258 #endif
259                         }
260
261                         break;
262 #ifdef HAVE_GROUP_QUOTA
263                 case SMB_GROUP_FS_QUOTA_TYPE:
264                         DEBUG(10,("sys_set_xfs_quota: path[%s] bdev[%s] SMB_GROUP_FS_QUOTA_TYPE (gid[%u])\n",
265                                 path, bdev, (unsigned)id.gid));
266
267                         quotactl(QCMD(Q_XGETQSTAT,GRPQUOTA), bdev, -1, (caddr_t)&F);
268                         
269                         if (qflags & QUOTAS_DENY_DISK) {
270                                 if (!(F.qs_flags & XFS_QUOTA_GDQ_ENFD))
271                                         q_on |= XFS_QUOTA_GDQ_ENFD;
272                                 if (!(F.qs_flags & XFS_QUOTA_GDQ_ACCT))
273                                         q_on |= XFS_QUOTA_GDQ_ACCT;
274                                 
275                                 if (q_on != 0) {
276                                         ret = quotactl(QCMD(Q_XQUOTAON,GRPQUOTA),bdev, -1, (caddr_t)&q_on);
277                                 } else {
278                                         ret = 0;
279                                 }
280
281                         } else if (qflags & QUOTAS_ENABLED) {
282                                 if (F.qs_flags & XFS_QUOTA_GDQ_ENFD)
283                                         q_off |= XFS_QUOTA_GDQ_ENFD;
284
285                                 if (q_off != 0) {
286                                         ret = quotactl(QCMD(Q_XQUOTAOFF,GRPQUOTA),bdev, -1, (caddr_t)&q_off);
287                                 } else {
288                                         ret = 0;
289                                 }
290
291                                 if (!(F.qs_flags & XFS_QUOTA_GDQ_ACCT))
292                                         q_on |= XFS_QUOTA_GDQ_ACCT;
293
294                                 if (q_on != 0) {
295                                         ret = quotactl(QCMD(Q_XQUOTAON,GRPQUOTA),bdev, -1, (caddr_t)&q_on);
296                                 } else {
297                                         ret = 0;
298                                 }
299                         } else {
300 #if 0
301                         /* Switch on XFS_QUOTA_UDQ_ACCT didn't work!
302                          * only swittching off XFS_QUOTA_UDQ_ACCT work
303                          */
304                                 if (F.qs_flags & XFS_QUOTA_GDQ_ENFD)
305                                         q_off |= XFS_QUOTA_GDQ_ENFD;
306                                 if (F.qs_flags & XFS_QUOTA_GDQ_ACCT)
307                                         q_off |= XFS_QUOTA_GDQ_ACCT;
308
309                                 if (q_off !=0) {
310                                         ret = quotactl(QCMD(Q_XQUOTAOFF,GRPQUOTA),bdev, -1, (caddr_t)&q_off);
311                                 } else {
312                                         ret = 0;
313                                 }
314 #else
315                                 ret = -1;
316 #endif
317                         }
318
319                         break;
320 #endif /* HAVE_GROUP_QUOTA */
321                 default:
322                         errno = ENOSYS;
323                         return -1;
324         }
325
326         return ret;
327 }
328
329 #else /* HAVE_XFS_QUOTAS */
330  void dummy_sysquotas_xfs(void);
331
332  void dummy_sysquotas_xfs(void){}
333 #endif /* HAVE_XFS_QUOTAS */