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"
25 #define DBGC_CLASS DBGC_QUOTA
27 static uint64_t limit_nt2unix(uint64_t in
, uint64_t bsize
)
29 uint64_t ret
= (uint64_t)0;
31 ret
= (uint64_t)(in
/bsize
);
33 /* we have to make sure that a overflow didn't set NO_LIMIT */
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
;
47 static uint64_t limit_unix2nt(uint64_t in
, uint64_t bsize
)
49 uint64_t ret
= (uint64_t)0;
51 ret
= (uint64_t)(in
*bsize
);
55 ret
= SMB_NTQUOTAS_NO_LIMIT
;
58 if (in
== SMB_QUOTAS_NO_LIMIT
)
59 ret
= SMB_NTQUOTAS_NO_LIMIT
;
64 static uint64_t limit_blk2inodes(uint64_t in
)
66 uint64_t ret
= (uint64_t)0;
68 ret
= (uint64_t)(in
/2);
70 if (ret
== 0 && in
!= 0)
76 int vfs_get_ntquota(files_struct
*fsp
, enum SMB_QUOTA_TYPE qtype
, struct dom_sid
*psid
, SMB_NTQUOTA_STRUCT
*qt
)
84 if (!fsp
||!fsp
->conn
||!qt
)
91 if (psid
&& !sid_to_uid(psid
, &id
.uid
)) {
92 DEBUG(0,("sid_to_uid: failed, SID[%s]\n",
93 sid_string_dbg(psid
)));
96 ret
= SMB_VFS_GET_QUOTA(fsp
->conn
, qtype
, id
, &D
);
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
;
114 int vfs_set_ntquota(files_struct
*fsp
, enum SMB_QUOTA_TYPE qtype
, struct dom_sid
*psid
, SMB_NTQUOTA_STRUCT
*qt
)
121 if (!fsp
||!fsp
->conn
||!qt
)
126 D
.bsize
= (uint64_t)QUOTABLOCK_SIZE
;
128 D
.softlimit
= limit_nt2unix(qt
->softlim
,D
.bsize
);
129 D
.hardlimit
= limit_nt2unix(qt
->hardlim
,D
.bsize
);
130 D
.qflags
= qt
->qflags
;
132 D
.isoftlimit
= limit_blk2inodes(D
.softlimit
);
133 D
.ihardlimit
= limit_blk2inodes(D
.hardlimit
);
135 if (psid
&& !sid_to_uid(psid
, &id
.uid
)) {
136 DEBUG(0,("sid_to_uid: failed, SID[%s]\n",
137 sid_string_dbg(psid
)));
140 ret
= SMB_VFS_SET_QUOTA(fsp
->conn
, qtype
, id
, &D
);
145 static bool allready_in_quota_list(SMB_NTQUOTA_LIST
*qt_list
, uid_t uid
)
147 SMB_NTQUOTA_LIST
*tmp_list
= NULL
;
152 for (tmp_list
=qt_list
;tmp_list
!=NULL
;tmp_list
=tmp_list
->next
) {
153 if (tmp_list
->uid
== uid
) {
161 int vfs_get_user_ntquota_list(files_struct
*fsp
, SMB_NTQUOTA_LIST
**qt_list
)
164 TALLOC_CTX
*mem_ctx
= NULL
;
166 if (!fsp
||!fsp
->conn
||!qt_list
)
171 if ((mem_ctx
=talloc_init("SMB_USER_QUOTA_LIST"))==NULL
) {
172 DEBUG(0,("talloc_init() failed\n"));
177 while ((usr
= sys_getpwent()) != NULL
) {
178 SMB_NTQUOTA_STRUCT tmp_qt
;
179 SMB_NTQUOTA_LIST
*tmp_list_ent
;
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
));
189 uid_to_sid(&sid
, usr
->pw_uid
);
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
));
198 DEBUG(15,("quota entry for id[%s] path[%s]\n",
199 sid_string_dbg(&sid
), fsp
->conn
->connectpath
));
201 if ((tmp_list_ent
=TALLOC_ZERO_P(mem_ctx
,SMB_NTQUOTA_LIST
))==NULL
) {
202 DEBUG(0,("TALLOC_ZERO() failed\n"));
204 talloc_destroy(mem_ctx
);
208 if ((tmp_list_ent
->quotas
=TALLOC_ZERO_P(mem_ctx
,SMB_NTQUOTA_STRUCT
))==NULL
) {
209 DEBUG(0,("TALLOC_ZERO() failed\n"));
211 talloc_destroy(mem_ctx
);
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
;
219 DLIST_ADD((*qt_list
),tmp_list_ent
);
227 static int quota_handle_destructor(SMB_NTQUOTA_HANDLE
*handle
)
229 if (handle
->quota_list
)
230 free_ntquota_list(&handle
->quota_list
);
234 void *init_quota_handle(TALLOC_CTX
*mem_ctx
)
236 SMB_NTQUOTA_HANDLE
*qt_handle
;
241 qt_handle
= TALLOC_ZERO_P(mem_ctx
,SMB_NTQUOTA_HANDLE
);
242 if (qt_handle
==NULL
) {
243 DEBUG(0,("TALLOC_ZERO() failed\n"));
247 talloc_set_destructor(qt_handle
, quota_handle_destructor
);
248 return (void *)qt_handle
;