s3:spoolss: Exit if fork call fails
[Samba.git] / source3 / smbd / ntquotas.c
blob47633b682a871cab8f99ff69407bc380baf6c809
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 "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 #include "libcli/security/dom_sid.h"
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_QUOTA
31 static uint64_t limit_nt2unix(uint64_t in, uint64_t bsize)
33 uint64_t ret = (uint64_t)0;
35 ret = (uint64_t)(in/bsize);
36 if (in>0 && ret==0) {
37 /* we have to make sure that a overflow didn't set NO_LIMIT */
38 ret = (uint64_t)1;
41 if (in == SMB_NTQUOTAS_NO_LIMIT)
42 ret = SMB_QUOTAS_NO_LIMIT;
43 else if (in == SMB_NTQUOTAS_NO_SPACE)
44 ret = SMB_QUOTAS_NO_SPACE;
45 else if (in == SMB_NTQUOTAS_NO_ENTRY)
46 ret = SMB_QUOTAS_NO_LIMIT;
48 return ret;
51 static uint64_t limit_unix2nt(uint64_t in, uint64_t bsize)
53 uint64_t ret = (uint64_t)0;
55 ret = (uint64_t)(in*bsize);
57 return ret;
60 NTSTATUS vfs_get_ntquota(files_struct *fsp, enum SMB_QUOTA_TYPE qtype,
61 struct dom_sid *psid, SMB_NTQUOTA_STRUCT *qt)
63 int ret;
64 SMB_DISK_QUOTA D;
65 unid_t id;
66 struct smb_filename *smb_fname_cwd = NULL;
67 int saved_errno = 0;
69 ZERO_STRUCT(D);
71 if (!fsp || !fsp->conn || !qt) {
72 return NT_STATUS_INTERNAL_ERROR;
75 ZERO_STRUCT(*qt);
77 id.uid = -1;
79 if (psid && !sid_to_uid(psid, &id.uid)) {
80 struct dom_sid_buf buf;
81 DEBUG(0,("sid_to_uid: failed, SID[%s]\n",
82 dom_sid_str_buf(psid, &buf)));
83 return NT_STATUS_NO_SUCH_USER;
86 smb_fname_cwd = synthetic_smb_fname(talloc_tos(),
87 ".",
88 NULL,
89 NULL,
90 0);
91 if (smb_fname_cwd == NULL) {
92 return NT_STATUS_NO_MEMORY;
95 ret = SMB_VFS_GET_QUOTA(fsp->conn, smb_fname_cwd, qtype, id, &D);
96 if (ret == -1) {
97 saved_errno = errno;
99 TALLOC_FREE(smb_fname_cwd);
100 if (saved_errno != 0) {
101 errno = saved_errno;
104 if (psid)
105 qt->sid = *psid;
107 if (ret!=0) {
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;
116 return NT_STATUS_OK;
119 int vfs_set_ntquota(files_struct *fsp, enum SMB_QUOTA_TYPE qtype, struct dom_sid *psid, SMB_NTQUOTA_STRUCT *qt)
121 int ret;
122 SMB_DISK_QUOTA D;
123 unid_t id;
124 ZERO_STRUCT(D);
126 if (!fsp||!fsp->conn||!qt)
127 return (-1);
129 id.uid = -1;
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 if (psid && !sid_to_uid(psid, &id.uid)) {
138 struct dom_sid_buf buf;
139 DEBUG(0,("sid_to_uid: failed, SID[%s]\n",
140 dom_sid_str_buf(psid, &buf)));
143 ret = SMB_VFS_SET_QUOTA(fsp->conn, qtype, id, &D);
145 return ret;
148 static bool already_in_quota_list(SMB_NTQUOTA_LIST *qt_list, uid_t uid)
150 SMB_NTQUOTA_LIST *tmp_list = NULL;
152 if (!qt_list)
153 return False;
155 for (tmp_list=qt_list;tmp_list!=NULL;tmp_list=tmp_list->next) {
156 if (tmp_list->uid == uid) {
157 return True;
161 return False;
164 int vfs_get_user_ntquota_list(files_struct *fsp, SMB_NTQUOTA_LIST **qt_list)
166 struct passwd *usr;
167 TALLOC_CTX *mem_ctx = NULL;
169 if (!fsp||!fsp->conn||!qt_list)
170 return (-1);
172 *qt_list = NULL;
174 if ((mem_ctx=talloc_init("SMB_USER_QUOTA_LIST"))==NULL) {
175 DEBUG(0,("talloc_init() failed\n"));
176 return (-1);
179 setpwent();
180 while ((usr = getpwent()) != NULL) {
181 SMB_NTQUOTA_STRUCT tmp_qt;
182 SMB_NTQUOTA_LIST *tmp_list_ent;
183 struct dom_sid sid;
184 struct dom_sid_buf buf;
185 NTSTATUS status;
187 ZERO_STRUCT(tmp_qt);
189 if (already_in_quota_list((*qt_list),usr->pw_uid)) {
190 DEBUG(5,("record for uid[%ld] already in the list\n",(long)usr->pw_uid));
191 continue;
194 uid_to_sid(&sid, usr->pw_uid);
196 status =
197 vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &tmp_qt);
198 if (!NT_STATUS_IS_OK(status)) {
199 DEBUG(5, ("failed getting quota for uid[%ld] - %s\n",
200 (long)usr->pw_uid, nt_errstr(status)));
201 continue;
203 if (tmp_qt.softlim == 0 && tmp_qt.hardlim == 0) {
204 DEBUG(5,("no quota entry for sid[%s] path[%s]\n",
205 dom_sid_str_buf(&sid, &buf),
206 fsp->conn->connectpath));
207 continue;
210 DEBUG(15,("quota entry for id[%s] path[%s]\n",
211 dom_sid_str_buf(&sid, &buf),
212 fsp->conn->connectpath));
214 if ((tmp_list_ent=talloc_zero(mem_ctx,SMB_NTQUOTA_LIST))==NULL) {
215 DEBUG(0,("TALLOC_ZERO() failed\n"));
216 *qt_list = NULL;
217 talloc_destroy(mem_ctx);
218 return (-1);
221 if ((tmp_list_ent->quotas=talloc_zero(mem_ctx,SMB_NTQUOTA_STRUCT))==NULL) {
222 DEBUG(0,("TALLOC_ZERO() failed\n"));
223 *qt_list = NULL;
224 talloc_destroy(mem_ctx);
225 return (-1);
228 tmp_list_ent->uid = usr->pw_uid;
229 memcpy(tmp_list_ent->quotas,&tmp_qt,sizeof(tmp_qt));
230 tmp_list_ent->mem_ctx = mem_ctx;
232 DLIST_ADD((*qt_list),tmp_list_ent);
235 endpwent();
237 if (*qt_list == NULL) {
238 TALLOC_FREE(mem_ctx);
240 return 0;
243 static int quota_handle_destructor(SMB_NTQUOTA_HANDLE *handle)
245 free_ntquota_list(&handle->quota_list);
246 return 0;
249 void *init_quota_handle(TALLOC_CTX *mem_ctx)
251 SMB_NTQUOTA_HANDLE *qt_handle;
253 if (!mem_ctx)
254 return NULL;
256 qt_handle = talloc_zero(mem_ctx,SMB_NTQUOTA_HANDLE);
257 if (qt_handle==NULL) {
258 DEBUG(0,("TALLOC_ZERO() failed\n"));
259 return NULL;
262 talloc_set_destructor(qt_handle, quota_handle_destructor);
263 return (void *)qt_handle;