home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / MSDOS / WATTCP / UNZIPPED / MSWATTCP / APPS / REXEC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-09  |  4.8 KB  |  204 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. #define getrandom(min,max) ((rand()% (int)(((max)+1)-(min)))+(min))
  31.  
  32. char cmdbuf[ 2048 ];
  33. word cmdbuflen;
  34.  
  35. char lname[ 64 ];               /* local copies if none supplied */
  36. char lpass[ 64 ];
  37. char lcmd[ 255 ];
  38.  
  39. void makecmdbuf( void *err, char *name, char *pass, char *cmd ) {
  40.  
  41.     char *p;
  42.  
  43.     p = cmdbuf;
  44.     *p++ = 0;
  45.     strcpy( p, name );
  46.     p = strchr( p, 0 );
  47.     strcpy( ++p, pass );
  48.     p = strchr( p, 0 );
  49.     strcpy( ++p, cmd );
  50.     p = strchr( p, 0 );
  51.  
  52.     cmdbuflen = (word)(p - cmdbuf) + 1;
  53.     }
  54.  
  55. char *getpass(char *s) {
  56.  
  57.     static char thepass[128];
  58.     char c, *p=thepass;
  59.     
  60.     fprintf(stderr,s);
  61.     
  62.     while ( c= (char)getch() ) {
  63.         switch (c) {
  64.             case 8 : /* backspace */
  65.             case 127: /* DEL */
  66.                 if (p>thepass) p--;
  67.                 break;
  68.             case 13:
  69.             case 10:
  70.                 *p=0;
  71.                 fprintf(stderr,"\n");
  72.                 return(thepass);
  73.             default: 
  74.                 *p++=c;
  75.                 if (p-thepass >=sizeof(thepass)-1) p--;
  76.                 break;
  77.             }
  78.         }
  79.     
  80.     return(NULL);
  81.     }
  82.  
  83. rsh( char *hostname, word port, char *name, char *pass, char *cmd ) {
  84.  
  85.     word lport, jqpublic, count;
  86.     int status;
  87.     longword host;
  88.     char buffer[ 1024 ];
  89.     tcp_Socket rsh_sock;
  90.  
  91.  
  92.     /* randomize(); */
  93.     lport = (word)(getrandom(0,512) + 512);  /* return 511 < port < 1024 */
  94.     /*    lport = (rand() & 512) + 512;      /* return 511 < port < 1024 */
  95.  
  96.     if (!(host = resolve( hostname ))) {
  97.     printf("Unable to resolve '%s'\naborting\n", hostname );
  98.     return( 2 );
  99.     }
  100.  
  101.     jqpublic = 0;
  102.     if ( !name ) {
  103.     printf(" Userid   : ");
  104.     gets( name = lname );
  105.     if ( !*name ) {
  106.         printf( name = "JQPUBLIC");
  107.         jqpublic = 1;
  108.         }
  109.     }
  110.     
  111.     if ( !pass ) {
  112.     if (jqpublic) pass = "";
  113.     else
  114.         strcpy( pass = lpass, getpass(" Password : "));
  115.          /* copy for neatness since getpass overwrites */
  116.     }
  117.  
  118.     if (!cmd) {
  119.     printf(" Command  : ");
  120.     gets( cmd = lcmd );
  121.     if ( !*cmd ) {
  122.         puts("No command given\n");
  123.         exit( 2 );
  124.         }
  125.     }
  126.  
  127.     makecmdbuf( NULL, name, pass, cmd);
  128.  
  129.     if (! tcp_open( &rsh_sock, lport, host, port, NULL )) {
  130.     printf("Remote host unaccessible");
  131.     return( 1 );
  132.     }
  133.     fprintf(stderr, "waiting for remote host to connect...\r");
  134.     sock_wait_established( (sock_type *)&rsh_sock, sock_delay, NULL, &status);
  135.  
  136.     fprintf(stderr, "remote host connected, waiting verification...\r");
  137.  
  138.     sock_write( (sock_type *)&rsh_sock, cmdbuf, cmdbuflen );
  139.  
  140.     while (1) {
  141.     sock_tick( (sock_type *)&rsh_sock, &status );
  142.     if (!sock_dataready( (sock_type *)&rsh_sock ))
  143.         continue;
  144.     sock_read( (sock_type *)&rsh_sock, buffer, 1 );
  145.     fprintf(stderr, "                                              \r");
  146.     if ( *buffer == 1 )
  147.         fprintf(stdout, "RSH failed...\n\r");
  148.     break;
  149.     }
  150.  
  151.     while (1) {
  152.     if (kbhit())
  153.         sock_putc( (sock_type *)&rsh_sock, (byte)getch());
  154.     sock_tick( (sock_type *)&rsh_sock, &status );
  155.     if (sock_dataready( (sock_type *)&rsh_sock )) {
  156.            count = sock_read((sock_type *)&rsh_sock,buffer,sizeof(buffer));
  157.  
  158. /*** 8-apr-92 gm Here we can insert a call to Tar.library 
  159.  *** and make the output in tar form.
  160.  ***/
  161.            fwrite( buffer , count, 1, stdout );
  162.            }
  163.     }
  164.  
  165. sock_err:
  166.     switch (status) {
  167.     case 1 : puts("\nConnection closed");
  168.          break;
  169.     case-1 : printf("ERROR: %s\n", sockerr( &rsh_sock ));
  170.          break;
  171.     }
  172.     return( (status == 1) ? 0 : 1 );
  173. }
  174.  
  175. void help()
  176. {
  177.     puts("RSH host [username [password]] cmdstring");
  178.     puts("The values for cmdstring should be placed inside quotes");
  179.     exit( 3 );
  180. }
  181.  
  182. void main( int argc, char **argv )
  183. {
  184.     char *hostname, *name, *pass, *cmd;
  185.  
  186.     dbuginit();
  187.     hostname = name = pass = cmd = NULL;
  188.     sock_init();
  189.  
  190.     hostname = argv[ 1 ];
  191.  
  192.     switch ( argc ) {
  193.     case  5 : pass = argv[3];
  194.     case  4 : name = argv[2];
  195.     case  3 : cmd = argv[ argc - 1 ];
  196.           break;
  197.     case  2 : break;
  198.     default : help();
  199.     }
  200.  
  201.     exit( rsh( hostname, RSH_PORT, name, pass, cmd ));
  202. }
  203.  
  204.