s3-includes: only include system/passwd.h when needed.
[Samba.git] / source3 / smbd / ntquotas.c
blob38ee297dde227274f052d72fc87d2fdad3cf6d0e
1 /*
2 Unix SMB/CIFS implementation.
3 NT QUOTA suppport
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/>.
20 #include "includes.h"
21 #include "../lib/util/util_pw.h"
22 #include "system/passwd.h"
24 #undef DBGC_CLASS
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);
32 if (in>0 && ret==0) {
33 /* we have to make sure that a overflow didn't set NO_LIMIT */
34 ret = (uint64_t)1;
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;
44 return ret;
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);
53 if (ret < in) {
54 /* we overflow */
55 ret = SMB_NTQUOTAS_NO_LIMIT;
58 if (in == SMB_QUOTAS_NO_LIMIT)
59 ret = SMB_NTQUOTAS_NO_LIMIT;
61 return ret;
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)
71 ret = (uint64_t)1;
73 return ret;
76 int vfs_get_ntquota(files_struct *fsp, enum SMB_QUOTA_TYPE qtype, struct dom_sid *psid, SMB_NTQUOTA_STRUCT *qt)
78 int ret;
79 SMB_DISK_QUOTA D;
80 unid_t id;
82 ZERO_STRUCT(D);
84 if (!fsp||!fsp->conn||!qt)
85 return (-1);
87 ZERO_STRUCT(*qt);
89 id.uid = -1;
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);
98 if (psid)
99 qt->sid = *psid;
101 if (ret!=0) {
102 return ret;
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;
111 return 0;
114 int vfs_set_ntquota(files_struct *fsp, enum SMB_QUOTA_TYPE qtype, struct dom_sid *psid, SMB_NTQUOTA_STRUCT *qt)
116 int ret;
117 SMB_DISK_QUOTA D;
118 unid_t id;
119 ZERO_STRUCT(D);
121 if (!fsp||!fsp->conn||!qt)
122 return (-1);
124 id.uid = -1;
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);
142 return ret;
145 static bool allready_in_quota_list(SMB_NTQUOTA_LIST *qt_list, uid_t uid)
147 SMB_NTQUOTA_LIST *tmp_list = NULL;
149 if (!qt_list)
150 return False;
152 for (tmp_list=qt_list;tmp_list!=NULL;tmp_list=tmp_list->next) {
153 if (tmp_list->uid == uid) {
154 return True;
158 return False;
161 int vfs_get_user_ntquota_list(files_struct *fsp, SMB_NTQUOTA_LIST **qt_list)
163 struct passwd *usr;
164 TALLOC_CTX *mem_ctx = NULL;
166 if (!fsp||!fsp->conn||!qt_list)
167 return (-1);
169 *qt_list = NULL;
171 if ((mem_ctx=talloc_init("SMB_USER_QUOTA_LIST"))==NULL) {
172 DEBUG(0,("talloc_init() failed\n"));
173 return (-1);
176 sys_setpwent();
177 while ((usr = sys_getpwent()) != NULL) {
178 SMB_NTQUOTA_STRUCT tmp_qt;
179 SMB_NTQUOTA_LIST *tmp_list_ent;
180 struct dom_sid sid;
182 ZERO_STRUCT(tmp_qt);
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));
186 continue;
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));
195 continue;
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"));
203 *qt_list = NULL;
204 talloc_destroy(mem_ctx);
205 return (-1);
208 if ((tmp_list_ent->quotas=TALLOC_ZERO_P(mem_ctx,SMB_NTQUOTA_STRUCT))==NULL) {
209 DEBUG(0,("TALLOC_ZERO() failed\n"));
210 *qt_list = NULL;
211 talloc_destroy(mem_ctx);
212 return (-1);
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);
222 sys_endpwent();
224 return 0;
227 static int quota_handle_destructor(SMB_NTQUOTA_HANDLE *handle)
229 if (handle->quota_list)
230 free_ntquota_list(&handle->quota_list);
231 return 0;
234 void *init_quota_handle(TALLOC_CTX *mem_ctx)
236 SMB_NTQUOTA_HANDLE *qt_handle;
238 if (!mem_ctx)
239 return False;
241 qt_handle = TALLOC_ZERO_P(mem_ctx,SMB_NTQUOTA_HANDLE);
242 if (qt_handle==NULL) {
243 DEBUG(0,("TALLOC_ZERO() failed\n"));
244 return NULL;
247 talloc_set_destructor(qt_handle, quota_handle_destructor);
248 return (void *)qt_handle;