The correct answer to SFTP is "open" to a nonexistent file

I am writing custom file system classes for Apache SSHd. I got into a problem when a client tries a getfile that does not exist.

My object is returning doesExist() == false.

This causes the server to send an SSH_FX_NO_SUCH_FILE packet in response to SSH_FXP_STAT.

Using the OpenSSH client, this works fine: the client reports โ€œFileโ€ / foo โ€œnot foundโ€.

However, PSFTP (Putty SFTP client) sends its requests, so it sends an OPEN packet regardless of the response to STAT. Then it sends a READ to the resulting file descriptor, which is currently throwing an exception in my code, which leads to the end of the whole session.

What should be the reaction of the server when trying to read a file that does not exist?

With apologies for the verbosity, here is the Apache code. I would rather not change it - my class is an SshFile. But if the Apache code is definitely buggy, I think I will have to give them a patch.

    if (version <= 4) {
       String path = buffer.getString();
       int pflags = buffer.getInt();
       // attrs
       try {
            SshFile file = resolveFile(path);
            if (file.doesExist()) {
                if (((pflags & SSH_FXF_CREAT) != 0) && ((pflags & SSH_FXF_EXCL) != 0)) {
                    sendStatus(id, SSH_FX_FILE_ALREADY_EXISTS, path);
                    return;
                }
            } else {
                if (((pflags & SSH_FXF_CREAT) != 0)) {
                    if (!file.isWritable()) {
                        sendStatus(id, SSH_FX_FAILURE, "Can not create " + path);
                        return;
                    }
                    file.create();
                }
            }
            String acc = ((pflags & (SSH_FXF_READ | SSH_FXF_WRITE)) != 0 ? "r" : "") +
                   ((pflags & SSH_FXF_WRITE) != 0 ? "w" : "");
            if ((pflags & SSH_FXF_TRUNC) != 0) {
                 file.truncate();
            }
            String handle = UUID.randomUUID().toString();
            handles.put(handle, new FileHandle(file, pflags)); // handle flags conversion
            sendHandle(id, handle);
       } catch (IOException e) {
            sendStatus(id, SSH_FX_FAILURE, e.getMessage());
       }
+5
source share
1 answer

The response to the request is SSH_FXP_OPENalso with a status code SSH_FX_NO_SUCH_FILE. This should solve the problem with psftp.

, . , , SSH_FX_FAILURE. error message SSH_FXP_STATUS, , .

+3

All Articles