2 Unix SMB/CIFS implementation.
3 service (connection) opening and closing
4 Copyright (C) Andrew Tridgell 1992-1998
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.
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.
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/>.
21 #include "system/filesys.h"
22 #include "smbd/smbd.h"
23 #include "smbd/globals.h"
24 #include "../librpc/gen_ndr/netlogon.h"
25 #include "../libcli/security/security.h"
26 #include "printing/pcap.h"
27 #include "passdb/lookup_sid.h"
29 extern userdom_struct current_user_info;
31 static bool canonicalize_connect_path(connection_struct *conn)
34 char *resolved_name = SMB_VFS_REALPATH(conn,conn->connectpath);
38 ret = set_conn_connectpath(conn,resolved_name);
39 SAFE_FREE(resolved_name);
43 /****************************************************************************
44 Ensure when setting connectpath it is a canonicalized (no ./ // or ../)
45 absolute path stating in / and not ending in /.
46 Observent people will notice a similarity between this and check_path_syntax :-).
47 ****************************************************************************/
49 bool set_conn_connectpath(connection_struct *conn, const char *connectpath)
53 const char *s = connectpath;
54 bool start_of_name_component = true;
56 if (connectpath == NULL || connectpath[0] == '\0') {
60 /* Allocate for strlen + '\0' + possible leading '/' */
61 destname = (char *)SMB_MALLOC(strlen(connectpath) + 2);
67 *d++ = '/'; /* Always start with root. */
71 /* Eat multiple '/' */
75 if ((d > destname + 1) && (*s != '\0')) {
78 start_of_name_component = True;
82 if (start_of_name_component) {
83 if ((s[0] == '.') && (s[1] == '.') && (s[2] == '/' || s[2] == '\0')) {
84 /* Uh oh - "/../" or "/..\0" ! */
86 /* Go past the ../ or .. */
90 s += 2; /* Go past the .. */
93 /* If we just added a '/' - delete it */
94 if ((d > destname) && (*(d-1) == '/')) {
99 /* Are we at the start ? Can't go back further if so. */
101 *d++ = '/'; /* Can't delete root */
104 /* Go back one level... */
105 /* Decrement d first as d points to the *next* char to write into. */
106 for (d--; d > destname; d--) {
111 /* We're still at the start of a name component, just the previous one. */
113 } else if ((s[0] == '.') && ((s[1] == '\0') || s[1] == '/')) {
114 /* Component of pathname can't be "." only - skip the '.' . */
128 /* Get the size of the next MB character. */
129 next_codepoint(s,&siz);
150 start_of_name_component = false;
154 /* And must not end in '/' */
155 if (d > destname + 1 && (*(d-1) == '/')) {
159 DEBUG(10,("set_conn_connectpath: service %s, connectpath = %s\n",
160 lp_servicename(SNUM(conn)), destname ));
162 string_set(&conn->connectpath, destname);
167 /****************************************************************************
168 Load parameters specific to a connection/service.
169 ****************************************************************************/
171 bool set_current_service(connection_struct *conn, uint16 flags, bool do_chdir)
180 conn->lastused_count++;
185 vfs_ChDir(conn,conn->connectpath) != 0 &&
186 vfs_ChDir(conn,conn->origpath) != 0) {
187 DEBUG(((errno!=EACCES)?0:3),("chdir (%s) failed, reason: %s\n",
188 conn->connectpath, strerror(errno)));
192 if ((conn == last_conn) && (last_flags == flags)) {
199 /* Obey the client case sensitivity requests - only for clients that support it. */
200 switch (lp_casesensitive(snum)) {
203 /* We need this uglyness due to DOS/Win9x clients that lie about case insensitivity. */
204 enum remote_arch_types ra_type = get_remote_arch();
205 if ((ra_type != RA_SAMBA) && (ra_type != RA_CIFSFS)) {
206 /* Client can't support per-packet case sensitive pathnames. */
207 conn->case_sensitive = False;
209 conn->case_sensitive = !(flags & FLAG_CASELESS_PATHNAMES);
214 conn->case_sensitive = True;
217 conn->case_sensitive = False;
223 static int load_registry_service(const char *servicename)
225 if (!lp_registry_shares()) {
229 if ((servicename == NULL) || (*servicename == '\0')) {
233 if (strequal(servicename, GLOBAL_NAME)) {
237 if (!process_registry_service(servicename)) {
241 return lp_servicenumber(servicename);
244 void load_registry_shares(void)
246 DEBUG(8, ("load_registry_shares()\n"));
247 if (!lp_registry_shares()) {
251 process_registry_shares();
256 /****************************************************************************
257 Add a home service. Returns the new service number or -1 if fail.
258 ****************************************************************************/
260 int add_home_service(const char *service, const char *username, const char *homedir)
264 if (!service || !homedir || homedir[0] == '\0')
267 if ((iHomeService = lp_servicenumber(HOMES_NAME)) < 0) {
268 if ((iHomeService = load_registry_service(HOMES_NAME)) < 0) {
274 * If this is a winbindd provided username, remove
275 * the domain component before adding the service.
276 * Log a warning if the "path=" parameter does not
277 * include any macros.
281 const char *p = strchr(service,*lp_winbind_separator());
283 /* We only want the 'user' part of the string */
289 if (!lp_add_home(service, iHomeService, username, homedir)) {
293 return lp_servicenumber(service);
298 * Find a service entry.
300 * @param service is modified (to canonical form??)
303 int find_service(TALLOC_CTX *ctx, const char *service_in, char **p_service_out)
311 /* First make a copy. */
312 *p_service_out = talloc_strdup(ctx, service_in);
313 if (!*p_service_out) {
317 all_string_sub(*p_service_out,"\\","/",0);
319 iService = lp_servicenumber(*p_service_out);
321 /* now handle the special case of a home directory */
323 char *phome_dir = get_user_home_dir(ctx, *p_service_out);
327 * Try mapping the servicename, it may
328 * be a Windows to unix mapped user name.
330 if(map_username(ctx, *p_service_out, p_service_out)) {
331 if (*p_service_out == NULL) {
335 phome_dir = get_user_home_dir(
336 ctx, *p_service_out);
340 DEBUG(3,("checking for home directory %s gave %s\n",*p_service_out,
341 phome_dir?phome_dir:"(NULL)"));
343 iService = add_home_service(*p_service_out,*p_service_out /* 'username' */, phome_dir);
346 /* If we still don't have a service, attempt to add it as a printer. */
350 if ((iPrinterService = lp_servicenumber(PRINTERS_NAME)) < 0) {
351 iPrinterService = load_registry_service(PRINTERS_NAME);
353 if (iPrinterService >= 0) {
354 DEBUG(3,("checking whether %s is a valid printer name...\n",
356 if (pcap_printername_ok(*p_service_out)) {
357 DEBUG(3,("%s is a valid printer name\n",
359 DEBUG(3,("adding %s as a printer service\n",
361 lp_add_printer(*p_service_out, iPrinterService);
362 iService = lp_servicenumber(*p_service_out);
364 DEBUG(0,("failed to add %s as a printer service!\n",
368 DEBUG(3,("%s is not a valid printer name\n",
374 /* Check for default vfs service? Unsure whether to implement this */
379 iService = load_registry_service(*p_service_out);
382 /* Is it a usershare service ? */
383 if (iService < 0 && *lp_usershare_path()) {
384 /* Ensure the name is canonicalized. */
385 strlower_m(*p_service_out);
386 iService = load_usershare_service(*p_service_out);
389 /* just possibly it's a default service? */
391 char *pdefservice = lp_defaultservice();
394 !strequal(pdefservice, *p_service_out)
395 && !strstr_m(*p_service_out,"..")) {
397 * We need to do a local copy here as lp_defaultservice()
398 * returns one of the rotating lp_string buffers that
399 * could get overwritten by the recursive find_service() call
400 * below. Fix from Josef Hinteregger <joehtg@joehtg.co.at>.
402 char *defservice = talloc_strdup(ctx, pdefservice);
408 /* Disallow anything except explicit share names. */
409 if (strequal(defservice,HOMES_NAME) ||
410 strequal(defservice, PRINTERS_NAME) ||
411 strequal(defservice, "IPC$")) {
412 TALLOC_FREE(defservice);
416 iService = find_service(ctx, defservice, p_service_out);
417 if (!*p_service_out) {
418 TALLOC_FREE(defservice);
423 all_string_sub(*p_service_out, "_","/",0);
424 iService = lp_add_service(*p_service_out, iService);
426 TALLOC_FREE(defservice);
431 if (!VALID_SNUM(iService)) {
432 DEBUG(0,("Invalid snum %d for %s\n",iService,
441 DEBUG(3,("find_service() failed to find service %s\n",
449 /****************************************************************************
450 do some basic sainity checks on the share.
451 This function modifies dev, ecode.
452 ****************************************************************************/
454 static NTSTATUS share_sanity_checks(struct client_address *client_id, int snum,
457 if (!lp_snum_ok(snum) ||
458 !allow_access(lp_hostsdeny(snum), lp_hostsallow(snum),
459 client_id->name, client_id->addr)) {
460 return NT_STATUS_ACCESS_DENIED;
463 if (dev[0] == '?' || !dev[0]) {
464 if (lp_print_ok(snum)) {
465 fstrcpy(dev,"LPT1:");
466 } else if (strequal(lp_fstype(snum), "IPC")) {
475 if (lp_print_ok(snum)) {
476 if (!strequal(dev, "LPT1:")) {
477 return NT_STATUS_BAD_DEVICE_TYPE;
479 } else if (strequal(lp_fstype(snum), "IPC")) {
480 if (!strequal(dev, "IPC")) {
481 return NT_STATUS_BAD_DEVICE_TYPE;
483 } else if (!strequal(dev, "A:")) {
484 return NT_STATUS_BAD_DEVICE_TYPE;
487 /* Behave as a printer if we are supposed to */
488 if (lp_print_ok(snum) && (strcmp(dev, "A:") == 0)) {
489 fstrcpy(dev, "LPT1:");
496 * Go through lookup_name etc to find the force'd group.
498 * Create a new token from src_token, replacing the primary group sid with the
502 static NTSTATUS find_forced_group(bool force_user,
503 int snum, const char *username,
504 struct dom_sid *pgroup_sid,
507 NTSTATUS result = NT_STATUS_NO_SUCH_GROUP;
508 TALLOC_CTX *frame = talloc_stackframe();
509 struct dom_sid group_sid;
510 enum lsa_SidType type;
512 bool user_must_be_member = False;
515 groupname = talloc_strdup(talloc_tos(), lp_force_group(snum));
516 if (groupname == NULL) {
517 DEBUG(1, ("talloc_strdup failed\n"));
518 result = NT_STATUS_NO_MEMORY;
522 if (groupname[0] == '+') {
523 user_must_be_member = True;
527 groupname = talloc_string_sub(talloc_tos(), groupname,
528 "%S", lp_servicename(snum));
529 if (groupname == NULL) {
530 DEBUG(1, ("talloc_string_sub failed\n"));
531 result = NT_STATUS_NO_MEMORY;
535 if (!lookup_name_smbconf(talloc_tos(), groupname,
536 LOOKUP_NAME_ALL|LOOKUP_NAME_GROUP,
537 NULL, NULL, &group_sid, &type)) {
538 DEBUG(10, ("lookup_name_smbconf(%s) failed\n",
543 if ((type != SID_NAME_DOM_GRP) && (type != SID_NAME_ALIAS) &&
544 (type != SID_NAME_WKN_GRP)) {
545 DEBUG(10, ("%s is a %s, not a group\n", groupname,
546 sid_type_lookup(type)));
550 if (!sid_to_gid(&group_sid, &gid)) {
551 DEBUG(10, ("sid_to_gid(%s) for %s failed\n",
552 sid_string_dbg(&group_sid), groupname));
557 * If the user has been forced and the forced group starts with a '+',
558 * then we only set the group to be the forced group if the forced
559 * user is a member of that group. Otherwise, the meaning of the '+'
563 if (force_user && user_must_be_member) {
564 if (user_in_group_sid(username, &group_sid)) {
565 sid_copy(pgroup_sid, &group_sid);
567 DEBUG(3,("Forced group %s for member %s\n",
568 groupname, username));
570 DEBUG(0,("find_forced_group: forced user %s is not a member "
571 "of forced group %s. Disallowing access.\n",
572 username, groupname ));
573 result = NT_STATUS_MEMBER_NOT_IN_GROUP;
577 sid_copy(pgroup_sid, &group_sid);
579 DEBUG(3,("Forced group %s\n", groupname));
582 result = NT_STATUS_OK;
588 /****************************************************************************
589 Create an auth_serversupplied_info structure for a connection_struct
590 ****************************************************************************/
592 static NTSTATUS create_connection_session_info(struct smbd_server_connection *sconn,
593 TALLOC_CTX *mem_ctx, int snum,
594 struct auth_serversupplied_info *vuid_serverinfo,
596 struct auth_serversupplied_info **presult)
598 if (lp_guest_only(snum)) {
599 return make_server_info_guest(mem_ctx, presult);
602 if (vuid_serverinfo != NULL) {
604 struct auth_serversupplied_info *result;
607 * This is the normal security != share case where we have a
608 * valid vuid from the session setup. */
610 if (vuid_serverinfo->guest) {
611 if (!lp_guest_ok(snum)) {
612 DEBUG(2, ("guest user (from session setup) "
613 "not permitted to access this share "
614 "(%s)\n", lp_servicename(snum)));
615 return NT_STATUS_ACCESS_DENIED;
618 if (!user_ok_token(vuid_serverinfo->unix_name,
619 vuid_serverinfo->info3->base.domain.string,
620 vuid_serverinfo->security_token, snum)) {
621 DEBUG(2, ("user '%s' (from session setup) not "
622 "permitted to access this share "
624 vuid_serverinfo->unix_name,
625 lp_servicename(snum)));
626 return NT_STATUS_ACCESS_DENIED;
630 result = copy_serverinfo(mem_ctx, vuid_serverinfo);
631 if (result == NULL) {
632 return NT_STATUS_NO_MEMORY;
639 if (lp_security() == SEC_SHARE) {
644 /* add the sharename as a possible user name if we
645 are in share mode security */
647 add_session_user(sconn, lp_servicename(snum));
649 /* shall we let them in? */
651 if (!authorise_login(sconn, snum,user,password,&guest)) {
652 DEBUG( 2, ( "Invalid username/password for [%s]\n",
653 lp_servicename(snum)) );
654 return NT_STATUS_WRONG_PASSWORD;
657 return make_serverinfo_from_username(mem_ctx, user, guest,
661 DEBUG(0, ("invalid VUID (vuser) but not in security=share\n"));
662 return NT_STATUS_ACCESS_DENIED;
665 /****************************************************************************
666 set relavent user and group settings corresponding to force user/group
667 configuration for the given snum.
668 ****************************************************************************/
670 NTSTATUS set_conn_force_user_group(connection_struct *conn, int snum)
674 if (*lp_force_user(snum)) {
677 * Replace conn->session_info with a completely faked up one
678 * from the username we are forced into :-)
682 struct auth_serversupplied_info *forced_serverinfo;
684 fuser = talloc_string_sub(conn, lp_force_user(snum), "%S",
685 lp_const_servicename(snum));
687 return NT_STATUS_NO_MEMORY;
690 status = make_serverinfo_from_username(
691 conn, fuser, conn->session_info->guest,
693 if (!NT_STATUS_IS_OK(status)) {
697 TALLOC_FREE(conn->session_info);
698 conn->session_info = forced_serverinfo;
700 conn->force_user = true;
701 DEBUG(3,("Forced user %s\n", fuser));
705 * If force group is true, then override
706 * any groupid stored for the connecting user.
709 if (*lp_force_group(snum)) {
711 status = find_forced_group(
712 conn->force_user, snum, conn->session_info->unix_name,
713 &conn->session_info->security_token->sids[1],
714 &conn->session_info->utok.gid);
716 if (!NT_STATUS_IS_OK(status)) {
721 * We need to cache this gid, to use within
722 * change_to_user() separately from the conn->session_info
723 * struct. We only use conn->session_info directly if
724 * "force_user" was set.
726 conn->force_group_gid = conn->session_info->utok.gid;
732 /****************************************************************************
733 Make a connection, given the snum to connect to, and the vuser of the
734 connecting user if appropriate.
735 ****************************************************************************/
737 connection_struct *make_connection_snum(struct smbd_server_connection *sconn,
738 int snum, user_struct *vuser,
743 connection_struct *conn = NULL;
744 struct smb_filename *smb_fname_cpath = NULL;
747 bool on_err_call_dis_hook = false;
748 bool claimed_connection = false;
755 *pstatus = share_sanity_checks(&sconn->client_id, snum, dev);
756 if (NT_STATUS_IS_ERR(*pstatus)) {
760 conn = conn_new(sconn);
762 DEBUG(0,("Couldn't find free connection.\n"));
763 *pstatus = NT_STATUS_INSUFFICIENT_RESOURCES;
767 conn->params->service = snum;
769 status = create_connection_session_info(sconn,
770 conn, snum, vuser ? vuser->session_info : NULL, password,
771 &conn->session_info);
773 if (!NT_STATUS_IS_OK(status)) {
774 DEBUG(1, ("create_connection_session_info failed: %s\n",
780 if ((lp_guest_only(snum)) || (lp_security() == SEC_SHARE)) {
781 conn->force_user = true;
784 add_session_user(sconn, conn->session_info->unix_name);
786 conn->num_files_open = 0;
787 conn->lastused = conn->lastused_count = time(NULL);
789 conn->printer = (strncmp(dev,"LPT",3) == 0);
790 conn->ipc = ( (strncmp(dev,"IPC",3) == 0) ||
791 ( lp_enable_asu_support() && strequal(dev,"ADMIN$")) );
793 /* Case options for the share. */
794 if (lp_casesensitive(snum) == Auto) {
795 /* We will be setting this per packet. Set to be case
796 * insensitive for now. */
797 conn->case_sensitive = False;
799 conn->case_sensitive = (bool)lp_casesensitive(snum);
802 conn->case_preserve = lp_preservecase(snum);
803 conn->short_case_preserve = lp_shortpreservecase(snum);
805 conn->encrypt_level = lp_smb_encrypt(snum);
807 conn->veto_list = NULL;
808 conn->hide_list = NULL;
809 conn->veto_oplock_list = NULL;
810 conn->aio_write_behind_list = NULL;
812 conn->read_only = lp_readonly(SNUM(conn));
814 status = set_conn_force_user_group(conn, snum);
815 if (!NT_STATUS_IS_OK(status)) {
821 conn->vuid = (vuser != NULL) ? vuser->vuid : UID_FIELD_INVALID;
824 char *s = talloc_sub_advanced(talloc_tos(),
825 lp_servicename(SNUM(conn)),
826 conn->session_info->unix_name,
828 conn->session_info->utok.gid,
829 conn->session_info->sanitized_username,
830 conn->session_info->info3->base.domain.string,
833 *pstatus = NT_STATUS_NO_MEMORY;
837 if (!set_conn_connectpath(conn,s)) {
839 *pstatus = NT_STATUS_NO_MEMORY;
842 DEBUG(3,("Connect path is '%s' for service [%s]\n",s,
843 lp_servicename(snum)));
848 * New code to check if there's a share security descripter
849 * added from NT server manager. This is done after the
850 * smb.conf checks are done as we need a uid and token. JRA.
855 bool can_write = False;
857 can_write = share_access_check(conn->session_info->security_token,
858 lp_servicename(snum),
862 if (!share_access_check(conn->session_info->security_token,
863 lp_servicename(snum),
865 /* No access, read or write. */
866 DEBUG(0,("make_connection: connection to %s "
867 "denied due to security "
869 lp_servicename(snum)));
870 *pstatus = NT_STATUS_ACCESS_DENIED;
873 conn->read_only = True;
877 /* Initialise VFS function pointers */
879 if (!smbd_vfs_init(conn)) {
880 DEBUG(0, ("vfs_init failed for service %s\n",
881 lp_servicename(snum)));
882 *pstatus = NT_STATUS_BAD_NETWORK_NAME;
886 /* ROOT Activities: */
887 /* explicitly check widelinks here so that we can correctly warn
889 widelinks_warning(snum);
892 * Enforce the max connections parameter.
895 if ((lp_max_connections(snum) > 0)
896 && (count_current_connections(lp_servicename(SNUM(conn)), True) >=
897 lp_max_connections(snum))) {
899 DEBUG(1, ("Max connections (%d) exceeded for %s\n",
900 lp_max_connections(snum), lp_servicename(snum)));
901 *pstatus = NT_STATUS_INSUFFICIENT_RESOURCES;
906 * Get us an entry in the connections db
908 if (!claim_connection(conn, lp_servicename(snum))) {
909 DEBUG(1, ("Could not store connections entry\n"));
910 *pstatus = NT_STATUS_INTERNAL_DB_ERROR;
913 claimed_connection = true;
915 /* Invoke VFS make connection hook - this must be the first
916 filesystem operation that we do. */
918 if (SMB_VFS_CONNECT(conn, lp_servicename(snum),
919 conn->session_info->unix_name) < 0) {
920 DEBUG(0,("make_connection: VFS make connection failed!\n"));
921 *pstatus = NT_STATUS_UNSUCCESSFUL;
925 /* Any error exit after here needs to call the disconnect hook. */
926 on_err_call_dis_hook = true;
928 if ((!conn->printer) && (!conn->ipc)) {
929 conn->notify_ctx = notify_init(conn,
930 sconn_server_id(sconn),
932 smbd_event_context(),
937 * Fix compatibility issue pointed out by Volker.
938 * We pass the conn->connectpath to the preexec
939 * scripts as a parameter, so attempt to canonicalize
940 * it here before calling the preexec scripts.
941 * We ignore errors here, as it is possible that
942 * the conn->connectpath doesn't exist yet and
943 * the preexec scripts will create them.
946 (void)canonicalize_connect_path(conn);
948 /* Preexecs are done here as they might make the dir we are to ChDir
950 /* execute any "root preexec = " line */
951 if (*lp_rootpreexec(snum)) {
952 char *cmd = talloc_sub_advanced(talloc_tos(),
953 lp_servicename(SNUM(conn)),
954 conn->session_info->unix_name,
956 conn->session_info->utok.gid,
957 conn->session_info->sanitized_username,
958 conn->session_info->info3->base.domain.string,
959 lp_rootpreexec(snum));
960 DEBUG(5,("cmd=%s\n",cmd));
961 ret = smbrun(cmd,NULL);
963 if (ret != 0 && lp_rootpreexec_close(snum)) {
964 DEBUG(1,("root preexec gave %d - failing "
965 "connection\n", ret));
966 *pstatus = NT_STATUS_ACCESS_DENIED;
971 /* USER Activites: */
972 if (!change_to_user(conn, conn->vuid)) {
973 /* No point continuing if they fail the basic checks */
974 DEBUG(0,("Can't become connected user!\n"));
975 *pstatus = NT_STATUS_LOGON_FAILURE;
982 /* Remember that a different vuid can connect later without these
985 /* Preexecs are done here as they might make the dir we are to ChDir
988 /* execute any "preexec = " line */
989 if (*lp_preexec(snum)) {
990 char *cmd = talloc_sub_advanced(talloc_tos(),
991 lp_servicename(SNUM(conn)),
992 conn->session_info->unix_name,
994 conn->session_info->utok.gid,
995 conn->session_info->sanitized_username,
996 conn->session_info->info3->base.domain.string,
998 ret = smbrun(cmd,NULL);
1000 if (ret != 0 && lp_preexec_close(snum)) {
1001 DEBUG(1,("preexec gave %d - failing connection\n",
1003 *pstatus = NT_STATUS_ACCESS_DENIED;
1008 #ifdef WITH_FAKE_KASERVER
1009 if (lp_afs_share(snum)) {
1015 * we've finished with the user stuff - go back to root
1016 * so the SMB_VFS_STAT call will only fail on path errors,
1017 * not permission problems.
1019 change_to_root_user();
1020 /* ROOT Activites: */
1023 * If widelinks are disallowed we need to canonicalise the connect
1024 * path here to ensure we don't have any symlinks in the
1025 * connectpath. We will be checking all paths on this connection are
1026 * below this directory. We must do this after the VFS init as we
1027 * depend on the realpath() pointer in the vfs table. JRA.
1029 if (!lp_widelinks(snum)) {
1030 if (!canonicalize_connect_path(conn)) {
1031 DEBUG(0, ("canonicalize_connect_path failed "
1032 "for service %s, path %s\n",
1033 lp_servicename(snum),
1034 conn->connectpath));
1035 *pstatus = NT_STATUS_BAD_NETWORK_NAME;
1040 /* Add veto/hide lists */
1041 if (!IS_IPC(conn) && !IS_PRINT(conn)) {
1042 set_namearray( &conn->veto_list, lp_veto_files(snum));
1043 set_namearray( &conn->hide_list, lp_hide_files(snum));
1044 set_namearray( &conn->veto_oplock_list, lp_veto_oplocks(snum));
1045 set_namearray( &conn->aio_write_behind_list,
1046 lp_aio_write_behind(snum));
1048 status = create_synthetic_smb_fname(talloc_tos(), conn->connectpath,
1049 NULL, NULL, &smb_fname_cpath);
1050 if (!NT_STATUS_IS_OK(status)) {
1055 /* win2000 does not check the permissions on the directory
1056 during the tree connect, instead relying on permission
1057 check during individual operations. To match this behaviour
1058 I have disabled this chdir check (tridge) */
1059 /* the alternative is just to check the directory exists */
1061 if ((ret = SMB_VFS_STAT(conn, smb_fname_cpath)) != 0 ||
1062 !S_ISDIR(smb_fname_cpath->st.st_ex_mode)) {
1063 if (ret == 0 && !S_ISDIR(smb_fname_cpath->st.st_ex_mode)) {
1064 DEBUG(0,("'%s' is not a directory, when connecting to "
1065 "[%s]\n", conn->connectpath,
1066 lp_servicename(snum)));
1068 DEBUG(0,("'%s' does not exist or permission denied "
1069 "when connecting to [%s] Error was %s\n",
1070 conn->connectpath, lp_servicename(snum),
1073 *pstatus = NT_STATUS_BAD_NETWORK_NAME;
1076 conn->base_share_dev = smb_fname_cpath->st.st_ex_dev;
1078 string_set(&conn->origpath,conn->connectpath);
1080 /* Figure out the characteristics of the underlying filesystem. This
1081 * assumes that all the filesystem mounted withing a share path have
1082 * the same characteristics, which is likely but not guaranteed.
1085 conn->fs_capabilities = SMB_VFS_FS_CAPABILITIES(conn, &conn->ts_res);
1088 * Print out the 'connected as' stuff here as we need
1089 * to know the effective uid and gid we will be using
1090 * (at least initially).
1093 if( DEBUGLVL( IS_IPC(conn) ? 3 : 1 ) ) {
1094 dbgtext( "%s (%s) ", get_remote_machine_name(),
1095 conn->sconn->client_id.addr );
1096 dbgtext( "%s", srv_is_signing_active(sconn) ? "signed " : "");
1097 dbgtext( "connect to service %s ", lp_servicename(snum) );
1098 dbgtext( "initially as user %s ",
1099 conn->session_info->unix_name );
1100 dbgtext( "(uid=%d, gid=%d) ", (int)effuid, (int)effgid );
1101 dbgtext( "(pid %d)\n", (int)sys_getpid() );
1107 TALLOC_FREE(smb_fname_cpath);
1108 /* We must exit this function as root. */
1109 if (geteuid() != 0) {
1110 change_to_root_user();
1112 if (on_err_call_dis_hook) {
1113 /* Call VFS disconnect hook */
1114 SMB_VFS_DISCONNECT(conn);
1116 if (claimed_connection) {
1117 yield_connection(conn, lp_servicename(snum));
1125 /****************************************************************************
1126 Make a connection to a service.
1129 ****************************************************************************/
1131 connection_struct *make_connection(struct smbd_server_connection *sconn,
1132 const char *service_in, DATA_BLOB password,
1133 const char *pdev, uint16 vuid,
1137 user_struct *vuser = NULL;
1138 char *service = NULL;
1144 /* This must ONLY BE CALLED AS ROOT. As it exits this function as
1146 if (!non_root_mode() && (euid = geteuid()) != 0) {
1147 DEBUG(0,("make_connection: PANIC ERROR. Called as nonroot "
1148 "(%u)\n", (unsigned int)euid ));
1149 smb_panic("make_connection: PANIC ERROR. Called as nonroot\n");
1152 if (conn_num_open(sconn) > 2047) {
1153 *status = NT_STATUS_INSUFF_SERVER_RESOURCES;
1157 if(lp_security() != SEC_SHARE) {
1158 vuser = get_valid_user_struct(sconn, vuid);
1160 DEBUG(1,("make_connection: refusing to connect with "
1161 "no session setup\n"));
1162 *status = NT_STATUS_ACCESS_DENIED;
1167 /* Logic to try and connect to the correct [homes] share, preferably
1168 without too many getpwnam() lookups. This is particulary nasty for
1169 winbind usernames, where the share name isn't the same as unix
1172 The snum of the homes share is stored on the vuser at session setup
1176 if (strequal(service_in,HOMES_NAME)) {
1177 if(lp_security() != SEC_SHARE) {
1178 DATA_BLOB no_pw = data_blob_null;
1179 if (vuser->homes_snum == -1) {
1180 DEBUG(2, ("[homes] share not available for "
1181 "this user because it was not found "
1182 "or created at session setup "
1184 *status = NT_STATUS_BAD_NETWORK_NAME;
1187 DEBUG(5, ("making a connection to [homes] service "
1188 "created at session setup time\n"));
1189 return make_connection_snum(sconn,
1194 /* Security = share. Try with
1195 * current_user_info.smb_name as the username. */
1196 if (*current_user_info.smb_name) {
1197 char *unix_username = NULL;
1198 (void)map_username(talloc_tos(),
1199 current_user_info.smb_name,
1201 snum = find_service(talloc_tos(),
1204 if (!unix_username) {
1205 *status = NT_STATUS_NO_MEMORY;
1210 DEBUG(5, ("making a connection to 'homes' "
1211 "service %s based on "
1212 "security=share\n", service_in));
1213 return make_connection_snum(sconn,
1219 } else if ((lp_security() != SEC_SHARE) && (vuser->homes_snum != -1)
1220 && strequal(service_in,
1221 lp_servicename(vuser->homes_snum))) {
1222 DATA_BLOB no_pw = data_blob_null;
1223 DEBUG(5, ("making a connection to 'homes' service [%s] "
1224 "created at session setup time\n", service_in));
1225 return make_connection_snum(sconn,
1231 service = talloc_strdup(talloc_tos(), service_in);
1233 *status = NT_STATUS_NO_MEMORY;
1237 strlower_m(service);
1239 snum = find_service(talloc_tos(), service, &service);
1241 *status = NT_STATUS_NO_MEMORY;
1246 if (strequal(service,"IPC$") ||
1247 (lp_enable_asu_support() && strequal(service,"ADMIN$"))) {
1248 DEBUG(3,("refusing IPC connection to %s\n", service));
1249 *status = NT_STATUS_ACCESS_DENIED;
1253 DEBUG(3,("%s (%s) couldn't find service %s\n",
1254 get_remote_machine_name(),
1255 tsocket_address_string(
1256 sconn->remote_address, talloc_tos()),
1258 *status = NT_STATUS_BAD_NETWORK_NAME;
1262 /* Handle non-Dfs clients attempting connections to msdfs proxy */
1263 if (lp_host_msdfs() && (*lp_msdfs_proxy(snum) != '\0')) {
1264 DEBUG(3, ("refusing connection to dfs proxy share '%s' "
1265 "(pointing to %s)\n",
1266 service, lp_msdfs_proxy(snum)));
1267 *status = NT_STATUS_BAD_NETWORK_NAME;
1271 DEBUG(5, ("making a connection to 'normal' service %s\n", service));
1273 return make_connection_snum(sconn, snum, vuser,
1278 /****************************************************************************
1280 ****************************************************************************/
1282 void close_cnum(connection_struct *conn, uint16 vuid)
1284 file_close_conn(conn);
1286 if (!IS_IPC(conn)) {
1287 dptr_closecnum(conn);
1290 change_to_root_user();
1292 DEBUG(IS_IPC(conn)?3:1, ("%s (%s) closed connection to service %s\n",
1293 get_remote_machine_name(),
1294 conn->sconn->client_id.addr,
1295 lp_servicename(SNUM(conn))));
1297 /* Call VFS disconnect hook */
1298 SMB_VFS_DISCONNECT(conn);
1300 yield_connection(conn, lp_servicename(SNUM(conn)));
1302 /* make sure we leave the directory available for unmount */
1303 vfs_ChDir(conn, "/");
1305 /* execute any "postexec = " line */
1306 if (*lp_postexec(SNUM(conn)) &&
1307 change_to_user(conn, vuid)) {
1308 char *cmd = talloc_sub_advanced(talloc_tos(),
1309 lp_servicename(SNUM(conn)),
1310 conn->session_info->unix_name,
1312 conn->session_info->utok.gid,
1313 conn->session_info->sanitized_username,
1314 conn->session_info->info3->base.domain.string,
1315 lp_postexec(SNUM(conn)));
1318 change_to_root_user();
1321 change_to_root_user();
1322 /* execute any "root postexec = " line */
1323 if (*lp_rootpostexec(SNUM(conn))) {
1324 char *cmd = talloc_sub_advanced(talloc_tos(),
1325 lp_servicename(SNUM(conn)),
1326 conn->session_info->unix_name,
1328 conn->session_info->utok.gid,
1329 conn->session_info->sanitized_username,
1330 conn->session_info->info3->base.domain.string,
1331 lp_rootpostexec(SNUM(conn)));