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/>.
23 #define DBGC_CLASS DBGC_QUOTA
25 static uint64_t limit_nt2unix(uint64_t in
, uint64_t bsize
)
27 uint64_t ret
= (uint64_t)0;
29 ret
= (uint64_t)(in
/bsize
);
31 /* we have to make sure that a overflow didn't set NO_LIMIT */
35 if (in
== SMB_NTQUOTAS_NO_LIMIT
)
36 ret
= SMB_QUOTAS_NO_LIMIT
;
37 else if (in
== SMB_NTQUOTAS_NO_SPACE
)
38 ret
= SMB_QUOTAS_NO_SPACE
;
39 else if (in
== SMB_NTQUOTAS_NO_ENTRY
)
40 ret
= SMB_QUOTAS_NO_LIMIT
;
45 static uint64_t limit_unix2nt(uint64_t in
, uint64_t bsize
)
47 uint64_t ret
= (uint64_t)0;
49 ret
= (uint64_t)(in
*bsize
);
53 ret
= SMB_NTQUOTAS_NO_LIMIT
;
56 if (in
== SMB_QUOTAS_NO_LIMIT
)
57 ret
= SMB_NTQUOTAS_NO_LIMIT
;
62 static uint64_t limit_blk2inodes(uint64_t in
)
64 uint64_t ret
= (uint64_t)0;
66 ret
= (uint64_t)(in
/2);
68 if (ret
== 0 && in
!= 0)
74 int vfs_get_ntquota(files_struct
*fsp
, enum SMB_QUOTA_TYPE qtype
, DOM_SID
*psid
, SMB_NTQUOTA_STRUCT
*qt
)
82 if (!fsp
||!fsp
->conn
||!qt
)
89 if (psid
&& !sid_to_uid(psid
, &id
.uid
)) {
90 DEBUG(0,("sid_to_uid: failed, SID[%s]\n",
91 sid_string_dbg(psid
)));
94 ret
= SMB_VFS_GET_QUOTA(fsp
->conn
, qtype
, id
, &D
);
103 qt
->usedspace
= (uint64_t)D
.curblocks
*D
.bsize
;
104 qt
->softlim
= limit_unix2nt(D
.softlimit
, D
.bsize
);
105 qt
->hardlim
= limit_unix2nt(D
.hardlimit
, D
.bsize
);
106 qt
->qflags
= D
.qflags
;
112 int vfs_set_ntquota(files_struct
*fsp
, enum SMB_QUOTA_TYPE qtype
, DOM_SID
*psid
, SMB_NTQUOTA_STRUCT
*qt
)
119 if (!fsp
||!fsp
->conn
||!qt
)
124 D
.bsize
= (uint64_t)QUOTABLOCK_SIZE
;
126 D
.softlimit
= limit_nt2unix(qt
->softlim
,D
.bsize
);
127 D
.hardlimit
= limit_nt2unix(qt
->hardlim
,D
.bsize
);
128 D
.qflags
= qt
->qflags
;
130 D
.isoftlimit
= limit_blk2inodes(D
.softlimit
);
131 D
.ihardlimit
= limit_blk2inodes(D
.hardlimit
);
133 if (psid
&& !sid_to_uid(psid
, &id
.uid
)) {
134 DEBUG(0,("sid_to_uid: failed, SID[%s]\n",
135 sid_string_dbg(psid
)));
138 ret
= SMB_VFS_SET_QUOTA(fsp
->conn
, qtype
, id
, &D
);
143 static bool allready_in_quota_list(SMB_NTQUOTA_LIST
*qt_list
, uid_t uid
)
145 SMB_NTQUOTA_LIST
*tmp_list
= NULL
;
150 for (tmp_list
=qt_list
;tmp_list
!=NULL
;tmp_list
=tmp_list
->next
) {
151 if (tmp_list
->uid
== uid
) {
159 int vfs_get_user_ntquota_list(files_struct
*fsp
, SMB_NTQUOTA_LIST
**qt_list
)
162 TALLOC_CTX
*mem_ctx
= NULL
;
164 if (!fsp
||!fsp
->conn
||!qt_list
)
169 if ((mem_ctx
=talloc_init("SMB_USER_QUOTA_LIST"))==NULL
) {
170 DEBUG(0,("talloc_init() failed\n"));
175 while ((usr
= sys_getpwent()) != NULL
) {
176 SMB_NTQUOTA_STRUCT tmp_qt
;
177 SMB_NTQUOTA_LIST
*tmp_list_ent
;
182 if (allready_in_quota_list((*qt_list
),usr
->pw_uid
)) {
183 DEBUG(5,("record for uid[%ld] allready in the list\n",(long)usr
->pw_uid
));
187 uid_to_sid(&sid
, usr
->pw_uid
);
189 if (vfs_get_ntquota(fsp
, SMB_USER_QUOTA_TYPE
, &sid
, &tmp_qt
)!=0) {
190 DEBUG(5,("no quota entry for sid[%s] path[%s]\n",
191 sid_string_dbg(&sid
),
192 fsp
->conn
->connectpath
));
196 DEBUG(15,("quota entry for id[%s] path[%s]\n",
197 sid_string_dbg(&sid
), fsp
->conn
->connectpath
));
199 if ((tmp_list_ent
=TALLOC_ZERO_P(mem_ctx
,SMB_NTQUOTA_LIST
))==NULL
) {
200 DEBUG(0,("TALLOC_ZERO() failed\n"));
202 talloc_destroy(mem_ctx
);
206 if ((tmp_list_ent
->quotas
=TALLOC_ZERO_P(mem_ctx
,SMB_NTQUOTA_STRUCT
))==NULL
) {
207 DEBUG(0,("TALLOC_ZERO() failed\n"));
209 talloc_destroy(mem_ctx
);
213 tmp_list_ent
->uid
= usr
->pw_uid
;
214 memcpy(tmp_list_ent
->quotas
,&tmp_qt
,sizeof(tmp_qt
));
215 tmp_list_ent
->mem_ctx
= mem_ctx
;
217 DLIST_ADD((*qt_list
),tmp_list_ent
);
225 static int quota_handle_destructor(SMB_NTQUOTA_HANDLE
*handle
)
227 if (handle
->quota_list
)
228 free_ntquota_list(&handle
->quota_list
);
232 void *init_quota_handle(TALLOC_CTX
*mem_ctx
)
234 SMB_NTQUOTA_HANDLE
*qt_handle
;
239 qt_handle
= TALLOC_ZERO_P(mem_ctx
,SMB_NTQUOTA_HANDLE
);
240 if (qt_handle
==NULL
) {
241 DEBUG(0,("TALLOC_ZERO() failed\n"));
245 talloc_set_destructor(qt_handle
, quota_handle_destructor
);
246 return (void *)qt_handle
;