2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan (metze) Metzmacher 2003
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.
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.
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/>.
21 #include "smbd/smbd.h"
22 #include "../lib/util/util_pw.h"
23 #include "system/passwd.h"
24 #include "passdb/lookup_sid.h"
27 #define DBGC_CLASS DBGC_QUOTA
29 static uint64_t limit_nt2unix(uint64_t in, uint64_t bsize)
31 uint64_t ret = (uint64_t)0;
33 ret = (uint64_t)(in/bsize);
35 /* we have to make sure that a overflow didn't set NO_LIMIT */
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;
49 static uint64_t limit_unix2nt(uint64_t in, uint64_t bsize)
51 uint64_t ret = (uint64_t)0;
53 ret = (uint64_t)(in*bsize);
57 ret = SMB_NTQUOTAS_NO_LIMIT;
60 if (in == SMB_QUOTAS_NO_LIMIT)
61 ret = SMB_NTQUOTAS_NO_LIMIT;
66 static uint64_t limit_blk2inodes(uint64_t in)
68 uint64_t ret = (uint64_t)0;
70 ret = (uint64_t)(in/2);
72 if (ret == 0 && in != 0)
78 int vfs_get_ntquota(files_struct *fsp, enum SMB_QUOTA_TYPE qtype, struct dom_sid *psid, SMB_NTQUOTA_STRUCT *qt)
86 if (!fsp||!fsp->conn||!qt)
93 if (psid && !sid_to_uid(psid, &id.uid)) {
94 DEBUG(0,("sid_to_uid: failed, SID[%s]\n",
95 sid_string_dbg(psid)));
98 ret = SMB_VFS_GET_QUOTA(fsp->conn, qtype, id, &D);
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;
116 int vfs_set_ntquota(files_struct *fsp, enum SMB_QUOTA_TYPE qtype, struct dom_sid *psid, SMB_NTQUOTA_STRUCT *qt)
123 if (!fsp||!fsp->conn||!qt)
128 D.bsize = (uint64_t)QUOTABLOCK_SIZE;
130 D.softlimit = limit_nt2unix(qt->softlim,D.bsize);
131 D.hardlimit = limit_nt2unix(qt->hardlim,D.bsize);
132 D.qflags = qt->qflags;
134 D.isoftlimit = limit_blk2inodes(D.softlimit);
135 D.ihardlimit = limit_blk2inodes(D.hardlimit);
137 if (psid && !sid_to_uid(psid, &id.uid)) {
138 DEBUG(0,("sid_to_uid: failed, SID[%s]\n",
139 sid_string_dbg(psid)));
142 ret = SMB_VFS_SET_QUOTA(fsp->conn, qtype, id, &D);
147 static bool allready_in_quota_list(SMB_NTQUOTA_LIST *qt_list, uid_t uid)
149 SMB_NTQUOTA_LIST *tmp_list = NULL;
154 for (tmp_list=qt_list;tmp_list!=NULL;tmp_list=tmp_list->next) {
155 if (tmp_list->uid == uid) {
163 int vfs_get_user_ntquota_list(files_struct *fsp, SMB_NTQUOTA_LIST **qt_list)
166 TALLOC_CTX *mem_ctx = NULL;
168 if (!fsp||!fsp->conn||!qt_list)
173 if ((mem_ctx=talloc_init("SMB_USER_QUOTA_LIST"))==NULL) {
174 DEBUG(0,("talloc_init() failed\n"));
179 while ((usr = sys_getpwent()) != NULL) {
180 SMB_NTQUOTA_STRUCT tmp_qt;
181 SMB_NTQUOTA_LIST *tmp_list_ent;
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));
191 uid_to_sid(&sid, usr->pw_uid);
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));
200 DEBUG(15,("quota entry for id[%s] path[%s]\n",
201 sid_string_dbg(&sid), fsp->conn->connectpath));
203 if ((tmp_list_ent=TALLOC_ZERO_P(mem_ctx,SMB_NTQUOTA_LIST))==NULL) {
204 DEBUG(0,("TALLOC_ZERO() failed\n"));
206 talloc_destroy(mem_ctx);
210 if ((tmp_list_ent->quotas=TALLOC_ZERO_P(mem_ctx,SMB_NTQUOTA_STRUCT))==NULL) {
211 DEBUG(0,("TALLOC_ZERO() failed\n"));
213 talloc_destroy(mem_ctx);
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;
221 DLIST_ADD((*qt_list),tmp_list_ent);
229 static int quota_handle_destructor(SMB_NTQUOTA_HANDLE *handle)
231 if (handle->quota_list)
232 free_ntquota_list(&handle->quota_list);
236 void *init_quota_handle(TALLOC_CTX *mem_ctx)
238 SMB_NTQUOTA_HANDLE *qt_handle;
243 qt_handle = TALLOC_ZERO_P(mem_ctx,SMB_NTQUOTA_HANDLE);
244 if (qt_handle==NULL) {
245 DEBUG(0,("TALLOC_ZERO() failed\n"));
249 talloc_set_destructor(qt_handle, quota_handle_destructor);
250 return (void *)qt_handle;