Fix warning about unused label with no sendfile.
[ira/wip.git] / source3 / smbd / reply.c
index a743385f7f51fa7c454f66875ba4fd7a19ba1104..f1275b1088e25a8516f581476746bbdbe06ab845 100644 (file)
@@ -72,11 +72,16 @@ static NTSTATUS check_path_syntax_internal(char *path,
                        }
                }
 
-               if (!stream_started && *s == ':') {
+               if (!posix_path && !stream_started && *s == ':') {
                        if (*p_last_component_contains_wcard) {
                                return NT_STATUS_OBJECT_NAME_INVALID;
                        }
-                       /* stream names allow more characters than file names */
+                       /* Stream names allow more characters than file names.
+                          We're overloading posix_path here to allow a wider
+                          range of characters. If stream_started is true this
+                          is still a Windows path even if posix_path is true.
+                          JRA.
+                       */
                        stream_started = true;
                        start_of_name_component = false;
                        posix_path = true;
@@ -402,6 +407,95 @@ bool fsp_belongs_conn(connection_struct *conn, struct smb_request *req,
        return False;
 }
 
+static bool netbios_session_retarget(const char *name, int name_type)
+{
+       char *trim_name;
+       char *trim_name_type;
+       const char *retarget_parm;
+       char *retarget;
+       char *p;
+       int retarget_type = 0x20;
+       int retarget_port = 139;
+       struct sockaddr_storage retarget_addr;
+       struct sockaddr_in *in_addr;
+       bool ret = false;
+       uint8_t outbuf[10];
+
+       if (get_socket_port(smbd_server_fd()) != 139) {
+               return false;
+       }
+
+       trim_name = talloc_strdup(talloc_tos(), name);
+       if (trim_name == NULL) {
+               goto fail;
+       }
+       trim_char(trim_name, ' ', ' ');
+
+       trim_name_type = talloc_asprintf(trim_name, "%s#%2.2x", trim_name,
+                                        name_type);
+       if (trim_name_type == NULL) {
+               goto fail;
+       }
+
+       retarget_parm = lp_parm_const_string(-1, "netbios retarget",
+                                            trim_name_type, NULL);
+       if (retarget_parm == NULL) {
+               retarget_parm = lp_parm_const_string(-1, "netbios retarget",
+                                                    trim_name, NULL);
+       }
+       if (retarget_parm == NULL) {
+               goto fail;
+       }
+
+       retarget = talloc_strdup(trim_name, retarget_parm);
+       if (retarget == NULL) {
+               goto fail;
+       }
+
+       DEBUG(10, ("retargeting %s to %s\n", trim_name_type, retarget));
+
+       p = strchr(retarget, ':');
+       if (p != NULL) {
+               *p++ = '\0';
+               retarget_port = atoi(p);
+       }
+
+       p = strchr_m(retarget, '#');
+       if (p != NULL) {
+               *p++ = '\0';
+               sscanf(p, "%x", &retarget_type);
+       }
+
+       ret = resolve_name(retarget, &retarget_addr, retarget_type);
+       if (!ret) {
+               DEBUG(10, ("could not resolve %s\n", retarget));
+               goto fail;
+       }
+
+       if (retarget_addr.ss_family != AF_INET) {
+               DEBUG(10, ("Retarget target not an IPv4 addr\n"));
+               goto fail;
+       }
+
+       in_addr = (struct sockaddr_in *)&retarget_addr;
+
+       _smb_setlen(outbuf, 6);
+       SCVAL(outbuf, 0, 0x84);
+       *(uint32_t *)(outbuf+4) = in_addr->sin_addr.s_addr;
+       *(uint16_t *)(outbuf+8) = htons(retarget_port);
+
+       if (!srv_send_smb(smbd_server_fd(), (char *)outbuf, false, 0, false,
+                         NULL)) {
+               exit_server_cleanly("netbios_session_regarget: srv_send_smb "
+                                   "failed.");
+       }
+
+       ret = true;
+ fail:
+       TALLOC_FREE(trim_name);
+       return ret;
+}
+
 /****************************************************************************
  Reply to a (netbios-level) special message.
 ****************************************************************************/
@@ -411,7 +505,7 @@ void reply_special(char *inbuf)
        int msg_type = CVAL(inbuf,0);
        int msg_flags = CVAL(inbuf,1);
        fstring name1,name2;
-       char name_type = 0;
+       char name_type1, name_type2;
 
        /*
         * We only really use 4 bytes of the outbuf, but for the smb_setlen
@@ -440,19 +534,23 @@ void reply_special(char *inbuf)
                        DEBUG(0,("Invalid name length in session request\n"));
                        return;
                }
-               name_extract(inbuf,4,name1);
-               name_type = name_extract(inbuf,4 + name_len(inbuf + 4),name2);
-               DEBUG(2,("netbios connect: name1=%s name2=%s\n",
-                        name1,name2));      
+               name_type1 = name_extract(inbuf,4,name1);
+               name_type2 = name_extract(inbuf,4 + name_len(inbuf + 4),name2);
+               DEBUG(2,("netbios connect: name1=%s0x%x name2=%s0x%x\n",
+                        name1, name_type1, name2, name_type2));
+
+               if (netbios_session_retarget(name1, name_type1)) {
+                       exit_server_cleanly("retargeted client");
+               }
 
                set_local_machine_name(name1, True);
                set_remote_machine_name(name2, True);
 
                DEBUG(2,("netbios connect: local=%s remote=%s, name type = %x\n",
                         get_local_machine_name(), get_remote_machine_name(),
-                        name_type));
+                        name_type2));
 
-               if (name_type == 'R') {
+               if (name_type2 == 'R') {
                        /* We are being asked for a pathworks session --- 
                           no thanks! */
                        SCVAL(outbuf, 0,0x83);
@@ -492,7 +590,7 @@ void reply_special(char *inbuf)
        DEBUG(5,("init msg_type=0x%x msg_flags=0x%x\n",
                    msg_type, msg_flags));
 
-       srv_send_smb(smbd_server_fd(), outbuf, false, NULL);
+       srv_send_smb(smbd_server_fd(), outbuf, false, 0, false, NULL);
        return;
 }
 
@@ -1601,6 +1699,8 @@ void reply_open(struct smb_request *req)
 
        START_PROFILE(SMBopen);
 
+       SET_STAT_INVALID(sbuf);
+
        if (req->wct < 2) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                END_PROFILE(SMBopen);
@@ -1736,6 +1836,8 @@ void reply_open_and_X(struct smb_request *req)
                return;
        }
 
+       SET_STAT_INVALID(sbuf);
+
        open_flags = SVAL(req->vwv+2, 0);
        deny_mode = SVAL(req->vwv+3, 0);
        smb_attr = SVAL(req->vwv+5, 0);
@@ -1940,6 +2042,7 @@ void reply_mknew(struct smb_request *req)
 
        START_PROFILE(SMBcreate);
        ZERO_STRUCT(ft);
+       SET_STAT_INVALID(sbuf);
 
         if (req->wct < 3) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
@@ -2111,13 +2214,14 @@ void reply_ctemp(struct smb_request *req)
                return;
        }
 
-       tmpfd = smb_mkstemp(fname);
+       tmpfd = mkstemp(fname);
        if (tmpfd == -1) {
                reply_unixerror(req, ERRDOS, ERRnoaccess);
                END_PROFILE(SMBctemp);
                return;
        }
 
+       SET_STAT_INVALID(sbuf);
        SMB_VFS_STAT(conn,fname,&sbuf);
 
        /* We should fail if file does not exist. */
@@ -2140,7 +2244,7 @@ void reply_ctemp(struct smb_request *req)
                NULL,                                   /* pinfo */
                &sbuf);                                 /* psbuf */
 
-       /* close fd from smb_mkstemp() */
+       /* close fd from mkstemp() */
        close(tmpfd);
 
        if (!NT_STATUS_IS_OK(status)) {
@@ -2668,6 +2772,7 @@ static ssize_t fake_sendfile(files_struct *fsp, SMB_OFF_T startpos,
        return (ssize_t)nread;
 }
 
+#if defined(WITH_SENDFILE)
 /****************************************************************************
  Deal with the case of sendfile reading less bytes from the file than
  requested. Fill with zeros (all we can do).
@@ -2724,6 +2829,7 @@ static void sendfile_short_send(files_struct *fsp,
                SAFE_FREE(buf);
        }
 }
+#endif /* defined WITH_SENDFILE */
 
 /****************************************************************************
  Return a readbraw error (4 bytes of zero).
@@ -2761,7 +2867,8 @@ static void send_file_readbraw(connection_struct *conn,
         */
 
        if ( !req_is_in_chain(req) && (nread > 0) && (fsp->base_fsp == NULL) &&
-           (fsp->wcp == NULL) && lp_use_sendfile(SNUM(conn)) ) {
+           (fsp->wcp == NULL) &&
+           lp_use_sendfile(SNUM(conn), smbd_server_conn->signing_state) ) {
                ssize_t sendfile_read = -1;
                char header[4];
                DATA_BLOB header_blob;
@@ -2865,7 +2972,8 @@ void reply_readbraw(struct smb_request *req)
 
        START_PROFILE(SMBreadbraw);
 
-       if (srv_is_signing_active() || is_encrypted_packet(req->inbuf)) {
+       if (srv_is_signing_active(smbd_server_conn) ||
+           is_encrypted_packet(req->inbuf)) {
                exit_server_cleanly("reply_readbraw: SMB signing/sealing is active - "
                        "raw reads/writes are disallowed.");
        }
@@ -3250,14 +3358,13 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
                return;
        }
 
-       if (startpos > sbuf.st_size) {
-               smb_maxcnt = 0;
-       } else if (smb_maxcnt > (sbuf.st_size - startpos)) {
-               smb_maxcnt = (sbuf.st_size - startpos);
-       }
-
-       if (smb_maxcnt == 0) {
-               goto normal_read;
+       if (!S_ISREG(sbuf.st_mode) || (startpos > sbuf.st_size)
+           || (smb_maxcnt > (sbuf.st_size - startpos))) {
+               /*
+                * We already know that we would do a short read, so don't
+                * try the sendfile() path.
+                */
+               goto nosendfile_read;
        }
 
 #if defined(WITH_SENDFILE)
@@ -3269,7 +3376,8 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
 
        if (!req_is_in_chain(req) &&
            !is_encrypted_packet(req->inbuf) && (fsp->base_fsp == NULL) &&
-           lp_use_sendfile(SNUM(conn)) && (fsp->wcp == NULL) ) {
+           (fsp->wcp == NULL) &&
+           lp_use_sendfile(SNUM(conn), smbd_server_conn->signing_state) ) {
                uint8 headerbuf[smb_size + 12 * 2];
                DATA_BLOB header;
 
@@ -3346,10 +3454,11 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
                SMB_PERFCOUNT_END(&req->pcd);
                return;
        }
-#endif
 
 normal_read:
 
+#endif
+
        if ((smb_maxcnt & 0xFF0000) > 0x10000) {
                uint8 headerbuf[smb_size + 2*12];
 
@@ -3373,6 +3482,8 @@ normal_read:
                return;
        }
 
+nosendfile_read:
+
        reply_outbuf(req, 12, smb_maxcnt);
 
        nread = read_file(fsp, smb_buf(req->outbuf), startpos, smb_maxcnt);
@@ -3445,7 +3556,8 @@ void reply_read_and_X(struct smb_request *req)
                                return;
                        }
                        /* We currently don't do this on signed or sealed data. */
-                       if (srv_is_signing_active() || is_encrypted_packet(req->inbuf)) {
+                       if (srv_is_signing_active(smbd_server_conn) ||
+                           is_encrypted_packet(req->inbuf)) {
                                reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
                                END_PROFILE(SMBreadX);
                                return;
@@ -3553,7 +3665,7 @@ void reply_writebraw(struct smb_request *req)
         */
        SCVAL(req->inbuf,smb_com,SMBwritec);
 
-       if (srv_is_signing_active()) {
+       if (srv_is_signing_active(smbd_server_conn)) {
                END_PROFILE(SMBwritebraw);
                exit_server_cleanly("reply_writebraw: SMB signing is active - "
                                "raw reads/writes are disallowed.");
@@ -3648,9 +3760,10 @@ void reply_writebraw(struct smb_request *req)
        SSVALS(buf,smb_vwv0,0xFFFF);
        show_msg(buf);
        if (!srv_send_smb(smbd_server_fd(),
-                       buf,
-                       IS_CONN_ENCRYPTED(conn),
-                       &req->pcd)) {
+                         buf,
+                         false, 0, /* no signing */
+                         IS_CONN_ENCRYPTED(conn),
+                         &req->pcd)) {
                exit_server_cleanly("reply_writebraw: srv_send_smb "
                        "failed.");
        }
@@ -4752,6 +4865,7 @@ void reply_echo(struct smb_request *req)
                show_msg((char *)req->outbuf);
                if (!srv_send_smb(smbd_server_fd(),
                                (char *)req->outbuf,
+                               true, req->seqnum+1,
                                IS_CONN_ENCRYPTED(conn)||req->encrypted,
                                cur_pcd))
                        exit_server_cleanly("reply_echo: srv_send_smb failed.");