Home | History | Annotate | Download | only in dropbear
      1 /*
      2  * Dropbear - a SSH2 server
      3  * SSH client implementation
      4  *
      5  * Copyright (c) 2002,2003 Matt Johnston
      6  * Copyright (c) 2004 by Mihnea Stoenescu
      7  * All rights reserved.
      8  *
      9  * Permission is hereby granted, free of charge, to any person obtaining a copy
     10  * of this software and associated documentation files (the "Software"), to deal
     11  * in the Software without restriction, including without limitation the rights
     12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     13  * copies of the Software, and to permit persons to whom the Software is
     14  * furnished to do so, subject to the following conditions:
     15  *
     16  * The above copyright notice and this permission notice shall be included in
     17  * all copies or substantial portions of the Software.
     18  *
     19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     25  * SOFTWARE. */
     26 
     27 #include "includes.h"
     28 #include "dbutil.h"
     29 #include "runopts.h"
     30 #include "session.h"
     31 
     32 static void cli_dropbear_exit(int exitcode, const char* format, va_list param);
     33 static void cli_dropbear_log(int priority, const char* format, va_list param);
     34 
     35 #if defined(DBMULTI_dbclient) || !defined(DROPBEAR_MULTI)
     36 #if defined(DBMULTI_dbclient) && defined(DROPBEAR_MULTI)
     37 int cli_main(int argc, char ** argv) {
     38 #else
     39 int main(int argc, char ** argv) {
     40 #endif
     41 
     42 	int sock;
     43 	char* error = NULL;
     44 	char* hostandport;
     45 	int len;
     46 
     47 	_dropbear_exit = cli_dropbear_exit;
     48 	_dropbear_log = cli_dropbear_log;
     49 
     50 	disallow_core();
     51 
     52 	cli_getopts(argc, argv);
     53 
     54 	TRACE(("user='%s' host='%s' port='%s'", cli_opts.username,
     55 				cli_opts.remotehost, cli_opts.remoteport))
     56 
     57 	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
     58 		dropbear_exit("signal() error");
     59 	}
     60 
     61 	sock = connect_remote(cli_opts.remotehost, cli_opts.remoteport,
     62 			0, &error);
     63 
     64 	if (sock < 0) {
     65 		dropbear_exit("%s", error);
     66 	}
     67 
     68 	/* Set up the host:port log */
     69 	len = strlen(cli_opts.remotehost);
     70 	len += 10; /* 16 bit port and leeway*/
     71 	hostandport = (char*)m_malloc(len);
     72 	snprintf(hostandport, len, "%s:%s",
     73 			cli_opts.remotehost, cli_opts.remoteport);
     74 
     75 	cli_session(sock, hostandport);
     76 
     77 	/* not reached */
     78 	return -1;
     79 }
     80 #endif /* DBMULTI stuff */
     81 
     82 static void cli_dropbear_exit(int exitcode, const char* format, va_list param) {
     83 
     84 	char fmtbuf[300];
     85 
     86 	if (!sessinitdone) {
     87 		snprintf(fmtbuf, sizeof(fmtbuf), "exited: %s",
     88 				format);
     89 	} else {
     90 		snprintf(fmtbuf, sizeof(fmtbuf),
     91 				"connection to %s@%s:%s exited: %s",
     92 				cli_opts.username, cli_opts.remotehost,
     93 				cli_opts.remoteport, format);
     94 	}
     95 
     96 	/* Do the cleanup first, since then the terminal will be reset */
     97 	cli_session_cleanup();
     98 	common_session_cleanup();
     99 
    100 	_dropbear_log(LOG_INFO, fmtbuf, param);
    101 
    102 	exit(exitcode);
    103 }
    104 
    105 static void cli_dropbear_log(int UNUSED(priority),
    106 		const char* format, va_list param) {
    107 
    108 	char printbuf[1024];
    109 
    110 	vsnprintf(printbuf, sizeof(printbuf), format, param);
    111 
    112 	fprintf(stderr, "%s: %s\n", cli_opts.progname, printbuf);
    113 
    114 }
    115