2b9e48a38bbba7c5552ba9310a58e69c55788dfa
[nivanova/samba-autobuild/.git] / file_server / file_server.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    run s3 file server within Samba4
5
6    Copyright (C) Andrew Tridgell        2011
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "talloc.h"
24 #include "tevent.h"
25 #include "system/filesys.h"
26 #include "lib/param/param.h"
27 #include "source4/smbd/service.h"
28 #include "source4/smbd/process_model.h"
29 #include "file_server/file_server.h"
30 #include "dynconfig.h"
31
32 /*
33   generate a smbd config file for the file server
34  */
35 static const char *generate_smb_conf(struct task_server *task)
36 {
37         int fd;
38         struct loadparm_context *lp_ctx = task->lp_ctx;
39         const char *path = smbd_tmp_path(task, lp_ctx, "fileserver.conf");
40
41         if (path == NULL) {
42                 return NULL;
43         }
44
45         fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644);
46         if (fd == -1) {
47                 DEBUG(0,("Failed to create %s", path));
48                 return NULL;
49         }
50
51         fdprintf(fd, "[globals]\n");
52         fdprintf(fd, "# auto-generated config for fileserver\n");
53         fdprintf(fd, "passdb backend = samba4\n");
54         fdprintf(fd, "rpc_server:default = external\n");
55         fdprintf(fd, "rpc_server:svcctl = embedded\n");
56         fdprintf(fd, "rpc_server:srvsvc = embedded\n");
57         fdprintf(fd, "rpc_server:eventlog = embedded\n");
58         fdprintf(fd, "rpc_server:ntsvcs = embedded\n");
59         fdprintf(fd, "rpc_server:winreg = embedded\n");
60         fdprintf(fd, "rpc_server:spoolss = embedded\n");
61         fdprintf(fd, "rpc_daemon:spoolssd = disabled\n");
62         fdprintf(fd, "rpc_server:tcpip = no\n");
63
64         /* If we are using xattr_tdb:file or posix:eadb then we need to load another VFS object */
65         if (lpcfg_parm_string(lp_ctx, NULL, "xattr_tdb", "file")) {
66                 fdprintf(fd, "vfs objects = acl_xattr xattr_tdb\n");
67         } else if (lpcfg_parm_string(lp_ctx, NULL, "posix", "eadb")) {
68                 fdprintf(fd, "vfs objects = acl_xattr posix_eadb\n");
69         } else {
70                 fdprintf(fd, "vfs objects = acl_xattr\n");
71         }
72
73         fdprintf(fd, "map hidden = no\n");
74         fdprintf(fd, "map system = no\n");
75         fdprintf(fd, "map readonly = no\n");
76         fdprintf(fd, "store dos attributes = yes\n");
77
78         fdprintf(fd, "include = %s\n", lpcfg_configfile(lp_ctx));
79
80         fdprintf(fd, "[IPC$]\n");
81         fdprintf(fd, " vfs objects = dfs_samba4\n");
82
83         close(fd);
84         return path;
85 }
86
87 /*
88   called if smbd exits
89  */
90 static void file_server_smbd_done(struct tevent_req *subreq)
91 {
92         int sys_errno;
93         int ret;
94
95         ret = samba_runcmd_recv(subreq, &sys_errno);
96         if (ret != 0) {
97                 DEBUG(0,("file_server smbd daemon died with exit status %d\n", sys_errno));
98         } else {
99                 DEBUG(0,("file_server smbd daemon exited normally\n"));
100         }
101 }
102
103
104 /*
105   startup a copy of smbd as a child daemon
106 */
107 static void s3fs_task_init(struct task_server *task)
108 {
109         const char *fileserver_conf;
110         struct tevent_req *req;
111         const char *smbd_path;
112         const char *smbd_cmd[2] = { NULL, NULL };
113
114         task_server_set_title(task, "task[s3fs_parent]");
115
116         /* create a smb.conf for smbd to use */
117         fileserver_conf = generate_smb_conf(task);
118
119         smbd_path = talloc_asprintf(task, "%s/smbd", dyn_SBINDIR);
120         smbd_cmd[0] = smbd_path;
121
122         /* start it as a child process */
123         req = samba_runcmd_send(task, task->event_ctx, timeval_zero(), 1, 0,
124                                 smbd_cmd,
125                                 "--configfile", fileserver_conf,
126                                 "--foreground",
127                                 debug_get_output_is_stdout()?"--log-stdout":NULL,
128                                 NULL);
129         if (req == NULL) {
130                 DEBUG(0, ("Failed to start smbd as child daemon\n"));
131                 goto failed;
132         }
133
134         tevent_req_set_callback(req, file_server_smbd_done, task);
135
136         DEBUG(1,("Started file server smbd with config %s\n", fileserver_conf));
137         return;
138 failed:
139         task_server_terminate(task, "Failed to startup s3fs smb task", true);
140 }
141
142 /* called at smbd startup - register ourselves as a server service */
143 NTSTATUS server_service_s3fs_init(void);
144
145 NTSTATUS server_service_s3fs_init(void)
146 {
147         return register_server_service("s3fs", s3fs_task_init);
148 }