home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / wattcp.zip / REXEC.C < prev    next >
C/C++ Source or Header  |  1991-08-16  |  4KB  |  167 lines

  1. /******************************************************************************
  2.  
  3.     REXEC - remotely execute a UNIX command
  4.  
  5.     Copyright (C) 1991, University of Waterloo
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it, but you may not sell it.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but without any warranty; without even the implied warranty of
  12.     merchantability or fitness for a particular purpose.
  13.  
  14.         Erick Engelke                   or via E-Mail
  15.         Faculty of Engineering
  16.         University of Waterloo          Erick@development.watstar.uwaterloo.ca
  17.         200 University Ave.,
  18.         Waterloo, Ont., Canada
  19.         N2L 3G1
  20.  
  21. ******************************************************************************/
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <time.h>        /* for randomize */
  25. #include <stdlib.h>
  26. #include <conio.h>        /* for getpass */
  27. #include <tcp.h>
  28.  
  29. #define RSH_PORT 512
  30.  
  31. char cmdbuf[ 2048 ];
  32. word cmdbuflen;
  33.  
  34. char lname[ 64 ];        /* local copies if none supplied */
  35. char lpass[ 64 ];
  36. char lcmd[ 255 ];
  37.  
  38. makecmdbuf( void *err, char *name, char *pass, char *cmd )
  39. {
  40.     char *p;
  41.  
  42.     p = cmdbuf;
  43.     *p++ = 0;
  44.     strcpy( p, name );
  45.     p = strchr( p, 0 );
  46.     strcpy( ++p, pass );
  47.     p = strchr( p, 0 );
  48.     strcpy( ++p, cmd );
  49.     p = strchr( p, 0 );
  50.  
  51.     cmdbuflen = (word)(p - cmdbuf) + 1;
  52. }
  53.  
  54. rsh( char *hostname, word port, char *name, char *pass, char *cmd )
  55. {
  56.     word lport, jqpublic, count;
  57.     int status;
  58.     longword host;
  59.     char ch, buffer[ 1024 ];
  60.     tcp_Socket rsh_sock;
  61.  
  62.  
  63.     randomize();
  64.     lport = (rand() & 512) + 512;    /* return 511 < port < 1024 */
  65.  
  66.     if (!(host = resolve( hostname ))) {
  67.     printf("Unable to resolve '%s'\naborting\n", hostname );
  68.     return( 2 );
  69.     }
  70.  
  71.     jqpublic = 0;
  72.     if ( !name ) {
  73.         printf(" Userid   : ");
  74.     gets( name = lname );
  75.     if ( !*name ) {
  76.         printf( name = "JQPUBLIC");
  77.         jqpublic = 1;
  78.     }
  79.     }
  80.     if ( !pass ) {
  81.     if (jqpublic) pass = "";
  82.     else
  83.         strcpy( pass = lpass, getpass(" Password : "));
  84.          /* copy for neatness since getpass overwrites */
  85.     }
  86.     if (!cmd) {
  87.         printf(" Command  : ");
  88.         gets( cmd = lcmd );
  89.         if ( !*cmd ) {
  90.             puts("No command given\n");
  91.             exit( 2 );
  92.         }
  93.     }
  94.  
  95.     makecmdbuf( NULL, name, pass, cmd);
  96.  
  97.     if (! tcp_open( &rsh_sock, lport, host, port, NULL )) {
  98.     printf("Remote host unaccessible");
  99.     return( 1 );
  100.     }
  101.     fprintf(stderr, "waiting for remote host to connect...\r");
  102.     sock_wait_established( &rsh_sock, sock_delay, NULL, &status );
  103.  
  104.     fprintf(stderr, "remote host connected, waiting verification...\r");
  105.  
  106.     sock_write( &rsh_sock, cmdbuf, cmdbuflen );
  107.  
  108.     while (1) {
  109.     sock_tick( &rsh_sock, &status );
  110.     if (!sock_dataready(&rsh_sock))
  111.         continue;
  112.     sock_fastread( &rsh_sock, buffer, 1 );
  113.     fprintf(stderr, "                                              \r");
  114.     if ( *buffer == 1 )
  115.         fprintf(stdout, "RSH failed...\n\r");
  116.     break;
  117.     }
  118.  
  119.     while (1) {
  120.     if (kbhit())
  121.         sock_putc( &rsh_sock, getch());
  122.     sock_tick( &rsh_sock, &status );
  123.     if (sock_dataready( &rsh_sock )) {
  124.         count = sock_fastread( &rsh_sock, buffer, sizeof( buffer ));
  125.         fwrite( buffer , count, 1, stdout );
  126.     }
  127.     }
  128.  
  129. sock_err:
  130.     switch (status) {
  131.     case 1 : puts("\nConnection closed");
  132.          break;
  133.         case-1 : printf("ERROR: %s\n", sockerr( &rsh_sock ));
  134.          break;
  135.     }
  136.     return( (status == 1) ? 0 : 1 );
  137. }
  138.  
  139. help()
  140. {
  141.     puts("RSH host [username [password]] cmdstring");
  142.     puts("The values for cmdstring should be placed inside quotes");
  143.     exit( 3 );
  144. }
  145.  
  146. main( int argc, char **argv )
  147. {
  148.     char *hostname, *name, *pass, *cmd;
  149.  
  150.     dbuginit();
  151.     hostname = name = pass = cmd = NULL;
  152.     sock_init();
  153.  
  154.     hostname = argv[ 1 ];
  155.  
  156.     switch ( argc ) {
  157.     case  5 : pass = argv[3];
  158.     case  4 : name = argv[2];
  159.         case  3 : cmd = argv[ argc - 1 ];
  160.                   break;
  161.         case  2 : break;
  162.     default : help();
  163.     }
  164.  
  165.     exit( rsh( hostname, RSH_PORT, name, pass, cmd ));
  166. }
  167.