Fix printf warnings in debug statments.
[nivanova/samba-autobuild/.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
78         ZERO_STRUCT(D);
79
80         if (!fsp||!fsp->conn||!qt)
81                 return (-1);
82
83         ZERO_STRUCT(*qt);
84
85         id.uid = -1;
86
87         if (psid && NT_STATUS_IS_ERR(sid_to_uid(psid, &id.uid))) {
88                 DEBUG(0,("sid_to_uid: failed, SID[%s]\n",
89                         sid_string_static(psid)));      
90         }
91
92         ret = SMB_VFS_GET_QUOTA(fsp->conn, qtype, id, &D);
93
94         if (psid)
95                 qt->sid    = *psid;
96
97         if (ret!=0) {
98                 return ret;
99         }
100                 
101         qt->usedspace = (SMB_BIG_UINT)D.curblocks*D.bsize;
102         qt->softlim = limit_unix2nt(D.softlimit, D.bsize);
103         qt->hardlim = limit_unix2nt(D.hardlimit, D.bsize);
104         qt->qflags = D.qflags;
105
106         
107         return 0;
108 }
109
110 int vfs_set_ntquota(files_struct *fsp, enum SMB_QUOTA_TYPE qtype, DOM_SID *psid, SMB_NTQUOTA_STRUCT *qt)
111 {
112         int ret;
113         SMB_DISK_QUOTA D;
114         unid_t id;
115         ZERO_STRUCT(D);
116
117         if (!fsp||!fsp->conn||!qt)
118                 return (-1);
119
120         id.uid = -1;
121
122         D.bsize     = (SMB_BIG_UINT)QUOTABLOCK_SIZE;
123
124         D.softlimit = limit_nt2unix(qt->softlim,D.bsize);
125         D.hardlimit = limit_nt2unix(qt->hardlim,D.bsize);
126         D.qflags     = qt->qflags;
127
128         D.isoftlimit = limit_blk2inodes(D.softlimit);
129         D.ihardlimit = limit_blk2inodes(D.hardlimit);
130
131         if (psid && NT_STATUS_IS_ERR(sid_to_uid(psid, &id.uid))) {
132                 DEBUG(0,("sid_to_uid: failed, SID[%s]\n",
133                         sid_string_static(psid)));      
134         }
135
136         ret = SMB_VFS_SET_QUOTA(fsp->conn, qtype, id, &D);
137         
138         return ret;
139 }
140
141 static BOOL allready_in_quota_list(SMB_NTQUOTA_LIST *qt_list, uid_t uid)
142 {
143         SMB_NTQUOTA_LIST *tmp_list = NULL;
144         
145         if (!qt_list)
146                 return False;
147
148         for (tmp_list=qt_list;tmp_list!=NULL;tmp_list=tmp_list->next) {
149                 if (tmp_list->uid == uid) {
150                         return True;    
151                 }
152         }
153
154         return False;
155 }
156
157 int vfs_get_user_ntquota_list(files_struct *fsp, SMB_NTQUOTA_LIST **qt_list)
158 {
159         struct passwd *usr;
160         TALLOC_CTX *mem_ctx = NULL;
161
162         if (!fsp||!fsp->conn||!qt_list)
163                 return (-1);
164
165         *qt_list = NULL;
166
167         if ((mem_ctx=talloc_init("SMB_USER_QUOTA_LIST"))==NULL) {
168                 DEBUG(0,("talloc_init() failed\n"));
169                 return (-1);
170         }
171
172         sys_setpwent();
173         while ((usr = sys_getpwent()) != NULL) {
174                 SMB_NTQUOTA_STRUCT tmp_qt;
175                 SMB_NTQUOTA_LIST *tmp_list_ent;
176                 DOM_SID sid;
177
178                 ZERO_STRUCT(tmp_qt);
179
180                 if (allready_in_quota_list((*qt_list),usr->pw_uid)) {
181                         DEBUG(5,("record for uid[%ld] allready in the list\n",(long)usr->pw_uid));
182                         continue;
183                 }
184
185                 if (NT_STATUS_IS_ERR(uid_to_sid(&sid, usr->pw_uid))) {
186                         DEBUG(0,("uid_to_sid failed for %ld\n",(long)usr->pw_uid));
187                         continue;
188                 }
189
190                 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &tmp_qt)!=0) {
191                         DEBUG(1,("no quota entry for sid[%s] path[%s]\n",
192                                 sid_string_static(&sid),fsp->conn->connectpath));
193                         continue;
194                 }
195
196                 DEBUG(15,("quota entry for id[%s] path[%s]\n",
197                         sid_string_static(&sid),fsp->conn->connectpath));
198
199                 if ((tmp_list_ent=(SMB_NTQUOTA_LIST *)talloc_zero(mem_ctx,sizeof(SMB_NTQUOTA_LIST)))==NULL) {
200                         DEBUG(0,("talloc_zero() failed\n"));
201                         *qt_list = NULL;
202                         talloc_destroy(mem_ctx);
203                         return (-1);
204                 }
205
206                 if ((tmp_list_ent->quotas=(SMB_NTQUOTA_STRUCT *)talloc_zero(mem_ctx,sizeof(SMB_NTQUOTA_STRUCT)))==NULL) {
207                         DEBUG(0,("talloc_zero() failed\n"));
208                         *qt_list = NULL;
209                         talloc_destroy(mem_ctx);
210                         return (-1);
211                 }
212
213                 tmp_list_ent->uid = usr->pw_uid;
214                 memcpy(tmp_list_ent->quotas,&tmp_qt,sizeof(tmp_qt));
215                 tmp_list_ent->mem_ctx = mem_ctx;
216
217                 DLIST_ADD((*qt_list),tmp_list_ent);
218                 
219         }
220         sys_endpwent();
221
222         return 0;
223 }
224
225 void *init_quota_handle(TALLOC_CTX *mem_ctx)
226 {
227         SMB_NTQUOTA_HANDLE *qt_handle;
228
229         if (!mem_ctx)
230                 return False;
231
232         qt_handle = (SMB_NTQUOTA_HANDLE *)talloc_zero(mem_ctx,sizeof(SMB_NTQUOTA_HANDLE));
233         if (qt_handle==NULL) {
234                 DEBUG(0,("talloc_zero() failed\n"));
235                 return NULL;
236         }
237
238         return (void *)qt_handle;       
239 }
240
241 void destroy_quota_handle(void **pqt_handle)
242 {
243         SMB_NTQUOTA_HANDLE *qt_handle = NULL;
244         if (!pqt_handle||!(*pqt_handle))
245                 return;
246         
247         qt_handle = (*pqt_handle);
248         
249         
250         if (qt_handle->quota_list)
251                 free_ntquota_list(&qt_handle->quota_list);
252
253         qt_handle->quota_list = NULL;
254         qt_handle->tmp_list = NULL;
255         qt_handle = NULL;
256
257         return;
258 }
259