ntquotas: support "freeing" an empty quota list
[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 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 #include "includes.h"
21 #include "smbd/smbd.h"
22 #include "../lib/util/util_pw.h"
23 #include "system/passwd.h"
24 #include "passdb/lookup_sid.h"
25 #include "libsmb/libsmb.h"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_QUOTA
29
30 static uint64_t limit_nt2unix(uint64_t in, uint64_t bsize)
31 {
32         uint64_t ret = (uint64_t)0;
33
34         ret =   (uint64_t)(in/bsize);
35         if (in>0 && ret==0) {
36                 /* we have to make sure that a overflow didn't set NO_LIMIT */
37                 ret = (uint64_t)1;
38         }
39
40         if (in == SMB_NTQUOTAS_NO_LIMIT)
41                 ret = SMB_QUOTAS_NO_LIMIT;
42         else if (in == SMB_NTQUOTAS_NO_SPACE)
43                 ret = SMB_QUOTAS_NO_SPACE;
44         else if (in == SMB_NTQUOTAS_NO_ENTRY)
45                 ret = SMB_QUOTAS_NO_LIMIT;
46
47         return ret;
48 }
49
50 static uint64_t limit_unix2nt(uint64_t in, uint64_t bsize)
51 {
52         uint64_t ret = (uint64_t)0;
53
54         ret = (uint64_t)(in*bsize);
55         
56         return ret;
57 }
58
59 static uint64_t limit_blk2inodes(uint64_t in)
60 {
61         uint64_t ret = (uint64_t)0;
62         
63         ret = (uint64_t)(in/2);
64         
65         if (ret == 0 && in != 0)
66                 ret = (uint64_t)1;
67
68         return ret;     
69 }
70
71 NTSTATUS vfs_get_ntquota(files_struct *fsp, enum SMB_QUOTA_TYPE qtype,
72                          struct 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 NT_STATUS_INTERNAL_ERROR;
82         }
83
84         ZERO_STRUCT(*qt);
85
86         id.uid = -1;
87
88         if (psid && !sid_to_uid(psid, &id.uid)) {
89                 DEBUG(0,("sid_to_uid: failed, SID[%s]\n",
90                          sid_string_dbg(psid)));
91                 return NT_STATUS_NO_SUCH_USER;
92         }
93
94         ret = SMB_VFS_GET_QUOTA(fsp->conn, ".", qtype, id, &D);
95
96         if (psid)
97                 qt->sid    = *psid;
98
99         if (ret!=0) {
100                 return map_nt_error_from_unix(errno);
101         }
102                 
103         qt->usedspace = (uint64_t)D.curblocks*D.bsize;
104         qt->softlim = limit_unix2nt(D.softlimit, D.bsize);
105         qt->hardlim = limit_unix2nt(D.hardlimit, D.bsize);
106         qt->qflags = D.qflags;
107
108         return NT_STATUS_OK;
109 }
110
111 int vfs_set_ntquota(files_struct *fsp, enum SMB_QUOTA_TYPE qtype, struct dom_sid *psid, SMB_NTQUOTA_STRUCT *qt)
112 {
113         int ret;
114         SMB_DISK_QUOTA D;
115         unid_t id;
116         ZERO_STRUCT(D);
117
118         if (!fsp||!fsp->conn||!qt)
119                 return (-1);
120
121         id.uid = -1;
122
123         D.bsize     = (uint64_t)QUOTABLOCK_SIZE;
124
125         D.softlimit = limit_nt2unix(qt->softlim,D.bsize);
126         D.hardlimit = limit_nt2unix(qt->hardlim,D.bsize);
127         D.qflags     = qt->qflags;
128
129         D.isoftlimit = limit_blk2inodes(D.softlimit);
130         D.ihardlimit = limit_blk2inodes(D.hardlimit);
131
132         if (psid && !sid_to_uid(psid, &id.uid)) {
133                 DEBUG(0,("sid_to_uid: failed, SID[%s]\n",
134                          sid_string_dbg(psid)));
135         }
136
137         ret = SMB_VFS_SET_QUOTA(fsp->conn, qtype, id, &D);
138         
139         return ret;
140 }
141
142 static bool allready_in_quota_list(SMB_NTQUOTA_LIST *qt_list, uid_t uid)
143 {
144         SMB_NTQUOTA_LIST *tmp_list = NULL;
145         
146         if (!qt_list)
147                 return False;
148
149         for (tmp_list=qt_list;tmp_list!=NULL;tmp_list=tmp_list->next) {
150                 if (tmp_list->uid == uid) {
151                         return True;    
152                 }
153         }
154
155         return False;
156 }
157
158 int vfs_get_user_ntquota_list(files_struct *fsp, SMB_NTQUOTA_LIST **qt_list)
159 {
160         struct passwd *usr;
161         TALLOC_CTX *mem_ctx = NULL;
162
163         if (!fsp||!fsp->conn||!qt_list)
164                 return (-1);
165
166         *qt_list = NULL;
167
168         if ((mem_ctx=talloc_init("SMB_USER_QUOTA_LIST"))==NULL) {
169                 DEBUG(0,("talloc_init() failed\n"));
170                 return (-1);
171         }
172
173         setpwent();
174         while ((usr = getpwent()) != NULL) {
175                 SMB_NTQUOTA_STRUCT tmp_qt;
176                 SMB_NTQUOTA_LIST *tmp_list_ent;
177                 struct dom_sid  sid;
178                 NTSTATUS status;
179
180                 ZERO_STRUCT(tmp_qt);
181
182                 if (allready_in_quota_list((*qt_list),usr->pw_uid)) {
183                         DEBUG(5,("record for uid[%ld] allready in the list\n",(long)usr->pw_uid));
184                         continue;
185                 }
186
187                 uid_to_sid(&sid, usr->pw_uid);
188
189                 status =
190                     vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &tmp_qt);
191                 if (!NT_STATUS_IS_OK(status)) {
192                         DEBUG(5, ("failed getting quota for uid[%ld] - %s\n",
193                                   (long)usr->pw_uid, nt_errstr(status)));
194                         continue;
195                 }
196                 if (tmp_qt.softlim == 0 && tmp_qt.hardlim == 0) {
197                         DEBUG(5,("no quota entry for sid[%s] path[%s]\n",
198                                  sid_string_dbg(&sid),
199                                  fsp->conn->connectpath));
200                         continue;
201                 }
202
203                 DEBUG(15,("quota entry for id[%s] path[%s]\n",
204                           sid_string_dbg(&sid), fsp->conn->connectpath));
205
206                 if ((tmp_list_ent=talloc_zero(mem_ctx,SMB_NTQUOTA_LIST))==NULL) {
207                         DEBUG(0,("TALLOC_ZERO() failed\n"));
208                         *qt_list = NULL;
209                         talloc_destroy(mem_ctx);
210                         return (-1);
211                 }
212
213                 if ((tmp_list_ent->quotas=talloc_zero(mem_ctx,SMB_NTQUOTA_STRUCT))==NULL) {
214                         DEBUG(0,("TALLOC_ZERO() failed\n"));
215                         *qt_list = NULL;
216                         talloc_destroy(mem_ctx);
217                         return (-1);
218                 }
219
220                 tmp_list_ent->uid = usr->pw_uid;
221                 memcpy(tmp_list_ent->quotas,&tmp_qt,sizeof(tmp_qt));
222                 tmp_list_ent->mem_ctx = mem_ctx;
223
224                 DLIST_ADD((*qt_list),tmp_list_ent);
225                 
226         }
227         endpwent();
228
229         return 0;
230 }
231
232 static int quota_handle_destructor(SMB_NTQUOTA_HANDLE *handle)
233 {
234         free_ntquota_list(&handle->quota_list);
235         return 0;
236 }
237
238 void *init_quota_handle(TALLOC_CTX *mem_ctx)
239 {
240         SMB_NTQUOTA_HANDLE *qt_handle;
241
242         if (!mem_ctx)
243                 return NULL;
244
245         qt_handle = talloc_zero(mem_ctx,SMB_NTQUOTA_HANDLE);
246         if (qt_handle==NULL) {
247                 DEBUG(0,("TALLOC_ZERO() failed\n"));
248                 return NULL;
249         }
250
251         talloc_set_destructor(qt_handle, quota_handle_destructor);
252         return (void *)qt_handle;
253 }