Skip to content

Commit 081ac97

Browse files
author
Rain Oksvort
committed
Update to work with Samba 4.17.12 and Python 3.11
1 parent ef290e6 commit 081ac97

File tree

10 files changed

+222
-109
lines changed

10 files changed

+222
-109
lines changed

Makefile.in

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
CC = @CC@
2-
CFLAGS = @CFLAGS@
2+
CFLAGS = @CFLAGS@ -D__STDC_WANT_LIB_EXT1__=1
33
CPPFLAGS = @CPPFLAGS@
44
LDFLAGS = @LDFLAGS@
55
LDSHFLAGS = @LDSHFLAGS@
6-
LDSUFFIX = -lpython2.7
6+
LDSUFFIX = -lpython3.11
77
INSTALLCMD = @INSTALL@
88
SAMBA_SOURCE = @SAMBA_SOURCE@
99
SHLIBEXT = @SHLIBEXT@
@@ -20,7 +20,9 @@ FLAGS = $(CFLAGS) $(CPPFLAGS) -fPIC \
2020
-I$(SAMBA_SOURCE)/librpc \
2121
-I$(SAMBA_SOURCE)/../librpc \
2222
-I$(SAMBA_SOURCE)/../ \
23-
-I$(SAMBA_SOURCE) -I.
23+
-I$(SAMBA_SOURCE) -I. \
24+
-I$(SAMBA_SOURCE)/../bin/default/include \
25+
-I$(SAMBA_SOURCE)/../bin/default
2426

2527

2628
prefix = @prefix@

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,25 @@ Installation
88
You must have the Python headers installed (`python-dev`) and the Samba sourcecode lying around.
99

1010
Clone this repository, then compile as follows:
11+
```sh
12+
./configure --enable-debug --enable-developer --with-samba-source=/path/to/samba/source3
13+
# or
14+
./configure --with-samba-source=$HOME/samba-4.17.12/source3
1115

12-
./configure --enable-debug --enable-developer --with-samba-source=/path/to/samba/source3
13-
make
16+
make
17+
```
1418

1519
Copy / symlink the resulting `vfs_python.so` to `/usr/local/samba/lib/vfs/python.so` (or wherever your VFS modules are).
1620

1721
In your `smb.conf`, enable `vfs_python` per share:
18-
19-
[myshare]
20-
path = /tmp
21-
vfs objects = python
22-
python:script = /path/to/your/handler.py
22+
```ini
23+
[global]
24+
log level = 5 # Debug in C code can on beconfigured from [global]
25+
[myshare]
26+
path = /tmp
27+
vfs objects = python
28+
python:script = /path/to/your/handler.py
29+
```
2330

2431
and make sure that the script path is valid.
2532

commands.c

Lines changed: 47 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
#include "includes.h"
12
#include "commands.h"
3+
#include "vfs.h"
24

35
int python_connect(vfs_handle_struct *handle,
46
const char *service,
@@ -11,8 +13,8 @@ int python_connect(vfs_handle_struct *handle,
1113
{
1214
PyObject *py_ret = PyObject_CallFunction(py_func, "ss", service, user);
1315
success = PyObject_IsTrue(py_ret);
14-
Py_DECREF(py_ret);
15-
Py_DECREF(py_func);
16+
Py_XDECREF(py_ret);
17+
Py_XDECREF(py_func);
1618
}
1719

1820
if (success == 1)
@@ -30,48 +32,57 @@ void python_disconnect(vfs_handle_struct *handle)
3032
SMB_VFS_NEXT_DISCONNECT(handle);
3133
}
3234

33-
34-
int python_mkdir(vfs_handle_struct *handle,
35-
const char *path,
36-
mode_t mode)
35+
int python_mkdirat(vfs_handle_struct *handle,
36+
struct files_struct *dirfsp,
37+
const struct smb_filename *smb_fname,
38+
mode_t mode)
3739
{
3840
int success = 1;
3941

4042
PyObject *py_func = get_func(handle, "mkdir");
4143
if (py_func != NULL)
4244
{
43-
PyObject *py_ret = PyObject_CallFunction(py_func, "s", path);
45+
PyObject *py_ret = PyObject_CallFunction(py_func, "s", smb_fname->base_name);
4446
success = PyObject_IsTrue(py_ret);
45-
Py_DECREF(py_ret);
46-
Py_DECREF(py_func);
47+
Py_XDECREF(py_ret);
48+
Py_XDECREF(py_func);
4749
}
4850

4951
if (success == 1)
5052
{
51-
return SMB_VFS_NEXT_MKDIR(handle, path, mode);
53+
return SMB_VFS_NEXT_MKDIRAT(handle, dirfsp, smb_fname, mode);
5254
}
5355
else
5456
{
5557
return -1;
5658
}
5759
}
5860

59-
int python_rmdir(vfs_handle_struct *handle, const char *path)
61+
int python_unlinkat(vfs_handle_struct *handle,
62+
struct files_struct *dirfsp,
63+
const struct smb_filename *smb_fname,
64+
int flags)
6065
{
6166
int success = 1;
6267

63-
PyObject *py_func = get_func(handle, "rmdir");
68+
PyObject *py_func = NULL;
69+
if (flags & AT_REMOVEDIR) {
70+
py_func = get_func(handle, "rmdir");
71+
} else {
72+
py_func = get_func(handle, "unlink");
73+
}
74+
6475
if (py_func != NULL)
6576
{
66-
PyObject *py_ret = PyObject_CallFunction(py_func, "s", path);
77+
PyObject *py_ret = PyObject_CallFunction(py_func, "s", smb_fname->base_name);
6778
success = PyObject_IsTrue(py_ret);
68-
Py_DECREF(py_ret);
69-
Py_DECREF(py_func);
79+
Py_XDECREF(py_ret);
80+
Py_XDECREF(py_func);
7081
}
7182

7283
if (success == 1)
7384
{
74-
return SMB_VFS_NEXT_RMDIR(handle, path);
85+
return SMB_VFS_NEXT_UNLINKAT(handle, dirfsp, smb_fname, flags);
7586
}
7687
else
7788
{
@@ -81,20 +92,23 @@ int python_rmdir(vfs_handle_struct *handle, const char *path)
8192

8293
NTSTATUS python_create_file(struct vfs_handle_struct *handle,
8394
struct smb_request *req,
84-
uint16_t root_dir_fid,
95+
struct files_struct *dirfsp,
8596
struct smb_filename *smb_fname,
8697
uint32_t access_mask,
8798
uint32_t share_access,
8899
uint32_t create_disposition,
89100
uint32_t create_options,
90101
uint32_t file_attributes,
91102
uint32_t oplock_request,
103+
const struct smb2_lease *lease,
92104
uint64_t allocation_size,
93105
uint32_t private_flags,
94106
struct security_descriptor *sd,
95107
struct ea_list *ea_list,
96108
files_struct **result,
97-
int *pinfo)
109+
int *pinfo,
110+
const struct smb2_create_blobs *in_context_blobs,
111+
struct smb2_create_blobs *out_context_blobs)
98112
{
99113
int success = 1;
100114

@@ -103,38 +117,43 @@ NTSTATUS python_create_file(struct vfs_handle_struct *handle,
103117
{
104118
PyObject *py_ret = PyObject_CallFunction(py_func, "s", smb_fname->base_name);
105119
success = PyObject_IsTrue(py_ret);
106-
Py_DECREF(py_ret);
107-
Py_DECREF(py_func);
120+
Py_XDECREF(py_ret);
121+
Py_XDECREF(py_func);
108122
}
109123

110124
if (success == 1)
111125
{
112126
return SMB_VFS_NEXT_CREATE_FILE(handle,
113127
req,
114-
root_dir_fid,
128+
dirfsp,
115129
smb_fname,
116130
access_mask,
117131
share_access,
118132
create_disposition,
119133
create_options,
120134
file_attributes,
121135
oplock_request,
136+
lease,
122137
allocation_size,
123138
private_flags,
124139
sd,
125140
ea_list,
126141
result,
127-
pinfo);
142+
pinfo,
143+
in_context_blobs,
144+
out_context_blobs);
128145
}
129146
else
130147
{
131148
return NT_STATUS_UNSUCCESSFUL;
132149
}
133150
}
134151

135-
int python_rename(vfs_handle_struct *handle,
136-
const struct smb_filename *smb_fname_src,
137-
const struct smb_filename *smb_fname_dst)
152+
int python_renameat(struct vfs_handle_struct *handle,
153+
struct files_struct *oldfsp,
154+
const struct smb_filename *smb_fname_src,
155+
struct files_struct *newfsp,
156+
const struct smb_filename *smb_fname_dst)
138157
{
139158
int success = 1;
140159

@@ -146,40 +165,17 @@ int python_rename(vfs_handle_struct *handle,
146165
smb_fname_src->base_name,
147166
smb_fname_dst->base_name);
148167
success = PyObject_IsTrue(py_ret);
149-
Py_DECREF(py_ret);
150-
Py_DECREF(py_func);
168+
Py_XDECREF(py_ret);
169+
Py_XDECREF(py_func);
151170
}
152171

153172
if (success == 1)
154173
{
155-
return SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
174+
return SMB_VFS_NEXT_RENAMEAT(handle, oldfsp, smb_fname_src, newfsp, smb_fname_dst);
156175
}
157176
else
158177
{
159178
return -1;
160179
}
161180
}
162181

163-
int python_unlink(vfs_handle_struct *handle,
164-
const struct smb_filename *smb_fname)
165-
{
166-
int success = 1;
167-
168-
PyObject *py_func = get_func(handle, "unlink");
169-
if (py_func != NULL)
170-
{
171-
PyObject *py_ret = PyObject_CallFunction(py_func, "s", smb_fname->base_name);
172-
success = PyObject_IsTrue(py_ret);
173-
Py_DECREF(py_ret);
174-
Py_DECREF(py_func);
175-
}
176-
177-
if (success == 1)
178-
{
179-
return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
180-
}
181-
else
182-
{
183-
return -1;
184-
}
185-
}

commands.h

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
1-
#include "python2.7/Python.h"
1+
#include "python3.11/Python.h"
22
#include "python_importer.h"
33

44
int python_connect(vfs_handle_struct *handle,
55
const char *service,
66
const char *user);
77
void python_disconnect(vfs_handle_struct *handle);
8-
int python_mkdir(vfs_handle_struct *handle,
9-
const char *path,
10-
mode_t mode);
11-
int python_rmdir(vfs_handle_struct *handle, const char *path);
12-
8+
int python_mkdirat(vfs_handle_struct *handle,
9+
struct files_struct *dirfsp,
10+
const struct smb_filename *smb_fname,
11+
mode_t mode);
12+
int python_unlinkat(vfs_handle_struct *handle,
13+
struct files_struct *dirfsp,
14+
const struct smb_filename *smb_fname,
15+
int flags);
1316
NTSTATUS python_create_file(struct vfs_handle_struct *handle,
1417
struct smb_request *req,
15-
uint16_t root_dir_fid,
18+
struct files_struct *dirfsp,
1619
struct smb_filename *smb_fname,
1720
uint32_t access_mask,
1821
uint32_t share_access,
1922
uint32_t create_disposition,
2023
uint32_t create_options,
2124
uint32_t file_attributes,
2225
uint32_t oplock_request,
26+
const struct smb2_lease *lease,
2327
uint64_t allocation_size,
2428
uint32_t private_flags,
2529
struct security_descriptor *sd,
2630
struct ea_list *ea_list,
2731
files_struct **result,
28-
int *pinfo);
29-
int python_rename(vfs_handle_struct *handle,
30-
const struct smb_filename *smb_fname_src,
31-
const struct smb_filename *smb_fname_dst);
32-
int python_unlink(vfs_handle_struct *handle,
33-
const struct smb_filename *smb_fname);
32+
int *pinfo,
33+
const struct smb2_create_blobs *in_context_blobs,
34+
struct smb2_create_blobs *out_context_blobs);
35+
int python_renameat(struct vfs_handle_struct *handle,
36+
struct files_struct *oldfsp,
37+
const struct smb_filename *smb_fname_src,
38+
struct files_struct *newfsp,
39+
const struct smb_filename *smb_fname_dst);

handler.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
def debug(text):
2-
with open('/tmp/samba.log', 'a') as fo:
3-
fo.write('PYTHON: {}\n'.format(text))
4-
51
def connect(service='default', user='default'):
62
debug('{} has connected to {}.'.format(user, service))
73
return True

helpers/reload_dev.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Recompiles and reloads the addon to simplify development
2+
# Assumes that you already have Samba installed and configured
3+
4+
if [ "$(id -u)" -ne 0 ]; then
5+
echo "Please run as root"
6+
exit 1
7+
fi
8+
9+
make clean
10+
make
11+
12+
cp vfs_python.so /usr/lib/x86_64-linux-gnu/samba/vfs/python.so
13+
chown root:root /usr/lib/x86_64-linux-gnu/samba/vfs/python.so
14+
chmod 644 /usr/lib/x86_64-linux-gnu/samba/vfs/python.so
15+
16+
cp handler.py /usr/lib/x86_64-linux-gnu/samba/vfs/handler.py
17+
chown root:root /usr/lib/x86_64-linux-gnu/samba/vfs/handler.py
18+
chmod 644 /usr/lib/x86_64-linux-gnu/samba/vfs/handler.py
19+
20+
rm -r /var/log/samba
21+
mkdir /var/log/samba
22+
chown root:root /var/log/samba
23+
systemctl restart smbd

0 commit comments

Comments
 (0)