home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 November / PCONLINE_11_99.ISO / filesbbs / OS2 / APCHSSL2.ZIP / OS2HTTPD / bin / ServerConfig / daemon-unix / ServerConfigd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-20  |  6.6 KB  |  229 lines

  1. /*
  2. ** ServerConfigd for OS/2 and Unix/Linux systems, v1.3
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <errno.h>
  7. #include <string.h>
  8. #include <sys/types.h>
  9. #include <netinet/in.h>
  10. #include <sys/socket.h>
  11. #include <sys/wait.h>
  12. #include <fcntl.h>
  13. #include <sys/stat.h>
  14.  
  15. #define MAXDATASIZE 1024
  16. #define BACKLOG 2
  17.  
  18. int sockfd, new_fd;
  19. int sin_size, numbytes, buf_size=0;
  20. char buf[MAXDATASIZE], buf1[MAXDATASIZE];
  21.  
  22. void Send(void) {
  23.  if (send(new_fd, buf, buf_size, 0) == -1)
  24.   perror("send");
  25. }
  26.  
  27. void Receive(void) {
  28.  if ((numbytes=recv(new_fd, buf, MAXDATASIZE, 0)) == -1) {
  29.   perror("recv");
  30.  }
  31.  if (numbytes > 0) buf[numbytes] = '\0';
  32. }
  33.  
  34. int main(int argc, char *argv[])
  35. {
  36. struct sockaddr_in my_addr;
  37. struct sockaddr_in their_addr;
  38. char PAZZ[80], APACHECONFIG[80], PIDCONFIG[80];
  39. int ALLOWRESET=0, ALLOWSHUTDOWN=0, MYPORT=0, Recv_size=0, Pazz_ok=0;
  40. char Config_buffer[1024], Config_Label[80], Config_Setting[80];
  41. FILE *ConfigFile, *ApacheFile, *PidFile, *Config_read;
  42.  
  43. #ifdef __EMX__
  44.  strcpy(Config_buffer,"ServerConfigd.cfg");
  45. #else
  46.  strcpy(Config_buffer,"/etc/ServerConfigd.cfg");
  47. #endif
  48.  
  49. if(argc > 1) {
  50.  strcpy(Config_buffer, *++argv);
  51. }
  52.  
  53.     if((ConfigFile = fopen(Config_buffer, "r")) == NULL) {
  54.      printf("Error opening configuration file.\n");
  55.      exit(1);
  56.     }
  57.     else
  58.      while(!feof(ConfigFile)) {
  59.       fgets(Config_buffer, sizeof(Config_buffer), ConfigFile);
  60.       if(Config_buffer[0]!='#' && Config_buffer[0]!='\n') {
  61.        sscanf(Config_buffer, "%s %s", Config_Label, Config_Setting);
  62.        if(!strcmp(Config_Label,"Port")) {
  63.         MYPORT=atoi(Config_Setting);
  64.        }
  65.        if(!strcmp(Config_Label,"Password")) {
  66.         strcpy(PAZZ,Config_Setting);
  67.        }
  68.        if(!strcmp(Config_Label,"ApacheConfig")) {
  69.         strcpy(APACHECONFIG,Config_Setting);
  70.        }
  71.        if(!strcmp(Config_Label,"PidFile")) {
  72.         strcpy(PIDCONFIG,Config_Setting);
  73.        }
  74.        if(!strcmp(Config_Label,"AllowReset")) {
  75.         ALLOWRESET=1;
  76.        }
  77.        if(!strcmp(Config_Label,"AllowShutDown")) {
  78.         ALLOWSHUTDOWN=1;
  79.        }
  80.       }
  81.      }
  82.     fclose(ConfigFile);
  83.  
  84.     if(MYPORT==0) { printf("Error, no port found.\n"); exit(1); }
  85.  
  86.     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  87.      perror("socket");
  88.      exit(1);
  89.     }
  90.  
  91.     my_addr.sin_family = AF_INET;         /* host byte order */
  92.     my_addr.sin_port = htons(MYPORT);     /* short, network byte order */
  93.     my_addr.sin_addr.s_addr = INADDR_ANY; /* automatically fill with my IP */
  94.     bzero(&(my_addr.sin_zero), 8);        /* zero the rest of the struct */
  95.  
  96.     if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
  97.      perror("bind");
  98.      exit(1);
  99.     }
  100.  
  101.     if (listen(sockfd, BACKLOG) == -1) {
  102.      perror("listen");
  103.      exit(1);
  104.     }
  105.  
  106.     while(1) {
  107.         sin_size = sizeof(struct sockaddr_in);
  108.         if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
  109.          perror("accept");
  110.          continue;
  111.         }
  112.         //printf("server: got connection from %s\n",inet_ntoa(their_addr.sin_addr));
  113.  
  114.         Receive();
  115.         if(strcmp(buf,"[RemoteSC] Hello")==0) {
  116.          strcpy(buf,"[RemoteD] Welcome");
  117.          buf_size=strlen(buf);
  118.          Send();
  119.          Receive();
  120.  
  121.          strcpy(buf1,"[RemoteSC] LOAD ");
  122.          if(strcmp(buf,strcat(buf1, PAZZ))==0) {
  123.           Pazz_ok=1;
  124.           if((Config_read = fopen(APACHECONFIG, "rb")) == NULL) {
  125.            strcpy(buf, "[RemoteD] BAD");
  126.            buf_size=strlen(buf);
  127.            Send();
  128.           } else {
  129.            fseek(Config_read,0l,SEEK_END);
  130.            sprintf(buf, "%d", ftell(Config_read));
  131.            buf_size=strlen(buf);
  132.            fclose(Config_read);
  133.            Send();
  134.           }
  135.           if((ApacheFile = fopen(APACHECONFIG, "rb")) == NULL) { }
  136.            //printf("Error opening configuration file.\n");
  137.           else {
  138.            while(!feof(ApacheFile)) {
  139.             buf_size=fread(Config_buffer, 1, 1024, ApacheFile);
  140.             strcpy(buf, Config_buffer);
  141.             /*buf_size=strlen(buf);*/
  142.             Send();
  143.            }
  144.           }
  145.          }
  146.  
  147.          strcpy(buf1,"[RemoteSC] SAVE ");
  148.          if(strcmp(buf,strcat(buf1, PAZZ))==0) {
  149.           Pazz_ok=1;
  150.           if((ApacheFile=fopen(APACHECONFIG, "wb")) == NULL) {
  151.            strcpy(buf, "[RemoteD] BAD");
  152.            buf_size=strlen(buf);
  153.            Send();
  154.           } else {
  155.            strcpy(buf, "[RemoteD] OK");
  156.            buf_size=strlen(buf);
  157.            Send();
  158.            Receive();
  159.            Recv_size=atoi(buf);
  160.            strcpy(buf, "[RemoteD] OK");
  161.            buf_size=strlen(buf);
  162.            Send();
  163.            buf_size=0;
  164.            while (buf_size < Recv_size) {
  165.             Receive();
  166.             fwrite(buf, strlen(buf), 1, ApacheFile);
  167.             buf_size = buf_size + strlen(buf);
  168.            }
  169.            fclose(ApacheFile);
  170.            strcpy(buf, "[RemoteD] OK");
  171.            buf_size=strlen(buf);
  172.            Send();
  173.           }
  174.          }
  175.  
  176.          strcpy(buf1,"[RemoteSC] RESET ");
  177.          if(strcmp(buf,strcat(buf1, PAZZ)) == 0) {
  178.           Pazz_ok=1;
  179.           if((PidFile=fopen(PIDCONFIG, "rb")) == NULL || ALLOWRESET==0) {
  180.            strcpy(buf, "[RemoteD] BAD");
  181.            buf_size=strlen(buf);
  182.            Send();
  183.           } else {
  184.            fread(Config_buffer, 1, 1024, PidFile);
  185.            fclose(PidFile);
  186.            buf_size=atoi(Config_buffer);
  187.            if((kill(buf_size,1)) == 0) {
  188.             strcpy(buf, "[RemoteD] OK");
  189.             buf_size=strlen(buf);
  190.             Send();
  191.            } else {
  192.             strcpy(buf, "[RemoteD] BAD");
  193.             buf_size=strlen(buf);
  194.             Send();
  195.            }
  196.           }
  197.          }
  198.  
  199.          strcpy(buf1,"[RemoteSC] SHUTDOWN ");
  200.          if(strcmp(buf,strcat(buf1, PAZZ)) == 0) {
  201.           Pazz_ok=1;
  202.           if((PidFile = fopen(PIDCONFIG, "rb")) == NULL || ALLOWSHUTDOWN==0) {
  203.            strcpy(buf, "[RemoteD] BAD");
  204.            buf_size=strlen(buf);
  205.            Send();
  206.           } else {
  207.            fread(Config_buffer, 1, 1024, PidFile);
  208.            fclose(PidFile);
  209.            buf_size=atoi(Config_buffer);
  210.            kill(buf_size,15);
  211.            strcpy(buf, "[RemoteD] OK");
  212.            buf_size=strlen(buf);
  213.            Send();
  214.           }
  215.          }
  216.  
  217.          if(Pazz_ok==0) {
  218.            strcpy(buf, "[RemoteD] BAD");
  219.            buf_size=strlen(buf);
  220.            Send();
  221.          }
  222.  
  223.         }
  224.         while(waitpid(-1,NULL,WNOHANG) > 0); /* clean up all child processes */
  225.     }
  226.     close(new_fd);
  227. }
  228.  
  229.