home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / mlddc320.zip / mlddc.c < prev    next >
C/C++ Source or Header  |  1997-12-16  |  5KB  |  213 lines

  1. /*
  2.  * mlddc
  3.  *
  4.  * Copyright 1997 Artur Skawina <skawina@usa.net>
  5.  * All Rights Reserved.
  6.  * Freely Distributable.
  7.  *
  8.  */
  9.  
  10. #define MLDDC_VERSION   "3.2.0"
  11.  
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16.  
  17. #include "config.h"
  18.  
  19. #include "netio.h"
  20.  
  21. #ifdef USE_SYSLOG
  22. #include <syslog.h>
  23. #define SYSLOG(x) x
  24. #else
  25. #define SYSLOG(x)
  26. #endif
  27.  
  28. #ifdef SECURE
  29.  
  30. #include "secure.h"
  31.  
  32. #define SECSTAT "SECURE"
  33. #define URLPORT 443
  34. typedef SSL    *netsock;
  35.  
  36. #define Connect(h,p) ( ssl_init(), ssl_connect((h),(p)) )
  37. #define Disconnect(s) {}      /* yet? */
  38.  
  39. #else
  40.  
  41. #define SECSTAT "STD"
  42. #define URLPORT 80
  43. typedef int    netsock;
  44.  
  45. #endif /*SECURE*/
  46.  
  47.  
  48.  
  49. void encbasic( char *d, const char *u, const char *p );
  50.  
  51. static const char MYDOMAIN[]  = "dyn.ml.org";
  52.  
  53. #ifndef DEBUG
  54. static const char URLHOST[]  = "members.ml.org";
  55. #else
  56. static const char URLHOST[]  = "localhost";
  57. #endif
  58.  
  59. static const char URLPATHF[] = "/mis-bin/ms3/nic/dyndns?"
  60.                   "command=Update+Host&"
  61.                   "domain=%s&"               /* (char *) myhost   */
  62.                   "act=%s&"                  /* "act" or "dec"    */
  63.                   "wildcard=on&"
  64.                   "do=mod&"
  65.                   "agree=agree";
  66.                  
  67. static const char OKSTRING[] = "MS3V STATUS:OK";
  68.  
  69.  
  70. static char             URLPATH[1024*4];
  71.  
  72.  
  73. void fatalerr( const char *s )
  74. {
  75.    /*if (s)*/
  76.       perror( s );
  77.       
  78.    SYSLOG( syslog( LOG_AUTHPRIV|LOG_NOTICE, "FAILED: %s (%m)\n", s ) );
  79.  
  80.    exit( 10 );
  81. }
  82.  
  83. int main( int argc, char *argv[] )
  84. {
  85.    static char    myhost[128],
  86.                   mymid[128],
  87.                   mypass[128];
  88.    char           *action = "act";
  89.                   
  90.    char           *result;        /*from http-query*/
  91.    char           *c;
  92.    
  93.    int len;
  94.                   
  95.  
  96.    printf( "MLDDC " MLDDC_VERSION " " SECSTAT
  97.             " Copyright 1997 Artur Skawina <skawina@usa.net>\n" );
  98.  
  99.    SYSLOG( openlog( "mlddc", 0, LOG_AUTHPRIV|LOG_NOTICE ) );
  100.  
  101.    if ( argc>1 )
  102.    {
  103.       if ( (argv[1][0] == 'd') || (argv[1][0] == 'D') )
  104.       {
  105.          action = "dec";
  106.          printf( "Deactivating\n" );
  107.       }
  108.    }
  109.    
  110.             /* Get user data */
  111.             /* and */
  112.             /* Check the user data */
  113.    if(
  114.       !gets( myhost )      ||
  115.       strlen(myhost)  <1   ||
  116.       !gets( mymid )       ||
  117.       strlen(mymid)   <3   ||
  118.       !gets( mypass )      ||
  119.       strlen(mypass)  <3
  120.      ) 
  121.      fatalerr( "Invalid user data" );
  122.      
  123.      encbasic( mypass, mymid, mypass );
  124.      
  125. #ifdef DEBUG                                 
  126.    printf( "%s\n", URLPATH );
  127. #endif
  128.     
  129. {
  130.     static char   buf[1024*64];
  131.     int           i;
  132.     netsock       sock;
  133.  
  134.     sock = Connect( URLHOST, URLPORT );
  135.         
  136.             /* Prepare the URL */
  137.             
  138.    sprintf( URLPATH, URLPATHF, myhost, action );
  139.  
  140.    sprintf( buf, "GET %s HTTP/1.0\r\n"
  141.                               "Host: %s\r\n"
  142.                               "Pragma: no-cache\r\n"
  143.                               "User-Agent: mlddc/" MLDDC_VERSION "/" SECSTAT "\r\n"
  144.                               "Authorization: Basic %s\r\n"
  145.                               "\r\n",
  146.                                         URLPATH, URLHOST, mypass );
  147.     
  148.    if ( write( sock, buf, strlen( buf ) ) != (ssize_t)strlen( buf ) )
  149.       fatalerr( "write( buf )" );
  150.    
  151.    if ( (i=read(sock, buf, sizeof(buf)-1)) <=0 )
  152.       fatalerr( "read" );
  153.         
  154.     buf[i] = '\0';
  155.     
  156.     if ( (c=strstr( buf, "\r\n\r\n" )) )
  157.         c+=4;
  158.     else if ( (c=strstr( buf, "\n\n" )) )
  159.         c+=2;
  160.  
  161.     if ( c==0 )
  162.        fatalerr("Error: Unable to find HTTP header");
  163.  
  164. //P     puts( c );
  165.     
  166.            /* Check if OKSTRING is present */
  167.    
  168.    len=i;
  169.  
  170.     if ( !(result=strstr( buf, OKSTRING )) )
  171.        while( (i=read(sock, buf+len, sizeof(buf)-1-len))>0 )
  172.        {
  173.          len += i;
  174.          buf[len] = '\0';
  175.           
  176. //P         puts( buf );
  177.     
  178.           result=strstr( buf, OKSTRING );
  179.           
  180.           if ( result )
  181.              break;
  182.        }
  183.  
  184.     Disconnect( sock );
  185. }
  186.  
  187.    if ( result )
  188.    {
  189.       printf( "Host %s.%s updated.\n", myhost, MYDOMAIN );
  190.       
  191.       SYSLOG( syslog( LOG_AUTHPRIV|LOG_NOTICE, "Host %s.%s updated.\n", myhost, MYDOMAIN ) );
  192.  
  193.       result += 5;
  194.       while ( *result != '-' )
  195.          putchar( *result++ );
  196.       putchar( '\n' );
  197.       fflush( stdout );
  198.    }
  199.    else
  200.    {
  201.       puts( c );
  202.       printf( "Host %s.%s NOT updated.\n", myhost, MYDOMAIN );
  203.       fflush( stdout );
  204.       
  205.       SYSLOG( syslog( LOG_AUTHPRIV|LOG_NOTICE, "Host %s.%s NOT updated.\n", myhost, MYDOMAIN ) );
  206.  
  207.       return 1;
  208.    }
  209.  
  210.    return 0;
  211. }
  212. /*EOF*/
  213.