Fix a wrong define check.
[ira/wip.git] / source3 / smbd / ntquotas.c
1 /* 
2    Unix SMB/CIFS implementation.
3    NT QUOTA suppport
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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 static SMB_BIG_UINT limit_nt2unix(SMB_BIG_UINT in, SMB_BIG_UINT bsize)
24 {
25         SMB_BIG_UINT ret = (SMB_BIG_UINT)0;
26
27         ret =   (SMB_BIG_UINT)(in/bsize);
28         if (in>0 && ret==0) {
29                 /* we have to make sure that a overflow didn't set NO_LIMIT */
30                 ret = (SMB_BIG_UINT)1;
31         }
32
33         if (in == SMB_NTQUOTAS_NO_LIMIT)
34                 ret = SMB_QUOTAS_NO_LIMIT;
35         else if (in == SMB_NTQUOTAS_NO_SPACE)
36                 ret = SMB_QUOTAS_NO_SPACE;
37         else if (in == SMB_NTQUOTAS_NO_ENTRY)
38                 ret = SMB_QUOTAS_NO_LIMIT;
39
40         return ret;
41 }
42
43 static SMB_BIG_UINT limit_unix2nt(SMB_BIG_UINT in, SMB_BIG_UINT bsize)
44 {
45         SMB_BIG_UINT ret = (SMB_BIG_UINT)0;
46
47         ret = (SMB_BIG_UINT)(in*bsize);
48         
49         if (ret < in) {
50                 /* we overflow */
51                 ret = SMB_NTQUOTAS_NO_LIMIT;
52         }
53
54         if (in == SMB_QUOTAS_NO_LIMIT)
55                 ret = SMB_NTQUOTAS_NO_LIMIT;
56
57         return ret;
58 }
59
60 static SMB_BIG_UINT limit_blk2inodes(SMB_BIG_UINT in)
61 {
62         SMB_BIG_UINT ret = (SMB_BIG_UINT)0;
63         
64         ret = (SMB_BIG_UINT)(in/2);
65         
66         if (ret == 0 && in != 0)
67                 ret = (SMB_BIG_UINT)1;
68
69         return ret;     
70 }
71
72 int vfs_get_ntquota(files_struct *fsp, enum SMB_QUOTA_TYPE qtype, DOM_SID *psid, SMB_NTQUOTA_STRUCT *qt)
73 {
74         int ret;
75         SMB_DISK_QUOTA D;
76         unid_t id;
77         enum SID_NAME_USE sid_use = SID_NAME_USE_NONE;
78
79         ZERO_STRUCT(D);
80
81         if (!fsp||!fsp->conn||!qt)
82                 return (-1);
83
84         ZERO_STRUCT(*qt);
85
86         id.uid = -1;
87
88         if (psid && !sid_to_uid(psid, &id.uid, &sid_use)) {
89                 DEBUG(0,("sid_to_uid: failed, SID[%s]\n",
90                         sid_string_static(psid)));      
91         }
92
93         ret = VFS_GET_QUOTA(fsp->conn, qtype, id, &D);
94
95         if (psid)
96                 qt->sid    = *psid;
97
98         if (ret!=0) {
99                 return ret;
100         }
101                 
102         qt->usedspace = (SMB_BIG_UINT)D.curblocks*D.bsize;
103         qt->softlim = limit_unix2nt(D.softlimit, D.bsize);
104         qt->hardlim = limit_unix2nt(D.hardlimit, D.bsize);
105         qt->qflags = D.qflags;
106
107         
108         return 0;
109 }
110
111 int vfs_set_ntquota(files_struct *fsp, enum SMB_QUOTA_TYPE qtype, DOM_SID *psid, SMB_NTQUOTA_STRUCT *qt)
112 {
113         int ret;
114         SMB_DISK_QUOTA D;
115         unid_t id;
116         enum SID_NAME_USE sid_use = SID_NAME_USE_NONE;
117         ZERO_STRUCT(D);
118
119         if (!fsp||!fsp->conn||!qt)
120                 return (-1);
121
122         id.uid = -1;
123
124 #if defined(QUOTABLOCK_SIZE)
125         D.bsize     = (SMB_BIG_UINT)QUOTABLOCK_SIZE;
126 #else
127         D.bsize     = (SMB_BIG_UINT)1024;
128 #endif
129         D.softlimit = limit_nt2unix(qt->softlim,D.bsize);
130         D.hardlimit = limit_nt2unix(qt->hardlim,D.bsize);
131         D.qflags     = qt->qflags;
132
133         D.isoftlimit = limit_blk2inodes(D.softlimit);
134         D.ihardlimit = limit_blk2inodes(D.hardlimit);
135
136         if (psid && !sid_to_uid(psid, &id.uid, &sid_use)) {
137                 DEBUG(0,("sid_to_uid: failed, SID[%s]\n",
138                         sid_string_static(psid)));      
139         }
140
141         ret = VFS_SET_QUOTA(fsp->conn, qtype, id, &D);
142         
143         return ret;
144 }
145
146 static BOOL allready_in_quota_list(SMB_NTQUOTA_LIST *qt_list, uid_t uid)
147 {
148         SMB_NTQUOTA_LIST *tmp_list = NULL;
149         
150         if (!qt_list)
151                 return False;
152
153         for (tmp_list=qt_list;tmp_list!=NULL;tmp_list=tmp_list->next) {
154                 if (tmp_list->uid == uid) {
155                         return True;    
156                 }
157         }
158
159         return False;
160 }
161
162 int vfs_get_user_ntquota_list(files_struct *fsp, SMB_NTQUOTA_LIST **qt_list)
163 {
164         struct passwd *usr;
165         TALLOC_CTX *mem_ctx = NULL;
166
167         if (!fsp||!fsp->conn||!qt_list)
168                 return (-1);
169
170         *qt_list = NULL;
171
172         if ((mem_ctx=talloc_init("SMB_USER_QUOTA_LIST"))==NULL) {
173                 DEBUG(0,("talloc_init() failed\n"));
174                 return (-1);
175         }
176
177         sys_setpwent();
178         while ((usr = sys_getpwent()) != NULL) {
179                 SMB_NTQUOTA_STRUCT tmp_qt;
180                 SMB_NTQUOTA_LIST *tmp_list_ent;
181                 DOM_SID sid;
182
183                 ZERO_STRUCT(tmp_qt);
184
185                 if (allready_in_quota_list((*qt_list),usr->pw_uid)) {
186                         DEBUG(5,("record for uid[%d] allready in the list\n",usr->pw_uid));
187                         continue;
188                 }
189
190                 if (uid_to_sid(&sid,usr->pw_uid)==NULL) {
191                         DEBUG(0,("uid_to_sid failed for %d\n",usr->pw_uid));
192                         continue;
193                 }
194
195                 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &tmp_qt)!=0) {
196                         DEBUG(1,("no quota entry for sid[%s] path[%s]\n",
197                                 sid_string_static(&sid),fsp->conn->connectpath));
198                         continue;
199                 }
200
201                 DEBUG(15,("quota entry for id[%s] path[%s]\n",
202                         sid_string_static(&sid),fsp->conn->connectpath));
203
204                 if ((tmp_list_ent=(SMB_NTQUOTA_LIST *)talloc_zero(mem_ctx,sizeof(SMB_NTQUOTA_LIST)))==NULL) {
205                         DEBUG(0,("talloc_zero() failed\n"));
206                         *qt_list = NULL;
207                         talloc_destroy(mem_ctx);
208                         return (-1);
209                 }
210
211                 if ((tmp_list_ent->quotas=(SMB_NTQUOTA_STRUCT *)talloc_zero(mem_ctx,sizeof(SMB_NTQUOTA_STRUCT)))==NULL) {
212                         DEBUG(0,("talloc_zero() failed\n"));
213                         *qt_list = NULL;
214                         talloc_destroy(mem_ctx);
215                         return (-1);
216                 }
217
218                 tmp_list_ent->uid = usr->pw_uid;
219                 memcpy(tmp_list_ent->quotas,&tmp_qt,sizeof(tmp_qt));
220                 tmp_list_ent->mem_ctx = mem_ctx;
221
222                 DLIST_ADD((*qt_list),tmp_list_ent);
223                 
224         }
225         sys_endpwent();
226
227         return 0;
228 }
229
230 void *init_quota_handle(TALLOC_CTX *mem_ctx)
231 {
232         SMB_NTQUOTA_HANDLE *qt_handle;
233
234         if (!mem_ctx)
235                 return False;
236
237         qt_handle = (SMB_NTQUOTA_HANDLE *)talloc_zero(mem_ctx,sizeof(SMB_NTQUOTA_HANDLE));
238         if (qt_handle==NULL) {
239                 DEBUG(0,("talloc_zero() failed\n"));
240                 return NULL;
241         }
242
243         return (void *)qt_handle;       
244 }
245
246 void destroy_quota_handle(void **pqt_handle)
247 {
248         SMB_NTQUOTA_HANDLE *qt_handle = NULL;
249         if (!pqt_handle||!(*pqt_handle))
250                 return;
251         
252         qt_handle = (*pqt_handle);
253         
254         
255         if (qt_handle->quota_list)
256                 free_ntquota_list(&qt_handle->quota_list);
257
258         qt_handle->quota_list = NULL;
259         qt_handle->tmp_list = NULL;
260         qt_handle = NULL;
261
262         return;
263 }
264