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 "../lib/util/util_pw.h"
22 #include "system/passwd.h"
23 #include "passdb/lookup_sid.h"
26 #define DBGC_CLASS DBGC_QUOTA
28 static uint64_t limit_nt2unix(uint64_t in, uint64_t bsize)
30 uint64_t ret = (uint64_t)0;
32 ret = (uint64_t)(in/bsize);
34 /* we have to make sure that a overflow didn't set NO_LIMIT */
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;
48 static uint64_t limit_unix2nt(uint64_t in, uint64_t bsize)
50 uint64_t ret = (uint64_t)0;
52 ret = (uint64_t)(in*bsize);
56 ret = SMB_NTQUOTAS_NO_LIMIT;
59 if (in == SMB_QUOTAS_NO_LIMIT)
60 ret = SMB_NTQUOTAS_NO_LIMIT;
65 static uint64_t limit_blk2inodes(uint64_t in)
67 uint64_t ret = (uint64_t)0;
69 ret = (uint64_t)(in/2);
71 if (ret == 0 && in != 0)
77 int vfs_get_ntquota(files_struct *fsp, enum SMB_QUOTA_TYPE qtype, struct dom_sid *psid, SMB_NTQUOTA_STRUCT *qt)
85 if (!fsp||!fsp->conn||!qt)
92 if (psid && !sid_to_uid(psid, &id.uid)) {
93 DEBUG(0,("sid_to_uid: failed, SID[%s]\n",
94 sid_string_dbg(psid)));
97 ret = SMB_VFS_GET_QUOTA(fsp->conn, qtype, id, &D);
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;
115 int vfs_set_ntquota(files_struct *fsp, enum SMB_QUOTA_TYPE qtype, struct dom_sid *psid, SMB_NTQUOTA_STRUCT *qt)
122 if (!fsp||!fsp->conn||!qt)
127 D.bsize = (uint64_t)QUOTABLOCK_SIZE;
129 D.softlimit = limit_nt2unix(qt->softlim,D.bsize);
130 D.hardlimit = limit_nt2unix(qt->hardlim,D.bsize);
131 D.qflags = qt->qflags;
133 D.isoftlimit = limit_blk2inodes(D.softlimit);
134 D.ihardlimit = limit_blk2inodes(D.hardlimit);
136 if (psid && !sid_to_uid(psid, &id.uid)) {
137 DEBUG(0,("sid_to_uid: failed, SID[%s]\n",
138 sid_string_dbg(psid)));
141 ret = SMB_VFS_SET_QUOTA(fsp->conn, qtype, id, &D);
146 static bool allready_in_quota_list(SMB_NTQUOTA_LIST *qt_list, uid_t uid)
148 SMB_NTQUOTA_LIST *tmp_list = NULL;
153 for (tmp_list=qt_list;tmp_list!=NULL;tmp_list=tmp_list->next) {
154 if (tmp_list->uid == uid) {
162 int vfs_get_user_ntquota_list(files_struct *fsp, SMB_NTQUOTA_LIST **qt_list)
165 TALLOC_CTX *mem_ctx = NULL;
167 if (!fsp||!fsp->conn||!qt_list)
172 if ((mem_ctx=talloc_init("SMB_USER_QUOTA_LIST"))==NULL) {
173 DEBUG(0,("talloc_init() failed\n"));
178 while ((usr = sys_getpwent()) != NULL) {
179 SMB_NTQUOTA_STRUCT tmp_qt;
180 SMB_NTQUOTA_LIST *tmp_list_ent;
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));
190 uid_to_sid(&sid, usr->pw_uid);
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));
199 DEBUG(15,("quota entry for id[%s] path[%s]\n",
200 sid_string_dbg(&sid), fsp->conn->connectpath));
202 if ((tmp_list_ent=TALLOC_ZERO_P(mem_ctx,SMB_NTQUOTA_LIST))==NULL) {
203 DEBUG(0,("TALLOC_ZERO() failed\n"));
205 talloc_destroy(mem_ctx);
209 if ((tmp_list_ent->quotas=TALLOC_ZERO_P(mem_ctx,SMB_NTQUOTA_STRUCT))==NULL) {
210 DEBUG(0,("TALLOC_ZERO() failed\n"));
212 talloc_destroy(mem_ctx);
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;
220 DLIST_ADD((*qt_list),tmp_list_ent);
228 static int quota_handle_destructor(SMB_NTQUOTA_HANDLE *handle)
230 if (handle->quota_list)
231 free_ntquota_list(&handle->quota_list);
235 void *init_quota_handle(TALLOC_CTX *mem_ctx)
237 SMB_NTQUOTA_HANDLE *qt_handle;
242 qt_handle = TALLOC_ZERO_P(mem_ctx,SMB_NTQUOTA_HANDLE);
243 if (qt_handle==NULL) {
244 DEBUG(0,("TALLOC_ZERO() failed\n"));
248 talloc_set_destructor(qt_handle, quota_handle_destructor);
249 return (void *)qt_handle;