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