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