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