drsuapi.idl: fix source_dsa spelling
[samba4-gss.git] / source3 / smbd / smb2_posix.c
blob8797b266260afde888400d09c2c90cdecc8f4f0f
1 /*
2 Unix SMB/CIFS implementation.
3 SMB2 POSIX code.
4 Copyright (C) Jeremy Allison 2022
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 "passdb/lookup_sid.h"
23 #include "librpc/gen_ndr/ndr_security.h"
24 #include "librpc/gen_ndr/smb3posix.h"
25 #include "libcli/security/security.h"
26 #include "source3/modules/util_reparse.h"
27 #include "libcli/smb/reparse.h"
29 static NTSTATUS reparse_buffer_parse_posix_type(uint32_t reparse_tag,
30 uint8_t *data,
31 uint32_t len,
32 mode_t *type)
34 struct reparse_data_buffer *reparse = NULL;
35 NTSTATUS status;
37 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
38 *type = S_IFLNK;
39 return NT_STATUS_OK;
41 if (reparse_tag != IO_REPARSE_TAG_NFS) {
43 * Clients can create reparse points with arbitrary tags, return
44 * anything that is not a NFS one (or symlink) as S_IFREG.
46 DBG_INFO("Unhandled NFS reparse tag: 0x%" PRIx32 "\n",
47 reparse->tag);
48 *type = S_IFREG;
49 return NT_STATUS_OK;
52 reparse = talloc_zero(talloc_tos(), struct reparse_data_buffer);
53 if (reparse == NULL) {
54 DBG_ERR("talloc_zero() failed\n");
55 return NT_STATUS_NO_MEMORY;
58 status = reparse_data_buffer_parse(reparse, reparse, data, len);
59 if (!NT_STATUS_IS_OK(status)) {
60 TALLOC_FREE(reparse);
61 return status;
64 switch (reparse->parsed.nfs.type) {
65 case NFS_SPECFILE_CHR:
66 *type = S_IFCHR;
67 break;
68 case NFS_SPECFILE_BLK:
69 *type = S_IFBLK;
70 break;
71 case NFS_SPECFILE_FIFO:
72 *type = S_IFIFO;
73 break;
74 case NFS_SPECFILE_SOCK:
75 *type = S_IFSOCK;
76 break;
77 default:
78 DBG_ERR("Unhandled NFS reparse type: 0x%" PRIx64 "\n",
79 reparse->parsed.nfs.type);
80 TALLOC_FREE(reparse);
81 return NT_STATUS_REPARSE_POINT_NOT_RESOLVED;
84 TALLOC_FREE(reparse);
85 return status;
88 NTSTATUS smb3_file_posix_information_init(
89 connection_struct *conn,
90 const struct smb_filename *smb_fname,
91 uint32_t dos_attributes,
92 struct smb3_file_posix_information *dst)
94 const struct stat_ex *st = &smb_fname->st;
95 mode_t mode = st->st_ex_mode;
96 uint32_t reparse_tag = 0;
97 NTSTATUS status;
99 switch (mode & S_IFMT) {
100 case S_IFREG:
101 case S_IFDIR:
102 break;
103 default:
105 * All non-directory or regular files are reported
106 * as reparse points. Client may or may not be able
107 * to access these. This should already be set by
108 * fdos_mode(), assert this.
110 SMB_ASSERT(dos_attributes & FILE_ATTRIBUTE_REPARSE_POINT);
111 break;
114 if (dos_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
115 uint8_t *reparse_data = NULL;
116 uint32_t reparse_len;
117 mode_t type = S_IFREG;
119 status = fsctl_get_reparse_point(smb_fname->fsp,
120 talloc_tos(),
121 &reparse_tag,
122 &reparse_data,
123 UINT32_MAX,
124 &reparse_len);
125 if (!NT_STATUS_IS_OK(status)) {
126 DBG_DEBUG("Could not get reparse point for %s: %s\n",
127 smb_fname_str_dbg(smb_fname),
128 nt_errstr(status));
129 return status;
132 status = reparse_buffer_parse_posix_type(reparse_tag,
133 reparse_data,
134 reparse_len,
135 &type);
136 TALLOC_FREE(reparse_data);
137 if (!NT_STATUS_IS_OK(status)) {
138 DBG_DEBUG("Could not parse reparse data for %s: %s\n",
139 smb_fname_str_dbg(smb_fname),
140 nt_errstr(status));
141 return status;
145 * Remove the type info we got via stat() and use what
146 * we got from the reparse point.
148 mode &= ~S_IFMT;
149 mode |= type;
152 *dst = (struct smb3_file_posix_information) {
153 .end_of_file = get_file_size_stat(st),
154 .allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn,NULL,st),
155 .inode = SMB_VFS_FS_FILE_ID(conn, st),
156 .device = st->st_ex_dev,
157 .creation_time = unix_timespec_to_nt_time(st->st_ex_btime),
158 .last_access_time = unix_timespec_to_nt_time(st->st_ex_atime),
159 .last_write_time = unix_timespec_to_nt_time(st->st_ex_mtime),
160 .change_time = unix_timespec_to_nt_time(st->st_ex_ctime),
161 .file_attributes = dos_attributes,
162 .cc.nlinks = st->st_ex_nlink,
163 .cc.reparse_tag = reparse_tag,
164 .cc.posix_mode = unix_mode_to_wire(mode),
165 .cc.owner = global_sid_NULL,
166 .cc.group = global_sid_NULL,
169 if (st->st_ex_uid != (uid_t)-1) {
170 uid_to_sid(&dst->cc.owner, st->st_ex_uid);
172 if (st->st_ex_gid != (uid_t)-1) {
173 gid_to_sid(&dst->cc.group, st->st_ex_gid);
175 return NT_STATUS_OK;