summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorMarkus Friedl <markus@cvs.openbsd.org>2008-02-04 21:53:01 +0000
committerMarkus Friedl <markus@cvs.openbsd.org>2008-02-04 21:53:01 +0000
commit9d8b3928b1c88c9fd75710461b7e5ea3310a30bd (patch)
tree9979f4b949147fdd404fdaa550d0f30dce91f5f5 /usr.bin
parentf384f36f596a32adb5cf27040e9739ee2549b8c0 (diff)
link sftp-server into sshd; feedback and ok djm@
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/ssh/session.c37
-rw-r--r--usr.bin/ssh/sftp-server-main.c34
-rw-r--r--usr.bin/ssh/sftp-server.c44
-rw-r--r--usr.bin/ssh/sftp-server/Makefile4
-rw-r--r--usr.bin/ssh/sftp.h5
-rw-r--r--usr.bin/ssh/sshd/Makefile4
6 files changed, 101 insertions, 27 deletions
diff --git a/usr.bin/ssh/session.c b/usr.bin/ssh/session.c
index f9fc444fdd2..2592b425073 100644
--- a/usr.bin/ssh/session.c
+++ b/usr.bin/ssh/session.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: session.c,v 1.224 2007/09/11 15:47:17 gilles Exp $ */
+/* $OpenBSD: session.c,v 1.225 2008/02/04 21:53:00 markus Exp $ */
/*
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
* All rights reserved
@@ -79,6 +79,7 @@
#include "ssh-gss.h"
#endif
#include "monitor_wrap.h"
+#include "sftp.h"
#ifdef KRB5
#include <kafs.h>
@@ -121,6 +122,10 @@ const char *original_command = NULL;
#define MAX_SESSIONS 10
Session sessions[MAX_SESSIONS];
+#define SUBSYSTEM_NONE 0
+#define SUBSYSTEM_EXT 1
+#define SUBSYSTEM_INT_SFTP 2
+
login_cap_t *lc;
static int is_child = 0;
@@ -545,10 +550,14 @@ do_exec(Session *s, const char *command)
if (options.adm_forced_command) {
original_command = command;
command = options.adm_forced_command;
+ if (s->is_subsystem)
+ s->is_subsystem = SUBSYSTEM_EXT;
debug("Forced command (config) '%.900s'", command);
} else if (forced_command) {
original_command = command;
command = forced_command;
+ if (s->is_subsystem)
+ s->is_subsystem = SUBSYSTEM_EXT;
debug("Forced command (key option) '%.900s'", command);
}
@@ -1026,12 +1035,13 @@ child_close_fds(void)
* environment, closing extra file descriptors, setting the user and group
* ids, and executing the command or shell.
*/
+#define ARGV_MAX 10
void
do_child(Session *s, const char *command)
{
extern char **environ;
char **env;
- char *argv[10];
+ char *argv[ARGV_MAX];
const char *shell, *shell0, *hostname = NULL;
struct passwd *pw = s->pw;
@@ -1132,6 +1142,22 @@ do_child(Session *s, const char *command)
/* restore SIGPIPE for child */
signal(SIGPIPE, SIG_DFL);
+ if (s->is_subsystem == SUBSYSTEM_INT_SFTP) {
+ extern int optind, optreset;
+ int i;
+ char *p, *args;
+
+ setproctitle("%s@internal-sftp-server", s->pw->pw_name);
+ args = strdup(command ? command : "sftp-server");
+ for (i = 0, (p = strtok(args, " ")); p; (p = strtok(NULL, " ")))
+ if (i < ARGV_MAX - 1)
+ argv[i++] = p;
+ argv[i] = NULL;
+ optind = optreset = 1;
+ __progname = argv[0];
+ exit(sftp_server_main(i, argv));
+ }
+
if (options.use_login) {
launch_login(pw, hostname);
/* NEVERREACHED */
@@ -1404,13 +1430,16 @@ session_subsystem_req(Session *s)
if (strcmp(subsys, options.subsystem_name[i]) == 0) {
prog = options.subsystem_command[i];
cmd = options.subsystem_args[i];
- if (stat(prog, &st) < 0) {
+ if (!strcmp("internal-sftp", prog)) {
+ s->is_subsystem = SUBSYSTEM_INT_SFTP;
+ } else if (stat(prog, &st) < 0) {
error("subsystem: cannot stat %s: %s", prog,
strerror(errno));
break;
+ } else {
+ s->is_subsystem = SUBSYSTEM_EXT;
}
debug("subsystem: exec() %s", cmd);
- s->is_subsystem = 1;
do_exec(s, cmd);
success = 1;
break;
diff --git a/usr.bin/ssh/sftp-server-main.c b/usr.bin/ssh/sftp-server-main.c
new file mode 100644
index 00000000000..993455b6c0a
--- /dev/null
+++ b/usr.bin/ssh/sftp-server-main.c
@@ -0,0 +1,34 @@
+/* $OpenBSD: */
+/*
+ * Copyright (c) 2008 Markus Friedl. All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+#include <stdarg.h>
+
+#include "log.h"
+#include "sftp.h"
+
+void
+cleanup_exit(int i)
+{
+ sftp_server_cleanup_exit(i);
+}
+
+int
+main(int argc, char **argv)
+{
+ return (sftp_server_main(argc, argv));
+}
diff --git a/usr.bin/ssh/sftp-server.c b/usr.bin/ssh/sftp-server.c
index fb288fc6e86..dfc85aec454 100644
--- a/usr.bin/ssh/sftp-server.c
+++ b/usr.bin/ssh/sftp-server.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sftp-server.c,v 1.75 2008/01/21 17:24:30 djm Exp $ */
+/* $OpenBSD: sftp-server.c,v 1.76 2008/02/04 21:53:00 markus Exp $ */
/*
* Copyright (c) 2000-2004 Markus Friedl. All rights reserved.
*
@@ -1089,7 +1089,7 @@ process(void)
if (msg_len > SFTP_MAX_MSG_LENGTH) {
error("bad message from %s local user %s",
client_addr, pw->pw_name);
- cleanup_exit(11);
+ sftp_server_cleanup_exit(11);
}
if (buf_len < msg_len + 4)
return;
@@ -1162,18 +1162,22 @@ process(void)
break;
}
/* discard the remaining bytes from the current packet */
- if (buf_len < buffer_len(&iqueue))
- fatal("iqueue grew unexpectedly");
+ if (buf_len < buffer_len(&iqueue)) {
+ error("iqueue grew unexpectedly");
+ sftp_server_cleanup_exit(255);
+ }
consumed = buf_len - buffer_len(&iqueue);
- if (msg_len < consumed)
- fatal("msg_len %d < consumed %d", msg_len, consumed);
+ if (msg_len < consumed) {
+ error("msg_len %d < consumed %d", msg_len, consumed);
+ sftp_server_cleanup_exit(255);
+ }
if (msg_len > consumed)
buffer_consume(&iqueue, msg_len - consumed);
}
/* Cleanup handler that logs active handles upon normal exit */
void
-cleanup_exit(int i)
+sftp_server_cleanup_exit(int i)
{
if (pw != NULL && client_addr != NULL) {
handle_log_exit();
@@ -1184,7 +1188,7 @@ cleanup_exit(int i)
}
static void
-usage(void)
+sftp_server_usage(void)
{
extern char *__progname;
@@ -1194,7 +1198,7 @@ usage(void)
}
int
-main(int argc, char **argv)
+sftp_server_main(int argc, char **argv)
{
fd_set *rset, *wset;
int in, out, max, ch, skipargs = 0, log_stderr = 0;
@@ -1234,7 +1238,7 @@ main(int argc, char **argv)
break;
case 'h':
default:
- usage();
+ sftp_server_usage();
}
}
@@ -1242,15 +1246,19 @@ main(int argc, char **argv)
if ((cp = getenv("SSH_CONNECTION")) != NULL) {
client_addr = xstrdup(cp);
- if ((cp = strchr(client_addr, ' ')) == NULL)
- fatal("Malformed SSH_CONNECTION variable: \"%s\"",
+ if ((cp = strchr(client_addr, ' ')) == NULL) {
+ error("Malformed SSH_CONNECTION variable: \"%s\"",
getenv("SSH_CONNECTION"));
+ sftp_server_cleanup_exit(255);
+ }
*cp = '\0';
} else
client_addr = xstrdup("UNKNOWN");
- if ((pw = getpwuid(getuid())) == NULL)
- fatal("No user found for uid %lu", (u_long)getuid());
+ if ((pw = getpwuid(getuid())) == NULL) {
+ error("No user found for uid %lu", (u_long)getuid());
+ sftp_server_cleanup_exit(255);
+ }
pw = pwcopy(pw);
logit("session opened for local user %s from [%s]",
@@ -1293,7 +1301,7 @@ main(int argc, char **argv)
if (errno == EINTR)
continue;
error("select: %s", strerror(errno));
- cleanup_exit(2);
+ sftp_server_cleanup_exit(2);
}
/* copy stdin to iqueue */
@@ -1301,10 +1309,10 @@ main(int argc, char **argv)
len = read(in, buf, sizeof buf);
if (len == 0) {
debug("read eof");
- cleanup_exit(0);
+ sftp_server_cleanup_exit(0);
} else if (len < 0) {
error("read: %s", strerror(errno));
- cleanup_exit(1);
+ sftp_server_cleanup_exit(1);
} else {
buffer_append(&iqueue, buf, len);
}
@@ -1314,7 +1322,7 @@ main(int argc, char **argv)
len = write(out, buffer_ptr(&oqueue), olen);
if (len < 0) {
error("write: %s", strerror(errno));
- cleanup_exit(1);
+ sftp_server_cleanup_exit(1);
} else {
buffer_consume(&oqueue, len);
}
diff --git a/usr.bin/ssh/sftp-server/Makefile b/usr.bin/ssh/sftp-server/Makefile
index 0d6068bce90..c923d4e84e4 100644
--- a/usr.bin/ssh/sftp-server/Makefile
+++ b/usr.bin/ssh/sftp-server/Makefile
@@ -1,4 +1,4 @@
-# $OpenBSD: Makefile,v 1.6 2006/04/18 10:44:28 dtucker Exp $
+# $OpenBSD: Makefile,v 1.7 2008/02/04 21:53:00 markus Exp $
.PATH: ${.CURDIR}/..
@@ -10,6 +10,6 @@ BINMODE?=555
BINDIR= /usr/libexec
MAN= sftp-server.8
-SRCS= sftp-server.c sftp-common.c
+SRCS= sftp-server.c sftp-common.c sftp-server-main.c
.include <bsd.prog.mk>
diff --git a/usr.bin/ssh/sftp.h b/usr.bin/ssh/sftp.h
index 610c0b758e2..12b9cc05680 100644
--- a/usr.bin/ssh/sftp.h
+++ b/usr.bin/ssh/sftp.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: sftp.h,v 1.5 2006/03/25 22:22:43 djm Exp $ */
+/* $OpenBSD: sftp.h,v 1.6 2008/02/04 21:53:00 markus Exp $ */
/*
* Copyright (c) 2001 Markus Friedl. All rights reserved.
@@ -90,3 +90,6 @@
#define SSH2_FX_CONNECTION_LOST 7
#define SSH2_FX_OP_UNSUPPORTED 8
#define SSH2_FX_MAX 8
+
+int sftp_server_main(int, char **);
+void sftp_server_cleanup_exit(int) __dead;
diff --git a/usr.bin/ssh/sshd/Makefile b/usr.bin/ssh/sshd/Makefile
index ddf954a1898..158624693db 100644
--- a/usr.bin/ssh/sshd/Makefile
+++ b/usr.bin/ssh/sshd/Makefile
@@ -1,4 +1,4 @@
-# $OpenBSD: Makefile,v 1.66 2007/09/21 08:15:29 djm Exp $
+# $OpenBSD: Makefile,v 1.67 2008/02/04 21:53:00 markus Exp $
.PATH: ${.CURDIR}/..
@@ -15,7 +15,7 @@ SRCS= sshd.c auth-rhosts.c auth-passwd.c auth-rsa.c auth-rh-rsa.c \
auth-bsdauth.c auth2-hostbased.c auth2-kbdint.c \
auth2-none.c auth2-passwd.c auth2-pubkey.c \
monitor_mm.c monitor.c monitor_wrap.c \
- kexdhs.c kexgexs.c
+ kexdhs.c kexgexs.c sftp-server.c sftp-common.c
.include <bsd.own.mk> # for KERBEROS and AFS