1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | NT QUOTA suppport
|
---|
4 | Copyright (C) Stefan (metze) Metzmacher 2003
|
---|
5 |
|
---|
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.
|
---|
10 |
|
---|
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.
|
---|
15 |
|
---|
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/>.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include "includes.h"
|
---|
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"
|
---|
26 |
|
---|
27 | #undef DBGC_CLASS
|
---|
28 | #define DBGC_CLASS DBGC_QUOTA
|
---|
29 |
|
---|
30 | static uint64_t limit_nt2unix(uint64_t in, uint64_t bsize)
|
---|
31 | {
|
---|
32 | uint64_t ret = (uint64_t)0;
|
---|
33 |
|
---|
34 | ret = (uint64_t)(in/bsize);
|
---|
35 | if (in>0 && ret==0) {
|
---|
36 | /* we have to make sure that a overflow didn't set NO_LIMIT */
|
---|
37 | ret = (uint64_t)1;
|
---|
38 | }
|
---|
39 |
|
---|
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;
|
---|
46 |
|
---|
47 | return ret;
|
---|
48 | }
|
---|
49 |
|
---|
50 | static uint64_t limit_unix2nt(uint64_t in, uint64_t bsize)
|
---|
51 | {
|
---|
52 | uint64_t ret = (uint64_t)0;
|
---|
53 |
|
---|
54 | ret = (uint64_t)(in*bsize);
|
---|
55 |
|
---|
56 | if (ret < in) {
|
---|
57 | /* we overflow */
|
---|
58 | ret = SMB_NTQUOTAS_NO_LIMIT;
|
---|
59 | }
|
---|
60 |
|
---|
61 | if (in == SMB_QUOTAS_NO_LIMIT)
|
---|
62 | ret = SMB_NTQUOTAS_NO_LIMIT;
|
---|
63 |
|
---|
64 | return ret;
|
---|
65 | }
|
---|
66 |
|
---|
67 | static uint64_t limit_blk2inodes(uint64_t in)
|
---|
68 | {
|
---|
69 | uint64_t ret = (uint64_t)0;
|
---|
70 |
|
---|
71 | ret = (uint64_t)(in/2);
|
---|
72 |
|
---|
73 | if (ret == 0 && in != 0)
|
---|
74 | ret = (uint64_t)1;
|
---|
75 |
|
---|
76 | return ret;
|
---|
77 | }
|
---|
78 |
|
---|
79 | int vfs_get_ntquota(files_struct *fsp, enum SMB_QUOTA_TYPE qtype, struct dom_sid *psid, SMB_NTQUOTA_STRUCT *qt)
|
---|
80 | {
|
---|
81 | int ret;
|
---|
82 | SMB_DISK_QUOTA D;
|
---|
83 | unid_t id;
|
---|
84 |
|
---|
85 | ZERO_STRUCT(D);
|
---|
86 |
|
---|
87 | if (!fsp||!fsp->conn||!qt)
|
---|
88 | return (-1);
|
---|
89 |
|
---|
90 | ZERO_STRUCT(*qt);
|
---|
91 |
|
---|
92 | id.uid = -1;
|
---|
93 |
|
---|
94 | if (psid && !sid_to_uid(psid, &id.uid)) {
|
---|
95 | DEBUG(0,("sid_to_uid: failed, SID[%s]\n",
|
---|
96 | sid_string_dbg(psid)));
|
---|
97 | }
|
---|
98 |
|
---|
99 | ret = SMB_VFS_GET_QUOTA(fsp->conn, qtype, id, &D);
|
---|
100 |
|
---|
101 | if (psid)
|
---|
102 | qt->sid = *psid;
|
---|
103 |
|
---|
104 | if (ret!=0) {
|
---|
105 | return ret;
|
---|
106 | }
|
---|
107 |
|
---|
108 | qt->usedspace = (uint64_t)D.curblocks*D.bsize;
|
---|
109 | qt->softlim = limit_unix2nt(D.softlimit, D.bsize);
|
---|
110 | qt->hardlim = limit_unix2nt(D.hardlimit, D.bsize);
|
---|
111 | qt->qflags = D.qflags;
|
---|
112 |
|
---|
113 |
|
---|
114 | return 0;
|
---|
115 | }
|
---|
116 |
|
---|
117 | int vfs_set_ntquota(files_struct *fsp, enum SMB_QUOTA_TYPE qtype, struct dom_sid *psid, SMB_NTQUOTA_STRUCT *qt)
|
---|
118 | {
|
---|
119 | int ret;
|
---|
120 | SMB_DISK_QUOTA D;
|
---|
121 | unid_t id;
|
---|
122 | ZERO_STRUCT(D);
|
---|
123 |
|
---|
124 | if (!fsp||!fsp->conn||!qt)
|
---|
125 | return (-1);
|
---|
126 |
|
---|
127 | id.uid = -1;
|
---|
128 |
|
---|
129 | D.bsize = (uint64_t)QUOTABLOCK_SIZE;
|
---|
130 |
|
---|
131 | D.softlimit = limit_nt2unix(qt->softlim,D.bsize);
|
---|
132 | D.hardlimit = limit_nt2unix(qt->hardlim,D.bsize);
|
---|
133 | D.qflags = qt->qflags;
|
---|
134 |
|
---|
135 | D.isoftlimit = limit_blk2inodes(D.softlimit);
|
---|
136 | D.ihardlimit = limit_blk2inodes(D.hardlimit);
|
---|
137 |
|
---|
138 | if (psid && !sid_to_uid(psid, &id.uid)) {
|
---|
139 | DEBUG(0,("sid_to_uid: failed, SID[%s]\n",
|
---|
140 | sid_string_dbg(psid)));
|
---|
141 | }
|
---|
142 |
|
---|
143 | ret = SMB_VFS_SET_QUOTA(fsp->conn, qtype, id, &D);
|
---|
144 |
|
---|
145 | return ret;
|
---|
146 | }
|
---|
147 |
|
---|
148 | static bool allready_in_quota_list(SMB_NTQUOTA_LIST *qt_list, uid_t uid)
|
---|
149 | {
|
---|
150 | SMB_NTQUOTA_LIST *tmp_list = NULL;
|
---|
151 |
|
---|
152 | if (!qt_list)
|
---|
153 | return False;
|
---|
154 |
|
---|
155 | for (tmp_list=qt_list;tmp_list!=NULL;tmp_list=tmp_list->next) {
|
---|
156 | if (tmp_list->uid == uid) {
|
---|
157 | return True;
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | return False;
|
---|
162 | }
|
---|
163 |
|
---|
164 | int vfs_get_user_ntquota_list(files_struct *fsp, SMB_NTQUOTA_LIST **qt_list)
|
---|
165 | {
|
---|
166 | struct passwd *usr;
|
---|
167 | TALLOC_CTX *mem_ctx = NULL;
|
---|
168 |
|
---|
169 | if (!fsp||!fsp->conn||!qt_list)
|
---|
170 | return (-1);
|
---|
171 |
|
---|
172 | *qt_list = NULL;
|
---|
173 |
|
---|
174 | if ((mem_ctx=talloc_init("SMB_USER_QUOTA_LIST"))==NULL) {
|
---|
175 | DEBUG(0,("talloc_init() failed\n"));
|
---|
176 | return (-1);
|
---|
177 | }
|
---|
178 |
|
---|
179 | sys_setpwent();
|
---|
180 | while ((usr = sys_getpwent()) != NULL) {
|
---|
181 | SMB_NTQUOTA_STRUCT tmp_qt;
|
---|
182 | SMB_NTQUOTA_LIST *tmp_list_ent;
|
---|
183 | struct dom_sid sid;
|
---|
184 |
|
---|
185 | ZERO_STRUCT(tmp_qt);
|
---|
186 |
|
---|
187 | if (allready_in_quota_list((*qt_list),usr->pw_uid)) {
|
---|
188 | DEBUG(5,("record for uid[%ld] allready in the list\n",(long)usr->pw_uid));
|
---|
189 | continue;
|
---|
190 | }
|
---|
191 |
|
---|
192 | uid_to_sid(&sid, usr->pw_uid);
|
---|
193 |
|
---|
194 | if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &tmp_qt)!=0) {
|
---|
195 | DEBUG(5,("no quota entry for sid[%s] path[%s]\n",
|
---|
196 | sid_string_dbg(&sid),
|
---|
197 | fsp->conn->connectpath));
|
---|
198 | continue;
|
---|
199 | }
|
---|
200 |
|
---|
201 | DEBUG(15,("quota entry for id[%s] path[%s]\n",
|
---|
202 | sid_string_dbg(&sid), fsp->conn->connectpath));
|
---|
203 |
|
---|
204 | if ((tmp_list_ent=TALLOC_ZERO_P(mem_ctx,SMB_NTQUOTA_LIST))==NULL) {
|
---|
205 | DEBUG(0,("TALLOC_ZERO() failed\n"));
|
---|
206 | *qt_list = NULL;
|
---|
207 | talloc_destroy(mem_ctx);
|
---|
208 | return (-1);
|
---|
209 | }
|
---|
210 |
|
---|
211 | if ((tmp_list_ent->quotas=TALLOC_ZERO_P(mem_ctx,SMB_NTQUOTA_STRUCT))==NULL) {
|
---|
212 | DEBUG(0,("TALLOC_ZERO() failed\n"));
|
---|
213 | *qt_list = NULL;
|
---|
214 | talloc_destroy(mem_ctx);
|
---|
215 | return (-1);
|
---|
216 | }
|
---|
217 |
|
---|
218 | tmp_list_ent->uid = usr->pw_uid;
|
---|
219 | memcpy(tmp_list_ent->quotas,&tmp_qt,sizeof(tmp_qt));
|
---|
220 | tmp_list_ent->mem_ctx = mem_ctx;
|
---|
221 |
|
---|
222 | DLIST_ADD((*qt_list),tmp_list_ent);
|
---|
223 |
|
---|
224 | }
|
---|
225 | sys_endpwent();
|
---|
226 |
|
---|
227 | return 0;
|
---|
228 | }
|
---|
229 |
|
---|
230 | static int quota_handle_destructor(SMB_NTQUOTA_HANDLE *handle)
|
---|
231 | {
|
---|
232 | if (handle->quota_list)
|
---|
233 | free_ntquota_list(&handle->quota_list);
|
---|
234 | return 0;
|
---|
235 | }
|
---|
236 |
|
---|
237 | void *init_quota_handle(TALLOC_CTX *mem_ctx)
|
---|
238 | {
|
---|
239 | SMB_NTQUOTA_HANDLE *qt_handle;
|
---|
240 |
|
---|
241 | if (!mem_ctx)
|
---|
242 | return False;
|
---|
243 |
|
---|
244 | qt_handle = TALLOC_ZERO_P(mem_ctx,SMB_NTQUOTA_HANDLE);
|
---|
245 | if (qt_handle==NULL) {
|
---|
246 | DEBUG(0,("TALLOC_ZERO() failed\n"));
|
---|
247 | return NULL;
|
---|
248 | }
|
---|
249 |
|
---|
250 | talloc_set_destructor(qt_handle, quota_handle_destructor);
|
---|
251 | return (void *)qt_handle;
|
---|
252 | }
|
---|