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"
25 #include "libsmb/libsmb.h"
28 #define DBGC_CLASS DBGC_QUOTA
30 static uint64_t limit_nt2unix(uint64_t in, uint64_t bsize)
32 uint64_t ret = (uint64_t)0;
34 ret = (uint64_t)(in/bsize);
36 /* we have to make sure that a overflow didn't set NO_LIMIT */
40 if (in == SMB_NTQUOTAS_NO_LIMIT)
41 ret = SMB_QUOTAS_NO_LIMIT;
42 else if (in == SMB_NTQUOTAS_NO_SPACE)
43 ret = SMB_QUOTAS_NO_SPACE;
44 else if (in == SMB_NTQUOTAS_NO_ENTRY)
45 ret = SMB_QUOTAS_NO_LIMIT;
50 static uint64_t limit_unix2nt(uint64_t in, uint64_t bsize)
52 uint64_t ret = (uint64_t)0;
54 ret = (uint64_t)(in*bsize);
58 ret = SMB_NTQUOTAS_NO_LIMIT;
61 if (in == SMB_QUOTAS_NO_LIMIT)
62 ret = SMB_NTQUOTAS_NO_LIMIT;
67 static uint64_t limit_blk2inodes(uint64_t in)
69 uint64_t ret = (uint64_t)0;
71 ret = (uint64_t)(in/2);
73 if (ret == 0 && in != 0)
79 NTSTATUS vfs_get_ntquota(files_struct *fsp, enum SMB_QUOTA_TYPE qtype,
80 struct dom_sid *psid, SMB_NTQUOTA_STRUCT *qt)
88 if (!fsp || !fsp->conn || !qt) {
89 return NT_STATUS_INTERNAL_ERROR;
96 if (psid && !sid_to_uid(psid, &id.uid)) {
97 DEBUG(0,("sid_to_uid: failed, SID[%s]\n",
98 sid_string_dbg(psid)));
99 return NT_STATUS_NO_SUCH_USER;
102 ret = SMB_VFS_GET_QUOTA(fsp->conn, ".", qtype, id, &D);
108 return map_nt_error_from_unix(errno);
111 qt->usedspace = (uint64_t)D.curblocks*D.bsize;
112 qt->softlim = limit_unix2nt(D.softlimit, D.bsize);
113 qt->hardlim = limit_unix2nt(D.hardlimit, D.bsize);
114 qt->qflags = D.qflags;
119 int vfs_set_ntquota(files_struct *fsp, enum SMB_QUOTA_TYPE qtype, struct dom_sid *psid, SMB_NTQUOTA_STRUCT *qt)
126 if (!fsp||!fsp->conn||!qt)
131 D.bsize = (uint64_t)QUOTABLOCK_SIZE;
133 D.softlimit = limit_nt2unix(qt->softlim,D.bsize);
134 D.hardlimit = limit_nt2unix(qt->hardlim,D.bsize);
135 D.qflags = qt->qflags;
137 D.isoftlimit = limit_blk2inodes(D.softlimit);
138 D.ihardlimit = limit_blk2inodes(D.hardlimit);
140 if (psid && !sid_to_uid(psid, &id.uid)) {
141 DEBUG(0,("sid_to_uid: failed, SID[%s]\n",
142 sid_string_dbg(psid)));
145 ret = SMB_VFS_SET_QUOTA(fsp->conn, qtype, id, &D);
150 static bool allready_in_quota_list(SMB_NTQUOTA_LIST *qt_list, uid_t uid)
152 SMB_NTQUOTA_LIST *tmp_list = NULL;
157 for (tmp_list=qt_list;tmp_list!=NULL;tmp_list=tmp_list->next) {
158 if (tmp_list->uid == uid) {
166 int vfs_get_user_ntquota_list(files_struct *fsp, SMB_NTQUOTA_LIST **qt_list)
169 TALLOC_CTX *mem_ctx = NULL;
171 if (!fsp||!fsp->conn||!qt_list)
176 if ((mem_ctx=talloc_init("SMB_USER_QUOTA_LIST"))==NULL) {
177 DEBUG(0,("talloc_init() failed\n"));
182 while ((usr = getpwent()) != NULL) {
183 SMB_NTQUOTA_STRUCT tmp_qt;
184 SMB_NTQUOTA_LIST *tmp_list_ent;
190 if (allready_in_quota_list((*qt_list),usr->pw_uid)) {
191 DEBUG(5,("record for uid[%ld] allready in the list\n",(long)usr->pw_uid));
195 uid_to_sid(&sid, usr->pw_uid);
198 vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &tmp_qt);
199 if (!NT_STATUS_IS_OK(status)) {
200 DEBUG(5,("no quota entry for sid[%s] path[%s]\n",
201 sid_string_dbg(&sid),
202 fsp->conn->connectpath));
206 DEBUG(15,("quota entry for id[%s] path[%s]\n",
207 sid_string_dbg(&sid), fsp->conn->connectpath));
209 if ((tmp_list_ent=talloc_zero(mem_ctx,SMB_NTQUOTA_LIST))==NULL) {
210 DEBUG(0,("TALLOC_ZERO() failed\n"));
212 talloc_destroy(mem_ctx);
216 if ((tmp_list_ent->quotas=talloc_zero(mem_ctx,SMB_NTQUOTA_STRUCT))==NULL) {
217 DEBUG(0,("TALLOC_ZERO() failed\n"));
219 talloc_destroy(mem_ctx);
223 tmp_list_ent->uid = usr->pw_uid;
224 memcpy(tmp_list_ent->quotas,&tmp_qt,sizeof(tmp_qt));
225 tmp_list_ent->mem_ctx = mem_ctx;
227 DLIST_ADD((*qt_list),tmp_list_ent);
235 static int quota_handle_destructor(SMB_NTQUOTA_HANDLE *handle)
237 if (handle->quota_list)
238 free_ntquota_list(&handle->quota_list);
242 void *init_quota_handle(TALLOC_CTX *mem_ctx)
244 SMB_NTQUOTA_HANDLE *qt_handle;
249 qt_handle = talloc_zero(mem_ctx,SMB_NTQUOTA_HANDLE);
250 if (qt_handle==NULL) {
251 DEBUG(0,("TALLOC_ZERO() failed\n"));
255 talloc_set_destructor(qt_handle, quota_handle_destructor);
256 return (void *)qt_handle;