s3-passdb: use passdb headers where needed.
[sfrench/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 "../lib/util/util_pw.h"
22 #include "system/passwd.h"
23 #include "passdb/lookup_sid.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_QUOTA
27
28 static uint64_t limit_nt2unix(uint64_t in, uint64_t bsize)
29 {
30         uint64_t ret = (uint64_t)0;
31
32         ret =   (uint64_t)(in/bsize);
33         if (in>0 && ret==0) {
34                 /* we have to make sure that a overflow didn't set NO_LIMIT */
35                 ret = (uint64_t)1;
36         }
37
38         if (in == SMB_NTQUOTAS_NO_LIMIT)
39                 ret = SMB_QUOTAS_NO_LIMIT;
40         else if (in == SMB_NTQUOTAS_NO_SPACE)
41                 ret = SMB_QUOTAS_NO_SPACE;
42         else if (in == SMB_NTQUOTAS_NO_ENTRY)
43                 ret = SMB_QUOTAS_NO_LIMIT;
44
45         return ret;
46 }
47
48 static uint64_t limit_unix2nt(uint64_t in, uint64_t bsize)
49 {
50         uint64_t ret = (uint64_t)0;
51
52         ret = (uint64_t)(in*bsize);
53         
54         if (ret < in) {
55                 /* we overflow */
56                 ret = SMB_NTQUOTAS_NO_LIMIT;
57         }
58
59         if (in == SMB_QUOTAS_NO_LIMIT)
60                 ret = SMB_NTQUOTAS_NO_LIMIT;
61
62         return ret;
63 }
64
65 static uint64_t limit_blk2inodes(uint64_t in)
66 {
67         uint64_t ret = (uint64_t)0;
68         
69         ret = (uint64_t)(in/2);
70         
71         if (ret == 0 && in != 0)
72                 ret = (uint64_t)1;
73
74         return ret;     
75 }
76
77 int vfs_get_ntquota(files_struct *fsp, enum SMB_QUOTA_TYPE qtype, struct dom_sid *psid, SMB_NTQUOTA_STRUCT *qt)
78 {
79         int ret;
80         SMB_DISK_QUOTA D;
81         unid_t id;
82
83         ZERO_STRUCT(D);
84
85         if (!fsp||!fsp->conn||!qt)
86                 return (-1);
87
88         ZERO_STRUCT(*qt);
89
90         id.uid = -1;
91
92         if (psid && !sid_to_uid(psid, &id.uid)) {
93                 DEBUG(0,("sid_to_uid: failed, SID[%s]\n",
94                          sid_string_dbg(psid)));
95         }
96
97         ret = SMB_VFS_GET_QUOTA(fsp->conn, qtype, id, &D);
98
99         if (psid)
100                 qt->sid    = *psid;
101
102         if (ret!=0) {
103                 return ret;
104         }
105                 
106         qt->usedspace = (uint64_t)D.curblocks*D.bsize;
107         qt->softlim = limit_unix2nt(D.softlimit, D.bsize);
108         qt->hardlim = limit_unix2nt(D.hardlimit, D.bsize);
109         qt->qflags = D.qflags;
110
111         
112         return 0;
113 }
114
115 int vfs_set_ntquota(files_struct *fsp, enum SMB_QUOTA_TYPE qtype, struct dom_sid *psid, SMB_NTQUOTA_STRUCT *qt)
116 {
117         int ret;
118         SMB_DISK_QUOTA D;
119         unid_t id;
120         ZERO_STRUCT(D);
121
122         if (!fsp||!fsp->conn||!qt)
123                 return (-1);
124
125         id.uid = -1;
126
127         D.bsize     = (uint64_t)QUOTABLOCK_SIZE;
128
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)) {
137                 DEBUG(0,("sid_to_uid: failed, SID[%s]\n",
138                          sid_string_dbg(psid)));
139         }
140
141         ret = SMB_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                 struct 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[%ld] allready in the list\n",(long)usr->pw_uid));
187                         continue;
188                 }
189
190                 uid_to_sid(&sid, usr->pw_uid);
191
192                 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &tmp_qt)!=0) {
193                         DEBUG(5,("no quota entry for sid[%s] path[%s]\n",
194                                  sid_string_dbg(&sid),
195                                  fsp->conn->connectpath));
196                         continue;
197                 }
198
199                 DEBUG(15,("quota entry for id[%s] path[%s]\n",
200                           sid_string_dbg(&sid), fsp->conn->connectpath));
201
202                 if ((tmp_list_ent=TALLOC_ZERO_P(mem_ctx,SMB_NTQUOTA_LIST))==NULL) {
203                         DEBUG(0,("TALLOC_ZERO() failed\n"));
204                         *qt_list = NULL;
205                         talloc_destroy(mem_ctx);
206                         return (-1);
207                 }
208
209                 if ((tmp_list_ent->quotas=TALLOC_ZERO_P(mem_ctx,SMB_NTQUOTA_STRUCT))==NULL) {
210                         DEBUG(0,("TALLOC_ZERO() failed\n"));
211                         *qt_list = NULL;
212                         talloc_destroy(mem_ctx);
213                         return (-1);
214                 }
215
216                 tmp_list_ent->uid = usr->pw_uid;
217                 memcpy(tmp_list_ent->quotas,&tmp_qt,sizeof(tmp_qt));
218                 tmp_list_ent->mem_ctx = mem_ctx;
219
220                 DLIST_ADD((*qt_list),tmp_list_ent);
221                 
222         }
223         sys_endpwent();
224
225         return 0;
226 }
227
228 static int quota_handle_destructor(SMB_NTQUOTA_HANDLE *handle)
229 {
230         if (handle->quota_list)
231                 free_ntquota_list(&handle->quota_list);
232         return 0;
233 }
234
235 void *init_quota_handle(TALLOC_CTX *mem_ctx)
236 {
237         SMB_NTQUOTA_HANDLE *qt_handle;
238
239         if (!mem_ctx)
240                 return False;
241
242         qt_handle = TALLOC_ZERO_P(mem_ctx,SMB_NTQUOTA_HANDLE);
243         if (qt_handle==NULL) {
244                 DEBUG(0,("TALLOC_ZERO() failed\n"));
245                 return NULL;
246         }
247
248         talloc_set_destructor(qt_handle, quota_handle_destructor);
249         return (void *)qt_handle;
250 }