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