home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / dn104os2.zip / dyn.c next >
C/C++ Source or Header  |  1997-07-12  |  7KB  |  282 lines

  1. /*
  2.    DYN -- a simple DYNdns client for the Monolith Coalition
  3.  
  4.    Written by Jon Klippenstein <random@ocii.com>
  5.    Copyright (C) 1997 Jon Klippenstein
  6.  
  7.    Network code based in part on fetchwww.c 1.0 by Artur Skawina
  8.    <skawina@usa.net>
  9.  
  10.    The dyn.ml.org domain is hardcoded in, this will have to be changed if
  11.    the dynamic domain changes.
  12.  
  13.    OS'es tested on:
  14.    Linux 2.0.27 i386 (my 386DX/40 infinity.dyn.ml.org/dparrot.bohica.net (UUCP))
  15.    SunOS 5.5 sparc (my ISP: nephi.ocii.com)
  16.    FreeBSD
  17.  
  18.  
  19.    Version: 1.0.4
  20.  
  21.    $Header: /root/Projects/dyn-1.0.4/RCS/dyn.c,v 1.6 1997/05/22 01:16:05 root Exp $
  22.  
  23.    $Log: dyn.c,v $
  24.    Revision 1.6  1997/05/22 01:16:05  root
  25.    fixed small stupid bug made by me :)
  26.  
  27.    Revision 1.5  1997/05/22 00:36:28  root
  28.    Added a few things, like commandline hiding, and support for expire-time and expire-date, and tagline.
  29.    generally cleaned up the source a bit.
  30.  
  31.    Revision 1.4  1997/02/01 20:36:46  root
  32.    added sysname,machine check with uname
  33.    added syslog stuff
  34.    added sleep 5 bit to readme
  35.  
  36.    Revision 1.3  1997/01/31 23:52:42  root
  37.    Added major funtionality
  38.    Added usage message
  39.    Added magic token checking
  40.    Added whole pile 'o crap :)
  41.  
  42.    Revision 1.2  1997/01/31 03:36:24  root
  43.    Made it work right.
  44.    Using some of the network code from Artur Skawina's fetchwww.c
  45.  
  46.    Revision 1.1  1997/01/31 01:10:50  root
  47.    Initial revision
  48.  
  49.    OS/2 Note:  #define SYSLOG       and compile with    -lsyslog
  50.                if you want to use it and have found syslog.a, I didn't.
  51.  */
  52.  
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <strings.h>
  56. #include <sys/types.h>
  57. #include <sys/socket.h>
  58. #include <errno.h>
  59. #include <unistd.h>
  60. #include <netinet/in.h>
  61. #include <netdb.h>
  62. #include <arpa/inet.h>
  63. #include <sys/utsname.h>    /* for uname functions */
  64. #ifdef SYSLOG
  65. #include <syslog.h>
  66. #endif
  67.  
  68. #define NAME        "DYN"
  69. #define VERSION     "1.0.4"
  70.  
  71. /* Uncomment DEBUG if you want to see raw HTML output from the server
  72.    (not neccessary unless you REALLY want to know :)) */
  73. /* NOTE: could also add -DDEBUG on gcc commandline */
  74. /* #define        DEBUG */
  75.  
  76. /* These are just in here so I can change them easily if the server or
  77.    path the freed.cgi changes (or if the WWW port number changes!! :)) */
  78. #define WWW_PORT    80
  79. #define SERVER        "scripts.ml.org"
  80. #define SCRIPT        "/mis-bin/freed.cgi"
  81.  
  82. int strtcmp(char *str1, char *str2)
  83. {
  84.   return (strncmp(str1, str2, strlen(str2)));
  85. }
  86.  
  87. void nuke_string(char *targetstring)
  88. {
  89.   char *mystring = targetstring;
  90.   while (*targetstring != '\0') {
  91.     *targetstring = ' ';
  92.     targetstring++;
  93.   }
  94.   *mystring = '\0';
  95. }
  96.  
  97. int main(int argc, void *argv[])
  98. {
  99.   struct sockaddr_in sin;
  100.   struct hostent *host;
  101.   int sock, port, connected;
  102.   char send[1024];
  103.   char temp[1024];
  104.   int i;
  105.   FILE *rfile;
  106.   struct utsname uts;
  107.   int sec1, sec2, sec3;
  108.   char mid[10];
  109.   char hostname[10];
  110.   char ip[16];
  111.   unsigned long int rawip;
  112.  
  113.   printf("%s %s by Jon Klippenstein <random@ocii.com>  Copyright (C) 1997\n", NAME, VERSION);
  114.   printf("Modified and compiled for OS/2 by Samuel Audet <guardia@cam.org>\n");
  115. #ifdef DEBUG
  116.   printf("*** DEBUG MODE ***\n");
  117. #endif
  118. #ifdef SYSLOG
  119.   openlog(argv[0], LOG_PID, LOG_USER);
  120. #endif
  121.  
  122.   if (argc < 6) {
  123.     printf("\nusage: %s MID SEC1 SEC2 SEC3 MACHINE IP\n", argv[0]);
  124.     printf("\tMID\tyour Monolith Coalition ID\n");
  125.     printf("\tSEC1\tthe first number in your security code\n");
  126.     printf("\tSEC2\tthe second number in your security code\n");
  127.     printf("\tSEC3\tthe third number in your security code\n");
  128.     printf("\tMACHINE\tyour Dynamic Domain Name, such as xpl.\n");
  129.     printf("\tIP\tyour machine's CURRENT dynamic IP (optional).\n\n");
  130.     printf("Example: %s klip1 11 22 33 xpl 123.123.123.123 \n\n", argv[0]);
  131.     exit(1);
  132.   }
  133.   uname(&uts);
  134.  
  135.   strcpy(mid, argv[1]);
  136.   sec1 = atoi(argv[2]);
  137.   sec2 = atoi(argv[3]);
  138.   sec3 = atoi(argv[4]);
  139.   strcpy(hostname, argv[5]);
  140.   if (argc > 6) {
  141.      strcpy(ip, argv[6]);
  142.      nuke_string(argv[6]);
  143.   }
  144.   else {
  145.      rawip = gethostid();
  146.      sprintf(ip, "%d.%d.%d.%d", rawip>>24,rawip<<8>>24,rawip<<16>>24,rawip<<24>>24);
  147.   }
  148.  
  149.   nuke_string(argv[0]);
  150.   nuke_string(argv[1]);
  151.   nuke_string(argv[2]);
  152.   nuke_string(argv[3]);
  153.   nuke_string(argv[4]);
  154.   nuke_string(argv[5]);
  155.   sprintf(argv[0], "%s %s", NAME, VERSION);
  156.  
  157.   printf("Host:\t%s.dyn.ml.org\n", hostname);
  158.   printf("IP:\t%s\n", ip);
  159.   printf("MID:\t%s\n", mid);
  160.   printf("MACTYP:\t%s\n", uts.machine);
  161.   printf("OS:\t%s\n", uts.sysname);
  162.  
  163.   sin.sin_family = AF_INET;
  164.   sin.sin_port = htons(80);
  165.  
  166.   if (!(host = gethostbyname(SERVER))) {
  167. #ifdef SYSLOG
  168.     syslog(LOG_WARNING, "gethostbyname: %m");
  169. #endif
  170.     perror("gethostbyname");
  171.     exit(1);
  172.   }
  173.   memcpy((char *) &sin.sin_addr, host->h_addr, host->h_length);
  174.  
  175.   printf("\nConnecting to %s...", SERVER);
  176.  
  177.   if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
  178. #ifdef SYSLOG
  179.     syslog(LOG_WARNING, "socket: %m");
  180. #endif
  181.     perror("socket");
  182.     exit(1);
  183.   }
  184.   if (connect(sock, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
  185. #ifdef SYSLOG
  186.     syslog(LOG_WARNING, "connect: %m");
  187. #endif
  188.     perror("connect:");
  189.     exit(1);
  190.   }
  191.   printf("connnected.\n");
  192.  
  193.   /* Now create line to send to HTTP server */
  194.  
  195.   printf("Sending update...");
  196.  
  197.   /* NEW 1.0.4: Added expire-date and expire-time fields for expiration,
  198.      if it ever becomes real.  For now they are set to 0.  Also added tagline
  199.      support.  Don't know if these have been implemented on the server
  200.      yet.  Removed mactype and os. */
  201.  
  202.   /* This sprintf is REALLY ugly.  Do something about it Jon :) */
  203.  
  204.   sprintf(send, "GET %s?do=mod\
  205. &type=machine\
  206. &domain=%s\
  207. &db=DYN.ML.ORG\
  208. &ipaddr=%s\
  209. &WWW=yes\
  210. &mail=\
  211. &mactype=%s\
  212. &os=%s\
  213. &expire-date=0\
  214. &expire-time=0\
  215. &tagline=DYN+%s/%s\
  216. &MID=%s\
  217. &sec1=%d\
  218. &sec2=%d\
  219. &sec3=%d\
  220. &agree=agree\n",
  221.       SCRIPT,
  222.       hostname,
  223.       ip,
  224.       uts.machine,
  225.       uts.sysname,
  226.       VERSION,
  227.       uts.sysname,
  228.       mid,
  229.       sec1,
  230.       sec2,
  231.       sec3);
  232.  
  233. #ifdef DEBUG
  234.   printf("\n\n%s\n\n", send);
  235. #endif
  236.  
  237.   write(sock, send, strlen(send));
  238.  
  239.   printf("done.\n");
  240.  
  241.   printf("Waiting for reply...");
  242.  
  243.   rfile = fdopen(sock, "r");
  244.  
  245.   while (!feof(rfile)) {
  246.     fgets(temp, (int) sizeof(temp), (FILE *) rfile);
  247.  
  248. #ifdef DEBUG
  249.     printf("%s", temp);
  250. #endif
  251.  
  252. /* Check for the ml-host-added magic token as explained by Aveek Datta:
  253.  
  254.    MTTS has been updated to remove the "DYNDNS isn ot available yet".  And as
  255.    someone asked, there is now a magic token in the return that you can test
  256.    for:
  257.  
  258.    <!-- ML-HOST-ADDED -->
  259.  
  260.    is present if the host was added/modified OK
  261.  
  262.  */
  263.  
  264.     if (strtcmp(temp, "<!-- ML-HOST-ADDED -->") == 0) {
  265.       printf("\n\n%s [%s] was updated successfully.\n", hostname, ip);
  266. #ifdef SYSLOG
  267.       syslog(LOG_INFO, "%s [%s] update successful.", hostname, ip);
  268. #endif
  269.       fclose(rfile);
  270.       close(sock);
  271.       exit(0);
  272.     }
  273.   }
  274.  
  275.   printf("\nERROR: ML-HOST-ADDED magic token not found in reply.\nHost NOT updated.\n");
  276. #ifdef SYSLOG
  277.   syslog(LOG_ERR, "ERROR: ML-HOST-ADDED not found, update unsuccessful.");
  278. #endif
  279.   fclose(rfile);
  280.   close(sock);
  281. }
  282.