source: branches/samba-3.5.x/source3/smbd/service.c@ 773

Last change on this file since 773 was 773, checked in by Herwig Bauernfeind, 12 years ago

Samba Server 3.5: Update branch to 3.5.20

File size: 36.7 KB
Line 
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 "smbd/globals.h"
22
23extern userdom_struct current_user_info;
24
25static bool canonicalize_connect_path(connection_struct *conn)
26{
27#ifdef REALPATH_TAKES_NULL
28 bool ret;
29 char *resolved_name = SMB_VFS_REALPATH(conn,conn->connectpath,NULL);
30 if (!resolved_name) {
31 return false;
32 }
33 ret = set_conn_connectpath(conn,resolved_name);
34 SAFE_FREE(resolved_name);
35 return ret;
36#else
37 char resolved_name_buf[PATH_MAX+1];
38 char *resolved_name = SMB_VFS_REALPATH(conn,conn->connectpath,resolved_name_buf);
39 if (!resolved_name) {
40 return false;
41 }
42 return set_conn_connectpath(conn,resolved_name);
43#endif /* REALPATH_TAKES_NULL */
44}
45
46/****************************************************************************
47 Ensure when setting connectpath it is a canonicalized (no ./ // or ../)
48 absolute path stating in / and not ending in /.
49 Observent people will notice a similarity between this and check_path_syntax :-).
50****************************************************************************/
51
52bool set_conn_connectpath(connection_struct *conn, const char *connectpath)
53{
54 char *destname;
55 char *d;
56 const char *s = connectpath;
57 bool start_of_name_component = true;
58
59 if (connectpath == NULL || connectpath[0] == '\0') {
60 return false;
61 }
62
63 /* Allocate for strlen + '\0' + possible leading '/' */
64 destname = SMB_MALLOC(strlen(connectpath) + 2);
65 if (!destname) {
66 return false;
67 }
68 d = destname;
69
70#ifndef __OS2__
71 *d++ = '/'; /* Always start with root. */
72
73 while (*s) {
74 if (*s == '/') {
75 /* Eat multiple '/' */
76 while (*s == '/') {
77 s++;
78 }
79 if ((d > destname + 1) && (*s != '\0')) {
80 *d++ = '/';
81 }
82 start_of_name_component = True;
83 continue;
84 }
85
86 if (start_of_name_component) {
87 if ((s[0] == '.') && (s[1] == '.') && (s[2] == '/' || s[2] == '\0')) {
88 /* Uh oh - "/../" or "/..\0" ! */
89
90 /* Go past the ../ or .. */
91 if (s[2] == '/') {
92 s += 3;
93 } else {
94 s += 2; /* Go past the .. */
95 }
96
97 /* If we just added a '/' - delete it */
98 if ((d > destname) && (*(d-1) == '/')) {
99 *(d-1) = '\0';
100 d--;
101 }
102
103 /* Are we at the start ? Can't go back further if so. */
104 if (d <= destname) {
105 *d++ = '/'; /* Can't delete root */
106 continue;
107 }
108 /* Go back one level... */
109 /* Decrement d first as d points to the *next* char to write into. */
110 for (d--; d > destname; d--) {
111 if (*d == '/') {
112 break;
113 }
114 }
115 /* We're still at the start of a name component, just the previous one. */
116 continue;
117 } else if ((s[0] == '.') && ((s[1] == '\0') || s[1] == '/')) {
118 /* Component of pathname can't be "." only - skip the '.' . */
119 if (s[1] == '/') {
120 s += 2;
121 } else {
122 s++;
123 }
124 continue;
125 }
126 }
127
128 if (!(*s & 0x80)) {
129 *d++ = *s++;
130 } else {
131 size_t siz;
132 /* Get the size of the next MB character. */
133 next_codepoint(s,&siz);
134 switch(siz) {
135 case 5:
136 *d++ = *s++;
137 /*fall through*/
138 case 4:
139 *d++ = *s++;
140 /*fall through*/
141 case 3:
142 *d++ = *s++;
143 /*fall through*/
144 case 2:
145 *d++ = *s++;
146 /*fall through*/
147 case 1:
148 *d++ = *s++;
149 break;
150 default:
151 break;
152 }
153 }
154 start_of_name_component = false;
155 }
156 *d = '\0';
157
158 /* And must not end in '/' */
159 if (d > destname + 1 && (*(d-1) == '/')) {
160 *(d-1) = '\0';
161 }
162#else
163 /* Assume OS/2 users are smart enough to put a correct path in smb.conf, and simply copy the string */
164 safe_strcpy(destname, connectpath, strlen(connectpath));
165#endif
166
167 DEBUG(10,("set_conn_connectpath: service %s, connectpath = %s\n",
168 lp_servicename(SNUM(conn)), destname ));
169
170 string_set(&conn->connectpath, destname);
171 SAFE_FREE(destname);
172 return true;
173}
174
175/****************************************************************************
176 Load parameters specific to a connection/service.
177****************************************************************************/
178
179bool set_current_service(connection_struct *conn, uint16 flags, bool do_chdir)
180{
181 int snum;
182
183 if (!conn) {
184 last_conn = NULL;
185 return(False);
186 }
187
188 conn->lastused_count++;
189
190 snum = SNUM(conn);
191
192 if (do_chdir &&
193 vfs_ChDir(conn,conn->connectpath) != 0 &&
194 vfs_ChDir(conn,conn->origpath) != 0) {
195 DEBUG(((errno!=EACCES)?0:3),("chdir (%s) failed, reason: %s\n",
196 conn->connectpath, strerror(errno)));
197 return(False);
198 }
199
200 if ((conn == last_conn) && (last_flags == flags)) {
201 return(True);
202 }
203
204 last_conn = conn;
205 last_flags = flags;
206
207 /* Obey the client case sensitivity requests - only for clients that support it. */
208 switch (lp_casesensitive(snum)) {
209 case Auto:
210 {
211 /* We need this uglyness due to DOS/Win9x clients that lie about case insensitivity. */
212 enum remote_arch_types ra_type = get_remote_arch();
213 if ((ra_type != RA_SAMBA) && (ra_type != RA_CIFSFS)) {
214 /* Client can't support per-packet case sensitive pathnames. */
215 conn->case_sensitive = False;
216 } else {
217 conn->case_sensitive = !(flags & FLAG_CASELESS_PATHNAMES);
218 }
219 }
220 break;
221 case True:
222 conn->case_sensitive = True;
223 break;
224 default:
225 conn->case_sensitive = False;
226 break;
227 }
228 return(True);
229}
230
231static int load_registry_service(const char *servicename)
232{
233 if (!lp_registry_shares()) {
234 return -1;
235 }
236
237 if ((servicename == NULL) || (*servicename == '\0')) {
238 return -1;
239 }
240
241 if (strequal(servicename, GLOBAL_NAME)) {
242 return -2;
243 }
244
245 if (!process_registry_service(servicename)) {
246 return -1;
247 }
248
249 return lp_servicenumber(servicename);
250}
251
252void load_registry_shares(void)
253{
254 DEBUG(8, ("load_registry_shares()\n"));
255 if (!lp_registry_shares()) {
256 return;
257 }
258
259 process_registry_shares();
260
261 return;
262}
263
264/****************************************************************************
265 Add a home service. Returns the new service number or -1 if fail.
266****************************************************************************/
267
268int add_home_service(const char *service, const char *username, const char *homedir)
269{
270 int iHomeService;
271
272 if (!service || !homedir || homedir[0] == '\0')
273 return -1;
274
275 if ((iHomeService = lp_servicenumber(HOMES_NAME)) < 0) {
276 if ((iHomeService = load_registry_service(HOMES_NAME)) < 0) {
277 return -1;
278 }
279 }
280
281 /*
282 * If this is a winbindd provided username, remove
283 * the domain component before adding the service.
284 * Log a warning if the "path=" parameter does not
285 * include any macros.
286 */
287
288 {
289 const char *p = strchr(service,*lp_winbind_separator());
290
291 /* We only want the 'user' part of the string */
292 if (p) {
293 service = p + 1;
294 }
295 }
296
297 if (!lp_add_home(service, iHomeService, username, homedir)) {
298 return -1;
299 }
300
301 return lp_servicenumber(service);
302
303}
304
305/**
306 * Find a service entry.
307 *
308 * @param service_in is modified (to canonical form??)
309 * and returned in return parameter service.
310 **/
311
312int find_service(const char *service_in, fstring service)
313{
314 int iService;
315 struct smbd_server_connection *sconn = smbd_server_conn;
316
317 if (!service_in) {
318 return -1;
319 }
320
321 fstrcpy(service, service_in);
322 all_string_sub(service,"\\","/",0);
323
324 iService = lp_servicenumber(service);
325
326 /* now handle the special case of a home directory */
327 if (iService < 0) {
328 char *phome_dir = get_user_home_dir(talloc_tos(), service);
329
330 if(!phome_dir) {
331 /*
332 * Try mapping the servicename, it may
333 * be a Windows to unix mapped user name.
334 */
335 if(map_username(sconn, service))
336 phome_dir = get_user_home_dir(
337 talloc_tos(), service);
338 }
339
340 DEBUG(3,("checking for home directory %s gave %s\n",service,
341 phome_dir?phome_dir:"(NULL)"));
342
343 iService = add_home_service(service,service /* 'username' */, phome_dir);
344 }
345
346 /* If we still don't have a service, attempt to add it as a printer. */
347 if (iService < 0) {
348 int iPrinterService;
349
350 if ((iPrinterService = lp_servicenumber(PRINTERS_NAME)) < 0) {
351 iPrinterService = load_registry_service(PRINTERS_NAME);
352 }
353 if (iPrinterService >= 0) {
354 DEBUG(3,("checking whether %s is a valid printer name...\n", service));
355 if (pcap_printername_ok(service)) {
356 DEBUG(3,("%s is a valid printer name\n", service));
357 DEBUG(3,("adding %s as a printer service\n", service));
358 lp_add_printer(service, iPrinterService);
359 iService = lp_servicenumber(service);
360 if (iService < 0) {
361 DEBUG(0,("failed to add %s as a printer service!\n", service));
362 }
363 } else {
364 DEBUG(3,("%s is not a valid printer name\n", service));
365 }
366 }
367 }
368
369 /* Check for default vfs service? Unsure whether to implement this */
370 if (iService < 0) {
371 }
372
373 if (iService < 0) {
374 iService = load_registry_service(service);
375 }
376
377 /* Is it a usershare service ? */
378 if (iService < 0 && *lp_usershare_path()) {
379 /* Ensure the name is canonicalized. */
380 strlower_m(service);
381 iService = load_usershare_service(service);
382 }
383
384 /* just possibly it's a default service? */
385 if (iService < 0) {
386 char *pdefservice = lp_defaultservice();
387 if (pdefservice && *pdefservice && !strequal(pdefservice,service) && !strstr_m(service,"..")) {
388 /*
389 * We need to do a local copy here as lp_defaultservice()
390 * returns one of the rotating lp_string buffers that
391 * could get overwritten by the recursive find_service() call
392 * below. Fix from Josef Hinteregger <joehtg@joehtg.co.at>.
393 */
394 char *defservice = SMB_STRDUP(pdefservice);
395
396 if (!defservice) {
397 goto fail;
398 }
399
400 /* Disallow anything except explicit share names. */
401 if (strequal(defservice,HOMES_NAME) ||
402 strequal(defservice, PRINTERS_NAME) ||
403 strequal(defservice, "IPC$")) {
404 SAFE_FREE(defservice);
405 goto fail;
406 }
407
408 iService = find_service(defservice, service);
409 if (iService >= 0) {
410 all_string_sub(service, "_","/",0);
411 iService = lp_add_service(service, iService);
412 }
413 SAFE_FREE(defservice);
414 }
415 }
416
417 if (iService >= 0) {
418 if (!VALID_SNUM(iService)) {
419 DEBUG(0,("Invalid snum %d for %s\n",iService, service));
420 iService = -1;
421 }
422 }
423
424 fail:
425
426 if (iService < 0)
427 DEBUG(3,("find_service() failed to find service %s\n", service));
428
429 return (iService);
430}
431
432
433/****************************************************************************
434 do some basic sainity checks on the share.
435 This function modifies dev, ecode.
436****************************************************************************/
437
438static NTSTATUS share_sanity_checks(int snum, fstring dev)
439{
440
441 if (!lp_snum_ok(snum) ||
442 !check_access(smbd_server_fd(),
443 lp_hostsallow(snum), lp_hostsdeny(snum))) {
444 return NT_STATUS_ACCESS_DENIED;
445 }
446
447 if (dev[0] == '?' || !dev[0]) {
448 if (lp_print_ok(snum)) {
449 fstrcpy(dev,"LPT1:");
450 } else if (strequal(lp_fstype(snum), "IPC")) {
451 fstrcpy(dev, "IPC");
452 } else {
453 fstrcpy(dev,"A:");
454 }
455 }
456
457 strupper_m(dev);
458
459 if (lp_print_ok(snum)) {
460 if (!strequal(dev, "LPT1:")) {
461 return NT_STATUS_BAD_DEVICE_TYPE;
462 }
463 } else if (strequal(lp_fstype(snum), "IPC")) {
464 if (!strequal(dev, "IPC")) {
465 return NT_STATUS_BAD_DEVICE_TYPE;
466 }
467 } else if (!strequal(dev, "A:")) {
468 return NT_STATUS_BAD_DEVICE_TYPE;
469 }
470
471 /* Behave as a printer if we are supposed to */
472 if (lp_print_ok(snum) && (strcmp(dev, "A:") == 0)) {
473 fstrcpy(dev, "LPT1:");
474 }
475
476 return NT_STATUS_OK;
477}
478
479/*
480 * Go through lookup_name etc to find the force'd group.
481 *
482 * Create a new token from src_token, replacing the primary group sid with the
483 * one found.
484 */
485
486static NTSTATUS find_forced_group(bool force_user,
487 int snum, const char *username,
488 DOM_SID *pgroup_sid,
489 gid_t *pgid)
490{
491 NTSTATUS result = NT_STATUS_NO_SUCH_GROUP;
492 TALLOC_CTX *frame = talloc_stackframe();
493 DOM_SID group_sid;
494 enum lsa_SidType type;
495 char *groupname;
496 bool user_must_be_member = False;
497 gid_t gid;
498
499 groupname = talloc_strdup(talloc_tos(), lp_force_group(snum));
500 if (groupname == NULL) {
501 DEBUG(1, ("talloc_strdup failed\n"));
502 result = NT_STATUS_NO_MEMORY;
503 goto done;
504 }
505
506 if (groupname[0] == '+') {
507 user_must_be_member = True;
508 groupname += 1;
509 }
510
511 groupname = talloc_string_sub(talloc_tos(), groupname,
512 "%S", lp_servicename(snum));
513 if (groupname == NULL) {
514 DEBUG(1, ("talloc_string_sub failed\n"));
515 result = NT_STATUS_NO_MEMORY;
516 goto done;
517 }
518
519 if (!lookup_name_smbconf(talloc_tos(), groupname,
520 LOOKUP_NAME_ALL|LOOKUP_NAME_GROUP,
521 NULL, NULL, &group_sid, &type)) {
522 DEBUG(10, ("lookup_name_smbconf(%s) failed\n",
523 groupname));
524 goto done;
525 }
526
527 if ((type != SID_NAME_DOM_GRP) && (type != SID_NAME_ALIAS) &&
528 (type != SID_NAME_WKN_GRP)) {
529 DEBUG(10, ("%s is a %s, not a group\n", groupname,
530 sid_type_lookup(type)));
531 goto done;
532 }
533
534 if (!sid_to_gid(&group_sid, &gid)) {
535 DEBUG(10, ("sid_to_gid(%s) for %s failed\n",
536 sid_string_dbg(&group_sid), groupname));
537 goto done;
538 }
539
540 /*
541 * If the user has been forced and the forced group starts with a '+',
542 * then we only set the group to be the forced group if the forced
543 * user is a member of that group. Otherwise, the meaning of the '+'
544 * would be ignored.
545 */
546
547 if (force_user && user_must_be_member) {
548 if (user_in_group_sid(username, &group_sid)) {
549 sid_copy(pgroup_sid, &group_sid);
550 *pgid = gid;
551 DEBUG(3,("Forced group %s for member %s\n",
552 groupname, username));
553 } else {
554 DEBUG(0,("find_forced_group: forced user %s is not a member "
555 "of forced group %s. Disallowing access.\n",
556 username, groupname ));
557 result = NT_STATUS_MEMBER_NOT_IN_GROUP;
558 goto done;
559 }
560 } else {
561 sid_copy(pgroup_sid, &group_sid);
562 *pgid = gid;
563 DEBUG(3,("Forced group %s\n", groupname));
564 }
565
566 result = NT_STATUS_OK;
567 done:
568 TALLOC_FREE(frame);
569 return result;
570}
571
572/****************************************************************************
573 Create an auth_serversupplied_info structure for a connection_struct
574****************************************************************************/
575
576static NTSTATUS create_connection_server_info(struct smbd_server_connection *sconn,
577 TALLOC_CTX *mem_ctx, int snum,
578 struct auth_serversupplied_info *vuid_serverinfo,
579 DATA_BLOB password,
580 struct auth_serversupplied_info **presult)
581{
582 if (lp_guest_only(snum)) {
583 return make_server_info_guest(mem_ctx, presult);
584 }
585
586 if (vuid_serverinfo != NULL) {
587
588 struct auth_serversupplied_info *result;
589
590 /*
591 * This is the normal security != share case where we have a
592 * valid vuid from the session setup. */
593
594 if (vuid_serverinfo->guest) {
595 if (!lp_guest_ok(snum)) {
596 DEBUG(2, ("guest user (from session setup) "
597 "not permitted to access this share "
598 "(%s)\n", lp_servicename(snum)));
599 return NT_STATUS_ACCESS_DENIED;
600 }
601 } else {
602 if (!user_ok_token(vuid_serverinfo->unix_name,
603 pdb_get_domain(vuid_serverinfo->sam_account),
604 vuid_serverinfo->ptok, snum)) {
605 DEBUG(2, ("user '%s' (from session setup) not "
606 "permitted to access this share "
607 "(%s)\n",
608 vuid_serverinfo->unix_name,
609 lp_servicename(snum)));
610 return NT_STATUS_ACCESS_DENIED;
611 }
612 }
613
614 result = copy_serverinfo(mem_ctx, vuid_serverinfo);
615 if (result == NULL) {
616 return NT_STATUS_NO_MEMORY;
617 }
618
619 *presult = result;
620 return NT_STATUS_OK;
621 }
622
623 if (lp_security() == SEC_SHARE) {
624
625 fstring user;
626 bool guest;
627
628 /* add the sharename as a possible user name if we
629 are in share mode security */
630
631 add_session_user(sconn, lp_servicename(snum));
632
633 /* shall we let them in? */
634
635 if (!authorise_login(sconn, snum,user,password,&guest)) {
636 DEBUG( 2, ( "Invalid username/password for [%s]\n",
637 lp_servicename(snum)) );
638 return NT_STATUS_WRONG_PASSWORD;
639 }
640
641 return make_serverinfo_from_username(mem_ctx, user, guest,
642 presult);
643 }
644
645 DEBUG(0, ("invalid VUID (vuser) but not in security=share\n"));
646 return NT_STATUS_ACCESS_DENIED;
647}
648
649
650/****************************************************************************
651 Make a connection, given the snum to connect to, and the vuser of the
652 connecting user if appropriate.
653****************************************************************************/
654
655connection_struct *make_connection_snum(struct smbd_server_connection *sconn,
656 int snum, user_struct *vuser,
657 DATA_BLOB password,
658 const char *pdev,
659 NTSTATUS *pstatus)
660{
661 connection_struct *conn;
662 struct smb_filename *smb_fname_cpath = NULL;
663 fstring dev;
664 int ret;
665 char addr[INET6_ADDRSTRLEN];
666 NTSTATUS status;
667
668 fstrcpy(dev, pdev);
669
670 if (NT_STATUS_IS_ERR(*pstatus = share_sanity_checks(snum, dev))) {
671 return NULL;
672 }
673
674 conn = conn_new(sconn);
675 if (!conn) {
676 DEBUG(0,("Couldn't find free connection.\n"));
677 *pstatus = NT_STATUS_INSUFFICIENT_RESOURCES;
678 return NULL;
679 }
680
681 conn->params->service = snum;
682
683 status = create_connection_server_info(sconn,
684 conn, snum, vuser ? vuser->server_info : NULL, password,
685 &conn->server_info);
686
687 if (!NT_STATUS_IS_OK(status)) {
688 DEBUG(1, ("create_connection_server_info failed: %s\n",
689 nt_errstr(status)));
690 *pstatus = status;
691 conn_free(conn);
692 return NULL;
693 }
694
695 if ((lp_guest_only(snum)) || (lp_security() == SEC_SHARE)) {
696 conn->force_user = true;
697 }
698
699 add_session_user(sconn, conn->server_info->unix_name);
700
701 safe_strcpy(conn->client_address,
702 client_addr(get_client_fd(),addr,sizeof(addr)),
703 sizeof(conn->client_address)-1);
704 conn->num_files_open = 0;
705 conn->lastused = conn->lastused_count = time(NULL);
706 conn->used = True;
707 conn->printer = (strncmp(dev,"LPT",3) == 0);
708 conn->ipc = ( (strncmp(dev,"IPC",3) == 0) ||
709 ( lp_enable_asu_support() && strequal(dev,"ADMIN$")) );
710
711 /* Case options for the share. */
712 if (lp_casesensitive(snum) == Auto) {
713 /* We will be setting this per packet. Set to be case
714 * insensitive for now. */
715 conn->case_sensitive = False;
716 } else {
717 conn->case_sensitive = (bool)lp_casesensitive(snum);
718 }
719
720 conn->case_preserve = lp_preservecase(snum);
721 conn->short_case_preserve = lp_shortpreservecase(snum);
722
723 conn->encrypt_level = lp_smb_encrypt(snum);
724
725 conn->veto_list = NULL;
726 conn->hide_list = NULL;
727 conn->veto_oplock_list = NULL;
728 conn->aio_write_behind_list = NULL;
729
730 conn->read_only = lp_readonly(SNUM(conn));
731 conn->admin_user = False;
732
733 if (*lp_force_user(snum)) {
734
735 /*
736 * Replace conn->server_info with a completely faked up one
737 * from the username we are forced into :-)
738 */
739
740 char *fuser;
741 struct auth_serversupplied_info *forced_serverinfo;
742
743 fuser = talloc_string_sub(conn, lp_force_user(snum), "%S",
744 lp_servicename(snum));
745 if (fuser == NULL) {
746 conn_free(conn);
747 *pstatus = NT_STATUS_NO_MEMORY;
748 return NULL;
749 }
750
751 status = make_serverinfo_from_username(
752 conn, fuser, conn->server_info->guest,
753 &forced_serverinfo);
754 if (!NT_STATUS_IS_OK(status)) {
755 conn_free(conn);
756 *pstatus = status;
757 return NULL;
758 }
759
760 /* We don't want to replace the original sanitized_username
761 as it is the original user given in the connect attempt.
762 This is used in '%U' substitutions. */
763 TALLOC_FREE(forced_serverinfo->sanitized_username);
764 forced_serverinfo->sanitized_username =
765 talloc_move(forced_serverinfo,
766 &conn->server_info->sanitized_username);
767
768 TALLOC_FREE(conn->server_info);
769 conn->server_info = forced_serverinfo;
770
771 conn->force_user = True;
772 DEBUG(3,("Forced user %s\n", fuser));
773 }
774
775 /*
776 * If force group is true, then override
777 * any groupid stored for the connecting user.
778 */
779
780 if (*lp_force_group(snum)) {
781
782 status = find_forced_group(
783 conn->force_user, snum, conn->server_info->unix_name,
784 &conn->server_info->ptok->user_sids[1],
785 &conn->server_info->utok.gid);
786
787 if (!NT_STATUS_IS_OK(status)) {
788 conn_free(conn);
789 *pstatus = status;
790 return NULL;
791 }
792
793 /*
794 * We need to cache this gid, to use within
795 * change_to_user() separately from the conn->server_info
796 * struct. We only use conn->server_info directly if
797 * "force_user" was set.
798 */
799 conn->force_group_gid = conn->server_info->utok.gid;
800 }
801
802 conn->vuid = (vuser != NULL) ? vuser->vuid : UID_FIELD_INVALID;
803
804 {
805 char *s = talloc_sub_advanced(talloc_tos(),
806 lp_servicename(SNUM(conn)),
807 conn->server_info->unix_name,
808 conn->connectpath,
809 conn->server_info->utok.gid,
810 conn->server_info->sanitized_username,
811 pdb_get_domain(conn->server_info->sam_account),
812 lp_pathname(snum));
813 if (!s) {
814 conn_free(conn);
815 *pstatus = NT_STATUS_NO_MEMORY;
816 return NULL;
817 }
818
819 if (!set_conn_connectpath(conn,s)) {
820 TALLOC_FREE(s);
821 conn_free(conn);
822 *pstatus = NT_STATUS_NO_MEMORY;
823 return NULL;
824 }
825 DEBUG(3,("Connect path is '%s' for service [%s]\n",s,
826 lp_servicename(snum)));
827 TALLOC_FREE(s);
828 }
829
830 /*
831 * New code to check if there's a share security descripter
832 * added from NT server manager. This is done after the
833 * smb.conf checks are done as we need a uid and token. JRA.
834 *
835 */
836
837 {
838 bool can_write = False;
839
840 can_write = share_access_check(conn->server_info->ptok,
841 lp_servicename(snum),
842 FILE_WRITE_DATA);
843
844 if (!can_write) {
845 if (!share_access_check(conn->server_info->ptok,
846 lp_servicename(snum),
847 FILE_READ_DATA)) {
848 /* No access, read or write. */
849 DEBUG(0,("make_connection: connection to %s "
850 "denied due to security "
851 "descriptor.\n",
852 lp_servicename(snum)));
853 conn_free(conn);
854 *pstatus = NT_STATUS_ACCESS_DENIED;
855 return NULL;
856 } else {
857 conn->read_only = True;
858 }
859 }
860 }
861 /* Initialise VFS function pointers */
862
863 if (!smbd_vfs_init(conn)) {
864 DEBUG(0, ("vfs_init failed for service %s\n",
865 lp_servicename(snum)));
866 conn_free(conn);
867 *pstatus = NT_STATUS_BAD_NETWORK_NAME;
868 return NULL;
869 }
870
871 if ((!conn->printer) && (!conn->ipc)) {
872 conn->notify_ctx = notify_init(conn, server_id_self(),
873 smbd_messaging_context(),
874 smbd_event_context(),
875 conn);
876 }
877
878/* ROOT Activities: */
879 /* explicitly check widelinks here so that we can correctly warn
880 * in the logs. */
881 widelinks_warning(snum);
882
883 /*
884 * Enforce the max connections parameter.
885 */
886
887 if ((lp_max_connections(snum) > 0)
888 && (count_current_connections(lp_servicename(SNUM(conn)), True) >=
889 lp_max_connections(snum))) {
890
891 DEBUG(1, ("Max connections (%d) exceeded for %s\n",
892 lp_max_connections(snum), lp_servicename(snum)));
893 conn_free(conn);
894 *pstatus = NT_STATUS_INSUFFICIENT_RESOURCES;
895 return NULL;
896 }
897
898 /*
899 * Get us an entry in the connections db
900 */
901 if (!claim_connection(conn, lp_servicename(snum), 0)) {
902 DEBUG(1, ("Could not store connections entry\n"));
903 conn_free(conn);
904 *pstatus = NT_STATUS_INTERNAL_DB_ERROR;
905 return NULL;
906 }
907
908 /* Invoke VFS make connection hook - must be the first
909 VFS operation we do. */
910
911 if (SMB_VFS_CONNECT(conn, lp_servicename(snum),
912 conn->server_info->unix_name) < 0) {
913 DEBUG(0,("make_connection: VFS make connection failed!\n"));
914 yield_connection(conn, lp_servicename(snum));
915 conn_free(conn);
916 *pstatus = NT_STATUS_UNSUCCESSFUL;
917 return NULL;
918 }
919
920 /*
921 * Fix compatibility issue pointed out by Volker.
922 * We pass the conn->connectpath to the preexec
923 * scripts as a parameter, so attempt to canonicalize
924 * it here before calling the preexec scripts.
925 * We ignore errors here, as it is possible that
926 * the conn->connectpath doesn't exist yet and
927 * the preexec scripts will create them.
928 */
929
930 (void)canonicalize_connect_path(conn);
931
932 /* Preexecs are done here as they might make the dir we are to ChDir
933 * to below */
934 /* execute any "root preexec = " line */
935 if (*lp_rootpreexec(snum)) {
936 char *cmd = talloc_sub_advanced(talloc_tos(),
937 lp_servicename(SNUM(conn)),
938 conn->server_info->unix_name,
939 conn->connectpath,
940 conn->server_info->utok.gid,
941 conn->server_info->sanitized_username,
942 pdb_get_domain(conn->server_info->sam_account),
943 lp_rootpreexec(snum));
944 DEBUG(5,("cmd=%s\n",cmd));
945 ret = smbrun(cmd,NULL);
946 TALLOC_FREE(cmd);
947 if (ret != 0 && lp_rootpreexec_close(snum)) {
948 DEBUG(1,("root preexec gave %d - failing "
949 "connection\n", ret));
950 SMB_VFS_DISCONNECT(conn);
951 yield_connection(conn, lp_servicename(snum));
952 conn_free(conn);
953 *pstatus = NT_STATUS_ACCESS_DENIED;
954 return NULL;
955 }
956 }
957
958/* USER Activites: */
959 if (!change_to_user(conn, conn->vuid)) {
960 /* No point continuing if they fail the basic checks */
961 DEBUG(0,("Can't become connected user!\n"));
962 SMB_VFS_DISCONNECT(conn);
963 yield_connection(conn, lp_servicename(snum));
964 conn_free(conn);
965 *pstatus = NT_STATUS_LOGON_FAILURE;
966 return NULL;
967 }
968
969 /* Remember that a different vuid can connect later without these
970 * checks... */
971
972 /* Preexecs are done here as they might make the dir we are to ChDir
973 * to below */
974
975 /* execute any "preexec = " line */
976 if (*lp_preexec(snum)) {
977 char *cmd = talloc_sub_advanced(talloc_tos(),
978 lp_servicename(SNUM(conn)),
979 conn->server_info->unix_name,
980 conn->connectpath,
981 conn->server_info->utok.gid,
982 conn->server_info->sanitized_username,
983 pdb_get_domain(conn->server_info->sam_account),
984 lp_preexec(snum));
985 ret = smbrun(cmd,NULL);
986 TALLOC_FREE(cmd);
987 if (ret != 0 && lp_preexec_close(snum)) {
988 DEBUG(1,("preexec gave %d - failing connection\n",
989 ret));
990 *pstatus = NT_STATUS_ACCESS_DENIED;
991 goto err_root_exit;
992 }
993 }
994
995 /*
996 * If widelinks are disallowed we need to canonicalise the connect
997 * path here to ensure we don't have any symlinks in the
998 * connectpath. We will be checking all paths on this connection are
999 * below this directory. We must do this after the VFS init as we
1000 * depend on the realpath() pointer in the vfs table. JRA.
1001 */
1002 if (!lp_widelinks(snum)) {
1003 if (!canonicalize_connect_path(conn)) {
1004 DEBUG(0, ("canonicalize_connect_path failed "
1005 "for service %s, path %s\n",
1006 lp_servicename(snum),
1007 conn->connectpath));
1008 *pstatus = NT_STATUS_BAD_NETWORK_NAME;
1009 goto err_root_exit;
1010 }
1011 }
1012
1013#ifdef WITH_FAKE_KASERVER
1014 if (lp_afs_share(snum)) {
1015 afs_login(conn);
1016 }
1017#endif
1018
1019 /* Add veto/hide lists */
1020 if (!IS_IPC(conn) && !IS_PRINT(conn)) {
1021 set_namearray( &conn->veto_list, lp_veto_files(snum));
1022 set_namearray( &conn->hide_list, lp_hide_files(snum));
1023 set_namearray( &conn->veto_oplock_list, lp_veto_oplocks(snum));
1024 set_namearray( &conn->aio_write_behind_list,
1025 lp_aio_write_behind(snum));
1026 }
1027
1028 status = create_synthetic_smb_fname(talloc_tos(), conn->connectpath,
1029 NULL, NULL, &smb_fname_cpath);
1030 if (!NT_STATUS_IS_OK(status)) {
1031 *pstatus = status;
1032 goto err_root_exit;
1033 }
1034
1035 /* win2000 does not check the permissions on the directory
1036 during the tree connect, instead relying on permission
1037 check during individual operations. To match this behaviour
1038 I have disabled this chdir check (tridge) */
1039 /* the alternative is just to check the directory exists */
1040 if ((ret = SMB_VFS_STAT(conn, smb_fname_cpath)) != 0 ||
1041 !S_ISDIR(smb_fname_cpath->st.st_ex_mode)) {
1042 if (ret == 0 && !S_ISDIR(smb_fname_cpath->st.st_ex_mode)) {
1043 DEBUG(0,("'%s' is not a directory, when connecting to "
1044 "[%s]\n", conn->connectpath,
1045 lp_servicename(snum)));
1046 } else {
1047 DEBUG(0,("'%s' does not exist or permission denied "
1048 "when connecting to [%s] Error was %s\n",
1049 conn->connectpath, lp_servicename(snum),
1050 strerror(errno) ));
1051 }
1052 *pstatus = NT_STATUS_BAD_NETWORK_NAME;
1053 goto err_root_exit;
1054 }
1055 conn->base_share_dev = smb_fname_cpath->st.st_ex_dev;
1056
1057 string_set(&conn->origpath,conn->connectpath);
1058
1059#if SOFTLINK_OPTIMISATION
1060 /* resolve any soft links early if possible */
1061 if (vfs_ChDir(conn,conn->connectpath) == 0) {
1062 TALLOC_CTX *ctx = talloc_tos();
1063 char *s = vfs_GetWd(ctx,s);
1064 if (!s) {
1065 *status = map_nt_error_from_unix(errno);
1066 goto err_root_exit;
1067 }
1068 if (!set_conn_connectpath(conn,s)) {
1069 *status = NT_STATUS_NO_MEMORY;
1070 goto err_root_exit;
1071 }
1072 vfs_ChDir(conn,conn->connectpath);
1073 }
1074#endif
1075
1076 /* Figure out the characteristics of the underlying filesystem. This
1077 * assumes that all the filesystem mounted withing a share path have
1078 * the same characteristics, which is likely but not guaranteed.
1079 */
1080
1081 conn->fs_capabilities = SMB_VFS_FS_CAPABILITIES(conn, &conn->ts_res);
1082
1083 /*
1084 * Print out the 'connected as' stuff here as we need
1085 * to know the effective uid and gid we will be using
1086 * (at least initially).
1087 */
1088
1089 if( DEBUGLVL( IS_IPC(conn) ? 3 : 1 ) ) {
1090 dbgtext( "%s (%s) ", get_remote_machine_name(),
1091 conn->client_address );
1092 dbgtext( "%s", srv_is_signing_active(smbd_server_conn) ? "signed " : "");
1093 dbgtext( "connect to service %s ", lp_servicename(snum) );
1094 dbgtext( "initially as user %s ",
1095 conn->server_info->unix_name );
1096 dbgtext( "(uid=%d, gid=%d) ", (int)geteuid(), (int)getegid() );
1097 dbgtext( "(pid %d)\n", (int)sys_getpid() );
1098 }
1099
1100 /* we've finished with the user stuff - go back to root */
1101 change_to_root_user();
1102 return(conn);
1103
1104 err_root_exit:
1105 TALLOC_FREE(smb_fname_cpath);
1106 change_to_root_user();
1107 /* Call VFS disconnect hook */
1108 SMB_VFS_DISCONNECT(conn);
1109 yield_connection(conn, lp_servicename(snum));
1110 conn_free(conn);
1111 return NULL;
1112}
1113
1114/****************************************************************************
1115 Make a connection to a service.
1116 *
1117 * @param service
1118****************************************************************************/
1119
1120connection_struct *make_connection(struct smbd_server_connection *sconn,
1121 const char *service_in, DATA_BLOB password,
1122 const char *pdev, uint16 vuid,
1123 NTSTATUS *status)
1124{
1125 uid_t euid;
1126 user_struct *vuser = NULL;
1127 fstring service;
1128 fstring dev;
1129 int snum = -1;
1130 char addr[INET6_ADDRSTRLEN];
1131
1132 fstrcpy(dev, pdev);
1133
1134 /* This must ONLY BE CALLED AS ROOT. As it exits this function as
1135 * root. */
1136 if (!non_root_mode() && (euid = geteuid()) != 0) {
1137 DEBUG(0,("make_connection: PANIC ERROR. Called as nonroot "
1138 "(%u)\n", (unsigned int)euid ));
1139 smb_panic("make_connection: PANIC ERROR. Called as nonroot\n");
1140 }
1141
1142 if (conn_num_open(sconn) > 2047) {
1143 *status = NT_STATUS_INSUFF_SERVER_RESOURCES;
1144 return NULL;
1145 }
1146
1147 if(lp_security() != SEC_SHARE) {
1148 vuser = get_valid_user_struct(sconn, vuid);
1149 if (!vuser) {
1150 DEBUG(1,("make_connection: refusing to connect with "
1151 "no session setup\n"));
1152 *status = NT_STATUS_ACCESS_DENIED;
1153 return NULL;
1154 }
1155 }
1156
1157 /* Logic to try and connect to the correct [homes] share, preferably
1158 without too many getpwnam() lookups. This is particulary nasty for
1159 winbind usernames, where the share name isn't the same as unix
1160 username.
1161
1162 The snum of the homes share is stored on the vuser at session setup
1163 time.
1164 */
1165
1166 if (strequal(service_in,HOMES_NAME)) {
1167 if(lp_security() != SEC_SHARE) {
1168 DATA_BLOB no_pw = data_blob_null;
1169 if (vuser->homes_snum == -1) {
1170 DEBUG(2, ("[homes] share not available for "
1171 "this user because it was not found "
1172 "or created at session setup "
1173 "time\n"));
1174 *status = NT_STATUS_BAD_NETWORK_NAME;
1175 return NULL;
1176 }
1177 DEBUG(5, ("making a connection to [homes] service "
1178 "created at session setup time\n"));
1179 return make_connection_snum(sconn,
1180 vuser->homes_snum,
1181 vuser, no_pw,
1182 dev, status);
1183 } else {
1184 /* Security = share. Try with
1185 * current_user_info.smb_name as the username. */
1186 if (*current_user_info.smb_name) {
1187 fstring unix_username;
1188 fstrcpy(unix_username,
1189 current_user_info.smb_name);
1190 map_username(sconn, unix_username);
1191 snum = find_service(unix_username, unix_username);
1192 }
1193 if (snum != -1) {
1194 DEBUG(5, ("making a connection to 'homes' "
1195 "service %s based on "
1196 "security=share\n", service_in));
1197 return make_connection_snum(sconn,
1198 snum, NULL,
1199 password,
1200 dev, status);
1201 }
1202 }
1203 } else if ((lp_security() != SEC_SHARE) && (vuser->homes_snum != -1)
1204 && strequal(service_in,
1205 lp_servicename(vuser->homes_snum))) {
1206 DATA_BLOB no_pw = data_blob_null;
1207 DEBUG(5, ("making a connection to 'homes' service [%s] "
1208 "created at session setup time\n", service_in));
1209 return make_connection_snum(sconn,
1210 vuser->homes_snum,
1211 vuser, no_pw,
1212 dev, status);
1213 }
1214
1215 fstrcpy(service, service_in);
1216
1217 strlower_m(service);
1218
1219 snum = find_service(service, service);
1220
1221 if (snum < 0) {
1222 if (strequal(service,"IPC$") ||
1223 (lp_enable_asu_support() && strequal(service,"ADMIN$"))) {
1224 DEBUG(3,("refusing IPC connection to %s\n", service));
1225 *status = NT_STATUS_ACCESS_DENIED;
1226 return NULL;
1227 }
1228
1229 DEBUG(3,("%s (%s) couldn't find service %s\n",
1230 get_remote_machine_name(),
1231 client_addr(get_client_fd(),addr,sizeof(addr)),
1232 service));
1233 *status = NT_STATUS_BAD_NETWORK_NAME;
1234 return NULL;
1235 }
1236
1237 /* Handle non-Dfs clients attempting connections to msdfs proxy */
1238 if (lp_host_msdfs() && (*lp_msdfs_proxy(snum) != '\0')) {
1239 DEBUG(3, ("refusing connection to dfs proxy share '%s' "
1240 "(pointing to %s)\n",
1241 service, lp_msdfs_proxy(snum)));
1242 *status = NT_STATUS_BAD_NETWORK_NAME;
1243 return NULL;
1244 }
1245
1246 DEBUG(5, ("making a connection to 'normal' service %s\n", service));
1247
1248 return make_connection_snum(sconn, snum, vuser,
1249 password,
1250 dev, status);
1251}
1252
1253/****************************************************************************
1254 Close a cnum.
1255****************************************************************************/
1256
1257void close_cnum(connection_struct *conn, uint16 vuid)
1258{
1259 file_close_conn(conn);
1260
1261 if (!IS_IPC(conn)) {
1262 dptr_closecnum(conn);
1263 }
1264
1265 change_to_root_user();
1266
1267 DEBUG(IS_IPC(conn)?3:1, ("%s (%s) closed connection to service %s\n",
1268 get_remote_machine_name(),
1269 conn->client_address,
1270 lp_servicename(SNUM(conn))));
1271
1272 /* Call VFS disconnect hook */
1273 SMB_VFS_DISCONNECT(conn);
1274
1275 yield_connection(conn, lp_servicename(SNUM(conn)));
1276
1277 /* make sure we leave the directory available for unmount */
1278 vfs_ChDir(conn, "/");
1279
1280 /* execute any "postexec = " line */
1281 if (*lp_postexec(SNUM(conn)) &&
1282 change_to_user(conn, vuid)) {
1283 char *cmd = talloc_sub_advanced(talloc_tos(),
1284 lp_servicename(SNUM(conn)),
1285 conn->server_info->unix_name,
1286 conn->connectpath,
1287 conn->server_info->utok.gid,
1288 conn->server_info->sanitized_username,
1289 pdb_get_domain(conn->server_info->sam_account),
1290 lp_postexec(SNUM(conn)));
1291 smbrun(cmd,NULL);
1292 TALLOC_FREE(cmd);
1293 change_to_root_user();
1294 }
1295
1296 change_to_root_user();
1297 /* execute any "root postexec = " line */
1298 if (*lp_rootpostexec(SNUM(conn))) {
1299 char *cmd = talloc_sub_advanced(talloc_tos(),
1300 lp_servicename(SNUM(conn)),
1301 conn->server_info->unix_name,
1302 conn->connectpath,
1303 conn->server_info->utok.gid,
1304 conn->server_info->sanitized_username,
1305 pdb_get_domain(conn->server_info->sam_account),
1306 lp_rootpostexec(SNUM(conn)));
1307 smbrun(cmd,NULL);
1308 TALLOC_FREE(cmd);
1309 }
1310
1311 conn_free(conn);
1312}
Note: See TracBrowser for help on using the repository browser.