ssh-mime.c 4.95 KB
Newer Older
1 2
/*
 * Copyright (c) 2004 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 
 * {{{EMULAB-LICENSE
 * 
 * This file is part of the Emulab network testbed software.
 * 
 * This file is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This file is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * }}}
22 23
 */

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/* Compile on cygwin with gcc -mwindows -mno-cygwin -o ssh-mime ssh-mime.c */
/* Set the env PUTTY_CMD to the path of where you have put putty */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/param.h>
#ifdef _WIN32
#include <windows.h>
#endif

char *usagestr = 
 "usage: ssh-mime <control-file>"
 "\n";

39
char *default_sshcmd = "\"c:\\putty.exe\" -ssh -1 -A -X";
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
char *tempfile = "c:\\windows\\temp\\puttycmd";
char *logfile = "c:\\windows\\temp\\ssh-mime.log";
char sshcmd[MAXPATHLEN];
FILE *fplog = 0;

void
usage()
{
	fprintf(fplog, usagestr);
	exit(1);
}

#ifdef _WIN32
int APIENTRY WinMain(HINSTANCE hInstance,
                HINSTANCE hPrevInstance,
                LPSTR lpCmdLine,
                int nCmdShow) {
#else
int main(int argc, char *argv[]) {
#endif

    FILE *fp = 0;
    char line[1024];
    char hostname[1024];
    char gateway[1024];
    char login[64];
    char port[10];
    char cmdline[MAXPATHLEN];
    char *str = 0;
#ifdef _WIN32
    char *argv[3];
    int argc=0;

    argv[0] = strtok( GetCommandLine(), " \t\n");
    if (argv[0]) {
	argc++;
    }
    if (lpCmdLine[0] != '\0') {
	argv[1] = lpCmdLine;
	argc++;
	argv[2] = 0;
    } else {
	argv[1] = 0;
    }
#endif
    fplog = fopen(logfile, "w");
    if (fplog == 0) {
	fplog = stderr;
    }

    if (argc != 2) {
	usage();
    }

    fp = fopen(argv[1], "r");
    if (fp == 0) {
	fprintf(fplog, "ssh-mime: Error \"%s\" opening file:%s", 
		strerror(errno), argv[1]);
	exit(2);
    }
    if( (str = getenv("PUTTY_CMD")) ) {
101
	snprintf(sshcmd, sizeof(sshcmd), "\"%s\" -ssh -1 -A -X", 
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
		 str );
    } else {
	strncpy( sshcmd, default_sshcmd, sizeof(sshcmd) );
    }

    memset(line, 0, sizeof(line));
    memset(hostname, 0, sizeof(hostname));
    memset(gateway, 0, sizeof(gateway));
    memset(login, 0, sizeof(login));
    memset(port, 0, sizeof(port));
    while( fgets(line, sizeof(line), fp) != NULL ) {
        char *name = strtok(line, ":\t \n");
	char *val = strtok(NULL, ":\t \n");
	if ( name && val ) {
	    if( strcmp(name, "hostname") == 0 ) {
		strncpy( hostname, val, sizeof(hostname) );
	    } else if( strcmp(name, "gateway") == 0 ) {
		strncpy( gateway, val, sizeof(gateway) );
	    } else if( strcmp(name, "login") == 0 ) {
		snprintf( login, sizeof(login), "-l %s", val );
	    } else if( strcmp(name, "port") == 0 ) {
		snprintf( port, sizeof(port), "-P %s", val );
	    }
	}
	memset(line, 0, sizeof(line));
    }
    if( hostname[0] == '\0' ) {
	fprintf(fplog, "ssh-mime: Config file must specify a hostname\n");
	exit(3);
    }
    if( gateway[0] != '\0' ) {
        FILE *tmpfp = fopen( tempfile, "w" );
        if (tmpfp == 0) {
		fprintf(fplog, "ssh-mime: Error \"%s\" opening file:%s", 
			strerror(errno), tempfile);
		exit(4);
	}
        fprintf( tmpfp, "ssh -tt -o StrictHostKeyChecking=no %s %s",
		 port, hostname );
        fclose(tmpfp);
	snprintf( cmdline, sizeof(cmdline), "%s -t %s -m %s %s", sshcmd,
		login, tempfile, gateway);
    } else {
	snprintf( cmdline, sizeof(cmdline), "%s %s %s %s", sshcmd,
		port, login, hostname);
    }
#ifdef _WIN32
    {
        /* Just cut'n'pasted from someplace */
        STARTUPINFO si;
        PROCESS_INFORMATION pi;

        ZeroMemory( &si, sizeof(si) );
        si.cb = sizeof(si);
        ZeroMemory( &pi, sizeof(pi) );

        // Start the child process.
        if( !CreateProcess( NULL, // No module name (use command line).
                cmdline, 	  // Command line. 
                NULL,             // Process handle not inheritable.
                NULL,             // Thread handle not inheritable.
                FALSE,            // Set handle inheritance to FALSE.
                0,                // No creation flags.
                NULL,             // Use parent's environment block.
                NULL,             // Use parent's starting directory.
                &si,              // Pointer to STARTUPINFO structure.
                &pi )             // Pointer to PROCESS_INFORMATION structure.
        )
        {
		fprintf(fplog, "Could not run cmdline:%s\n", cmdline );
        } else {
		WaitForInputIdle( pi.hProcess, 1000 ); 
	}
    }
#else
    system(cmdline);
#endif
    exit(0);
}