vfs_shadow_copy2: check crossmountpoints against snapdirseverywhere
[obnox/samba/samba-obnox.git] / source3 / param / service.c
1 /*
2    Unix SMB/CIFS implementation.
3    service (connection) opening and closing
4    Copyright (C) Andrew Tridgell 1992-1998
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 "system/filesys.h"
22 #include "../lib/tsocket/tsocket.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "../librpc/gen_ndr/netlogon.h"
26 #include "../libcli/security/security.h"
27 #include "printing/pcap.h"
28 #include "passdb/lookup_sid.h"
29 #include "auth.h"
30 #include "lib/param/loadparm.h"
31
32 static int load_registry_service(const char *servicename)
33 {
34         if (!lp_registry_shares()) {
35                 return -1;
36         }
37
38         if ((servicename == NULL) || (*servicename == '\0')) {
39                 return -1;
40         }
41
42         if (strequal(servicename, GLOBAL_NAME)) {
43                 return -2;
44         }
45
46         if (!process_registry_service(servicename)) {
47                 return -1;
48         }
49
50         return lp_servicenumber(servicename);
51 }
52
53 void load_registry_shares(void)
54 {
55         DEBUG(8, ("load_registry_shares()\n"));
56         if (!lp_registry_shares()) {
57                 return;
58         }
59
60         process_registry_shares();
61
62         return;
63 }
64
65 /****************************************************************************
66  Add a home service. Returns the new service number or -1 if fail.
67 ****************************************************************************/
68
69 int add_home_service(const char *service, const char *username, const char *homedir)
70 {
71         int iHomeService;
72
73         if (!service || !homedir || homedir[0] == '\0')
74                 return -1;
75
76         if ((iHomeService = lp_servicenumber(HOMES_NAME)) < 0) {
77                 if ((iHomeService = load_registry_service(HOMES_NAME)) < 0) {
78                         return -1;
79                 }
80         }
81
82         /*
83          * If this is a winbindd provided username, remove
84          * the domain component before adding the service.
85          * Log a warning if the "path=" parameter does not
86          * include any macros.
87          */
88
89         {
90                 const char *p = strchr(service,*lp_winbind_separator());
91
92                 /* We only want the 'user' part of the string */
93                 if (p) {
94                         service = p + 1;
95                 }
96         }
97
98         if (!lp_add_home(service, iHomeService, username, homedir)) {
99                 return -1;
100         }
101
102         return lp_servicenumber(service);
103
104 }
105
106 /**
107  * Find a service entry.
108  *
109  * @param service is modified (to canonical form??)
110  **/
111
112 int find_service(TALLOC_CTX *ctx, const char *service_in, char **p_service_out)
113 {
114         int iService;
115
116         if (!service_in) {
117                 return -1;
118         }
119
120         /* First make a copy. */
121         *p_service_out = talloc_strdup(ctx, service_in);
122         if (!*p_service_out) {
123                 return -1;
124         }
125
126         all_string_sub(*p_service_out,"\\","/",0);
127
128         iService = lp_servicenumber(*p_service_out);
129
130         /* now handle the special case of a home directory */
131         if (iService < 0) {
132                 char *phome_dir = get_user_home_dir(ctx, *p_service_out);
133
134                 if(!phome_dir) {
135                         /*
136                          * Try mapping the servicename, it may
137                          * be a Windows to unix mapped user name.
138                          */
139                         if(map_username(ctx, *p_service_out, p_service_out)) {
140                                 if (*p_service_out == NULL) {
141                                         /* Out of memory. */
142                                         return -1;
143                                 }
144                                 phome_dir = get_user_home_dir(
145                                                 ctx, *p_service_out);
146                         }
147                 }
148
149                 DEBUG(3,("checking for home directory %s gave %s\n",*p_service_out,
150                         phome_dir?phome_dir:"(NULL)"));
151
152                 iService = add_home_service(*p_service_out,*p_service_out /* 'username' */, phome_dir);
153         }
154
155         /* If we still don't have a service, attempt to add it as a printer. */
156         if (iService < 0) {
157                 int iPrinterService;
158
159                 if ((iPrinterService = lp_servicenumber(PRINTERS_NAME)) < 0) {
160                         iPrinterService = load_registry_service(PRINTERS_NAME);
161                 }
162                 if (iPrinterService >= 0) {
163                         DEBUG(3,("checking whether %s is a valid printer name...\n",
164                                 *p_service_out));
165                         if (pcap_printername_ok(*p_service_out)) {
166                                 DEBUG(3,("%s is a valid printer name\n",
167                                         *p_service_out));
168                                 DEBUG(3,("adding %s as a printer service\n",
169                                         *p_service_out));
170                                 lp_add_printer(*p_service_out, iPrinterService);
171                                 iService = lp_servicenumber(*p_service_out);
172                                 if (iService < 0) {
173                                         DEBUG(0,("failed to add %s as a printer service!\n",
174                                                 *p_service_out));
175                                 }
176                         } else {
177                                 DEBUG(3,("%s is not a valid printer name\n",
178                                         *p_service_out));
179                         }
180                 }
181         }
182
183         if (iService < 0) {
184                 iService = load_registry_service(*p_service_out);
185         }
186
187         /* Is it a usershare service ? */
188         if (iService < 0 && *lp_usershare_path(talloc_tos())) {
189                 /* Ensure the name is canonicalized. */
190                 if (!strlower_m(*p_service_out)) {
191                         goto fail;
192                 }
193                 iService = load_usershare_service(*p_service_out);
194         }
195
196         /* just possibly it's a default service? */
197         if (iService < 0) {
198                 char *pdefservice = lp_defaultservice(talloc_tos());
199                 if (pdefservice &&
200                                 *pdefservice &&
201                                 !strequal(pdefservice, *p_service_out)
202                                 && !strstr_m(*p_service_out,"..")) {
203                         /*
204                          * We need to do a local copy here as lp_defaultservice()
205                          * returns one of the rotating lp_string buffers that
206                          * could get overwritten by the recursive find_service() call
207                          * below. Fix from Josef Hinteregger <joehtg@joehtg.co.at>.
208                          */
209                         char *defservice = talloc_strdup(ctx, pdefservice);
210
211                         if (!defservice) {
212                                 goto fail;
213                         }
214
215                         /* Disallow anything except explicit share names. */
216                         if (strequal(defservice,HOMES_NAME) ||
217                                         strequal(defservice, PRINTERS_NAME) ||
218                                         strequal(defservice, "IPC$")) {
219                                 TALLOC_FREE(defservice);
220                                 goto fail;
221                         }
222
223                         iService = find_service(ctx, defservice, p_service_out);
224                         if (!*p_service_out) {
225                                 TALLOC_FREE(defservice);
226                                 iService = -1;
227                                 goto fail;
228                         }
229                         if (iService >= 0) {
230                                 all_string_sub(*p_service_out, "_","/",0);
231                                 iService = lp_add_service(*p_service_out, iService);
232                         }
233                         TALLOC_FREE(defservice);
234                 }
235         }
236
237         if (iService >= 0) {
238                 if (!VALID_SNUM(iService)) {
239                         DEBUG(0,("Invalid snum %d for %s\n",iService,
240                                 *p_service_out));
241                         iService = -1;
242                 }
243         }
244
245   fail:
246
247         if (iService < 0) {
248                 DEBUG(3,("find_service() failed to find service %s\n",
249                         *p_service_out));
250         }
251
252         return (iService);
253 }