home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 May / PCO_5_97.ISO / FilesBBS / OS2 / WWWCNT15.ARJ / WWWCNT15 / WWWCNT15.ZIP / utils.c < prev   
Encoding:
C/C++ Source or Header  |  1995-09-17  |  22.0 KB  |  1,093 lines

  1. #include "count.h"
  2. #include "config.h"
  3.  
  4.  
  5. /*
  6.  *  ParseAuthorizationList() -   parses the authorization list
  7.  *
  8.  *  RCS:
  9.  *      $Revision: 1.3 $
  10.  *      $Date: 1995/07/16 17:02:55 $
  11.  *
  12.  *  Security:
  13.  *      Unclassified
  14.  *
  15.  *  Description:
  16.  *      text
  17.  *
  18.  *  Input Parameters:
  19.  *      type    identifier  description
  20.  *
  21.  *      text
  22.  *
  23.  *  Output Parameters:
  24.  *      type    identifier  description
  25.  *
  26.  *      text
  27.  *
  28.  *  Return Values:
  29.  *      value   description
  30.  *
  31.  *  Side Effects:
  32.  *      text
  33.  *
  34.  *  Limitations and Comments:
  35.  *      text
  36.  *
  37.  *  Development History:
  38.  *      who                 when        why
  39.  *      muquit@semcor.com   05-Jun-95   first cut
  40.  */
  41.  
  42. int ParseAuthorizationList ()
  43. {
  44.     FILE
  45.         *fp;
  46.  
  47.     int
  48.         i;
  49.  
  50.     char
  51.         buf[MaxLineLength+1];
  52.  
  53.     i=0;
  54.  
  55.     *buf='\0';
  56.     (void) sprintf(buf,"%s/%s",ConfigDir,ConfigFile);
  57.     fp = fopen(buf, "r");
  58.     if (fp == (FILE *) NULL)
  59.     {
  60.         return (ConfigOpenFailed);
  61.     }
  62.  
  63.     *buf='\0';
  64.     (void) GetLine (fp, buf);
  65.     if (strcmp (buf, "{") != 0)
  66.     {
  67.         (void) fclose ((FILE *) fp);
  68.         return (NoIgnoreHostsBlock);
  69.     }
  70.  
  71.     while (True)
  72.     {
  73.         if (!GetLine (fp, buf))
  74.         {
  75.             (void) fclose ((FILE *) fp);
  76.             return (UnpexpectedEof);
  77.         }
  78.  
  79.         if (strcmp(buf, "}") == 0)
  80.             break;
  81.         RemoveTrailingSp (buf);
  82.         GignoreSite[Gsite++] = mystrdup (buf);
  83.     }
  84.  
  85.     if (Gdebug == True)
  86.     {
  87.         if (Gsite > 0)
  88.             (void) fprintf (stderr,"Ignore Hosts:\n");
  89.         else
  90.             (void) fprintf (stderr," Access from any hosts counted!\n");
  91.         for (i=0; i < Gsite; i++)
  92.         {
  93.             (void) fprintf (stderr," %s\n", GignoreSite[i]);
  94.         }
  95.             (void) fprintf (stderr,"\n");
  96.     }
  97.  
  98. #ifdef ACCESS_AUTH
  99.     (void) GetLine (fp, buf);
  100.     if (strcmp(buf, "{") != 0)
  101.     {
  102.        (void) fclose ((FILE *) fp);
  103.         return (NoRefhBlock);
  104.     }
  105.     while (True)
  106.     {
  107.         if (!GetLine (fp, buf))
  108.         {
  109.             (void) fclose ((FILE *) fp);
  110.             return (UnpexpectedEof);
  111.         }
  112.         if (strcmp (buf, "}") == 0)
  113.             break;
  114.         RemoveTrailingSp (buf);
  115.         GrefererHost[Grhost++]= mystrdup(buf);
  116.  
  117.     }
  118.  
  119.     if (Gdebug == True)
  120.     {
  121.         if (Grhost > 0)
  122.             (void) fprintf (stderr,"-Referer Hosts-\n");
  123.         else
  124.             (void) fprintf (stderr,"Grhost:%d\n",Grhost);
  125.         for (i=0; i < Grhost; i++)
  126.         {
  127.             (void) fprintf (stderr," %s\n", GrefererHost[i]);
  128.         }
  129.         (void) fprintf (stderr,"\n");
  130.     }
  131.  
  132. #endif /* ACCESS_AUTH */
  133.     (void) fclose ((FILE *) fp);
  134.     return (0);
  135. }
  136.  
  137. #ifdef TEST
  138.  
  139. void main (argc, argv)
  140. int
  141.     argc;
  142. char
  143.     **argv;
  144. {
  145.     ParseAuthorizationList ();
  146. }
  147. #endif /* TEST */
  148.  
  149.  
  150. /*
  151.  *  GetLine () - reads a line from the passed file pointer and puts the
  152.  *               line in the passed array
  153.  *
  154.  *  RCS:
  155.  *      $Revision: 1.3 $
  156.  *      $Date: 1995/07/16 17:02:55 $
  157.  *
  158.  *  Security:
  159.  *      Unclassified
  160.  *
  161.  *  Description:
  162.  *      borrowed from wusage 2.3
  163.  *
  164.  *  Input Parameters:
  165.  *      type    identifier  description
  166.  *
  167.  *      text
  168.  *
  169.  *  Output Parameters:
  170.  *      type    identifier  description
  171.  *
  172.  *      text
  173.  *
  174.  *  Return Values:
  175.  *      value   description
  176.  *
  177.  *  Side Effects:
  178.  *      text
  179.  *
  180.  *  Limitations and Comments:
  181.  *      text
  182.  *
  183.  *  Development History:
  184.  *      who                 when        why
  185.  *      muquit@semcor.com   05-Jun-95   first cut
  186.  */
  187.  
  188. int GetLine (fp, string)
  189. FILE
  190.     *fp;
  191. char
  192.     *string;
  193. {
  194.     int
  195.         s,
  196.         i;
  197.  
  198.     int
  199.         length;
  200.  
  201.     char
  202.         *x;
  203.     while (!feof (fp))
  204.     {
  205.         x = fgets (string, 80, fp);
  206.         if (x == (char *) NULL)
  207.             return (0);
  208.  
  209.         if (*string == '#') /* a comment */
  210.             continue;
  211.  
  212.         if (string[(int) strlen(string)-1] == '\n')
  213.             string[(int) strlen(string)-1] = '\0'; /* NULL terminate*/
  214.  
  215.         length = (int) strlen(string);
  216.         s=0;
  217.  
  218.         for (i=0; i < length; i++)
  219.         {
  220.             if (isspace (string[i]))
  221.                 s++;
  222.             else
  223.                 break;
  224.         }
  225.  
  226.         if (s)
  227.         {
  228.             char buf[81];
  229.             (void) strcpy (buf, string+s);
  230.             (void) strcpy(string,buf);
  231.         }
  232.  
  233.         length = (int) strlen(string);
  234.         for (i=(length-1); i >= 0; i--)
  235.         {
  236.             if (isspace(string[i]))
  237.                 string[i]='\0';
  238.             else
  239.                 break;
  240.         }
  241.         return (1);
  242.     }
  243.     return (0);
  244. }
  245.  
  246. /*
  247. ** from wusage 2.3
  248. */
  249. void RemoveTrailingSp (string)
  250. char
  251.     *string;
  252. {
  253.     while (True)
  254.     {
  255.         int
  256.             l;
  257.  
  258.         l = (int) strlen (string);
  259.         if (!l)
  260.             return;
  261.  
  262.         if (string[l-1] == ' ')
  263.             string[l-1] = '\0';
  264.         else
  265.             return;
  266.     }
  267. }
  268.  
  269. /*
  270. ** duplicate a string
  271. */
  272. char *mystrdup (string)
  273. char
  274.     *string;
  275. {
  276.     char
  277.         *tmp;
  278.  
  279.     if (string == (char *) NULL)
  280.         return ((char *) NULL);
  281.  
  282.     tmp = (char *) malloc ((int) strlen(string) + 1);
  283.  
  284.     if (tmp == (char *) NULL)
  285.         return ((char *) NULL);
  286.  
  287.     (void) strcpy(tmp, string);
  288.     return (tmp);
  289. }
  290.  
  291. /*
  292. ** check if the data file has correct ownership
  293. */
  294.  
  295. /*
  296. ** Return values
  297. ** 0 - nothing matches (group or owner)
  298. ** 1 - owner of the file and the owner found in query string matches
  299. ** 2 - memory allocation problem
  300. ** 3 - owner of the file and the owner found in query string matches but
  301. **     the group id of the child process of httpd and the group id of the
  302. **     file does not match
  303. */
  304.  
  305. #ifdef _USE_ME_PLEASE_
  306. int CheckOwner (owner,file)
  307. char
  308.     *owner;
  309. char
  310.     *file;
  311. {
  312.     char
  313.         *tmp;
  314.  
  315.     struct stat
  316.         statbuf;
  317.  
  318.     int
  319.         uid,
  320.         gid;
  321.  
  322.     struct passwd
  323.         *p;
  324.  
  325.     tmp = mystrdup(owner);
  326.     if (tmp == (char *) NULL)
  327.         return 2;
  328.  
  329.     while ((p=getpwent()) != NULL)
  330.     {
  331.         if (strcmp(tmp, p->pw_name) == 0)
  332.         {
  333.             uid = p->pw_uid;                        
  334.             stat(file, &statbuf);
  335.             if (uid == statbuf.st_uid)
  336.             {
  337.                 /*
  338.                 ** now check the group id of the child process of httpd
  339.                 ** and group id of the data file, they must match
  340.                 */
  341.                 gid = getgid();
  342.                 if (gid != statbuf.st_gid)
  343.                 {
  344.                     char
  345.                         buf[BUFSIZ];
  346.                     *buf = '\0';
  347.                     (void) sprintf (buf, "Group Id of the counter data file \"%s\" is %d, it should be %d, httpd's child processes run with group id %d",file,statbuf.st_gid,gid,gid);
  348.                 Warning(buf);
  349.                 (void) free ((char *) tmp);
  350.                     return 3;
  351.                 }
  352.                 (void) free ((char *) tmp);
  353.                 return (1);
  354.             }
  355.             else
  356.             {
  357.                 (void) free ((char *) tmp);
  358.                 return (0); 
  359.             }
  360.         } 
  361.     }
  362.  
  363.     (void) free ((char *) tmp);
  364.     return (0);
  365. }
  366. #endif /* _USE_ME_PLEASE */
  367. /*
  368.  *  ParseQueryString() - parses the QUERY_STRING for Count.cgi
  369.  *
  370.  *  RCS:
  371.  *      $Revision$
  372.  *      $Date$
  373.  *
  374.  *  Security:
  375.  *      Unclassified
  376.  *
  377.  *  Description:
  378.  *      text
  379.  *
  380.  *  Input Parameters:
  381.  *      type    identifier  description
  382.  *
  383.  *      char        *qs
  384.  *      DigitInfo   *digit_info
  385.  *      FrameInfo   *frame_info
  386.  *
  387.  *  Output Parameters:
  388.  *      type    identifier  description
  389.  *
  390.  *      DigitInfo   *digit_info
  391.  *      FrameInfo   *frame_info
  392.  *
  393.  *  Return Values:
  394.  *      value   description
  395.  *      0   on success
  396.  *      1   on failure
  397.  *
  398.  *  Side Effects:
  399.  *      text
  400.  *
  401.  *  Limitations and Comments:
  402.  *      text
  403.  *
  404.  *  Development History:
  405.  *      who                 when        why
  406.  *      muquit@semcor.com   22-Aug-95   first cut
  407.  */
  408.  
  409.  
  410. int ParseQueryString(qs, digit_info, frame_info)
  411. char
  412.     *qs;
  413. DigitInfo
  414.     *digit_info;
  415. FrameInfo
  416.     *frame_info;
  417. {
  418.     char
  419.         query_string[MaxTextLength],
  420.         buf1[50], 
  421.         buf2[50], 
  422.         buf3[50], 
  423.         buf4[50], 
  424.         buf5[50], 
  425.         buf6[50],
  426.         buf7[50],
  427.         buf8[50],
  428.         buf9[50],
  429.         buf10[50];
  430.  
  431.     register char
  432.         *p;
  433.  
  434.     int
  435.         thickness,
  436.         tr,
  437.         width,
  438.         height,
  439.         maxdigits,
  440.         r,
  441.         g,
  442.         b,
  443.         trr,
  444.         tg,
  445.         tb,
  446.         st,
  447.         sh;
  448.  
  449.     int
  450.         rc=0;
  451.  
  452.     *query_string='\0';
  453.     *buf1='\0';
  454.     *buf2='\0';
  455.     *buf3='\0';
  456.     *buf4='\0';
  457.     *buf5='\0';
  458.     *buf6='\0';
  459.     *buf7='\0';
  460.     *buf8='\0';
  461.     *buf9='\0',
  462.     *buf10='\0';
  463.  
  464.     (void) strcpy(query_string,qs);
  465.     for (p=query_string; *p != '\0'; p++)
  466.     {
  467.         if ((*p == '=') || (*p == '|') || (*p == ';'))
  468.             *p=' '; 
  469.     }
  470.     if (Gdebug == True)
  471.     (void) fprintf (stderr," QUERY_STRING (tokenized): \n%s\n", query_string);
  472.  
  473.    rc=sscanf(query_string, "%s %d %s %d %d %d %s %d %s %d %d %d %s %d %d %s %d %s %s %s %d %s %d %s %s",
  474.         buf1,&thickness,buf2,&r,&g,&b,buf3,&tr,buf4,&trr,&tg,&tb,
  475.         buf5,&width,&height, buf6,&maxdigits,
  476.         buf7,digit_info->ddhead,buf8,&st,buf9,&sh,buf10,digit_info->datafile);
  477.  
  478.     if (rc != 25)
  479.     {
  480.         rc=1;
  481.         goto ExitProcessing;
  482.     }
  483.     
  484.     if (strncmp(buf1,"ft",2) != 0)
  485.     {
  486.         if (Gdebug == True)
  487.             (void) fprintf (stderr," buf1 != ft\n");
  488.  
  489.         PrintHeader ();
  490.         StringImage("Problem with ft in QUERY_STRING");
  491.         rc=1; 
  492.         goto ExitProcessing;
  493.     }
  494.     else
  495.     {
  496.         if (thickness < 0)
  497.             thickness=AbsoluteValue(thickness);
  498.  
  499.         frame_info->width=thickness;
  500.     }
  501.  
  502.     if (strncmp(buf2,"frgb",3) != 0)
  503.     {
  504.         if (Gdebug == True)
  505.             (void) fprintf (stderr," buf2 != frgb\n");
  506.  
  507.         PrintHeader ();
  508.         StringImage("Problem with frgb in QUERY_STRING");
  509.         rc=1; 
  510.         goto ExitProcessing;
  511.     }
  512.     else
  513.     {
  514.         if (r < 0)
  515.             r=0;
  516.          else if (r > MaxRGB)
  517.             r=MaxRGB;
  518.         frame_info->matte_color.red=(unsigned char) r;
  519.         
  520.         if (g < 0)
  521.             g=0;
  522.          else if (g > MaxRGB)
  523.             g=MaxRGB;
  524.  
  525.         frame_info->matte_color.green=(unsigned char) g;
  526.  
  527.         if (b < 0)
  528.             b=0;
  529.         else if (b > MaxRGB)
  530.             b=MaxRGB;
  531.  
  532.         frame_info->matte_color.blue=(unsigned char) b;
  533.     }
  534.  
  535.     /*
  536.     ** tr=?
  537.     */
  538.  
  539.     if (strncmp(buf3,"tr",2) != 0)
  540.     {
  541.         if (Gdebug == True)
  542.             (void) fprintf (stderr," buf3 != tr\n");
  543.  
  544.         PrintHeader();
  545.         StringImage("Problem with tr in QUERY_STRING");
  546.         rc=1; 
  547.         goto ExitProcessing;
  548.     }
  549.     else
  550.     {
  551.         if ((tr != 0) && (tr != 1))
  552.         {
  553.         if (Gdebug == True)
  554.             (void) fprintf (stderr," tr must be 0 or 1\n");
  555.  
  556.         PrintHeader();
  557.         StringImage("tr must be 0 or 1 in QUERY_STRING");
  558.         rc=1; 
  559.         goto ExitProcessing;
  560.         }
  561.  
  562.         switch(tr)
  563.         {
  564.             case 0:
  565.             {
  566.                 digit_info->alpha=False;
  567.                 break;
  568.             }
  569.             case 1:
  570.             {
  571.                 digit_info->alpha=True;
  572.                 break;
  573.             }
  574.         } 
  575.     }
  576.  
  577.     /*
  578.     ** trgb=?;?;?
  579.     */
  580.     if (strncmp(buf4,"trgb",4) != 0)
  581.     {
  582.         if (Gdebug == True)
  583.             (void) fprintf (stderr," buf4 != trgb\n");
  584.  
  585.         PrintHeader();
  586.         StringImage("Problem with trgb in QUERY_STRING");
  587.         rc=1; 
  588.         goto ExitProcessing;
  589.     }
  590.     else
  591.     {
  592.         if (tr < 0)
  593.             tr=0;
  594.          else if (tr > MaxRGB)
  595.             tr=MaxRGB;
  596.         digit_info->alpha_red=(unsigned char) trr;
  597.         
  598.         if (tg < 0)
  599.             tg=0;
  600.          else if (tg > MaxRGB)
  601.             tg=MaxRGB;
  602.         digit_info->alpha_green=(unsigned char) tg;
  603.  
  604.         if (tb < 0)
  605.             tb=0;
  606.         else if (tb > MaxRGB)
  607.             tb=MaxRGB;
  608.         digit_info->alpha_blue=(unsigned char) tb;
  609.     }
  610.  
  611.     /*
  612.     ** wxh=??
  613.     */
  614.     if (strncmp(buf5,"wxh",3) != 0)
  615.     {
  616.         if (Gdebug == True)
  617.             (void) fprintf (stderr," buf5 != wxh\n");
  618.  
  619.         PrintHeader();
  620.         StringImage("Problem with wxh in QUERY_STRING");
  621.         rc=1; 
  622.         goto ExitProcessing;
  623.     }
  624.     else
  625.     {
  626.         if (width < 0)
  627.             width=AbsoluteValue(width);
  628.         if (height < 0)
  629.             height=AbsoluteValue(height);
  630.  
  631.         digit_info->width=(unsigned int) width;
  632.         digit_info->height=(unsigned int) height;
  633.     }
  634.  
  635.     if (strncmp(buf6,"md",2) != 0)
  636.     {
  637.         if (strncmp(buf6,"pad",3) != 0)
  638.         {
  639.         if (Gdebug == True)
  640.             (void) fprintf (stderr," buf6 != md or pad\n");
  641.  
  642.         PrintHeader();
  643.         StringImage("Problem with md in QUERY_STRING");
  644.         rc=1; 
  645.         goto ExitProcessing;
  646.         }
  647.         else
  648.         {
  649.             digit_info->leftpad=False;
  650.             digit_info->maxdigits=0;
  651.         }
  652.     }
  653.     else
  654.     {
  655.         if (maxdigits < 0)
  656.             maxdigits=AbsoluteValue(maxdigits);
  657.          digit_info->leftpad=True;
  658.          digit_info->maxdigits=maxdigits;
  659.     }
  660.  
  661.     if (strncmp(buf7,"dd",2) != 0)
  662.     {
  663.         if (Gdebug == True)
  664.             (void) fprintf (stderr," buf7 != dd\n");
  665.  
  666.         PrintHeader();
  667.         StringImage("Problem with dd in QUERY_STRING");
  668.         rc=1; 
  669.         goto ExitProcessing;
  670.     }
  671.  
  672.     if (strncmp(buf8,"st",2) != 0)
  673.     {
  674.         if (Gdebug == True)
  675.             (void) fprintf (stderr," buf8 != st\n");
  676.  
  677.         PrintHeader();
  678.         StringImage("Problem with st in QUERY_STRING");
  679.         rc=1;
  680.         goto ExitProcessing;
  681.     }
  682.     else
  683.         digit_info->st=st;
  684.  
  685.     if (strncmp(buf9,"sh",2) != 0)
  686.     {
  687.         if (Gdebug == True)
  688.             (void) fprintf (stderr," buf9 != sh\n");
  689.  
  690.         PrintHeader();
  691.         StringImage("Problem with sh in QUERY_STIRNG");
  692.         rc=1;
  693.         goto ExitProcessing;
  694.     }
  695.     else
  696.     {
  697.         if ((sh != 0) && (sh != 1))
  698.         {
  699.             if (Gdebug == True)
  700.                 (void) fprintf (stderr," sh must be 0 or 1\n");
  701.  
  702.             PrintHeader();
  703.             StringImage("sh must be 0 or 1 in QUERY_STRING");
  704.             rc=1;
  705.             goto ExitProcessing;
  706.         }
  707.         else
  708.         {
  709.             if (sh == 1)
  710.                 digit_info->show=True;
  711.             else
  712.                 digit_info->show=False;
  713.         }
  714.     }
  715.     if (strncmp(buf10,"df",2) != 0)
  716.     {
  717.         if (Gdebug == True)
  718.             (void) fprintf (stderr," buf10 != df\n");
  719.  
  720.         PrintHeader();
  721.         StringImage("Problem with df in QUERY_STRING");
  722.         rc=1; 
  723.         goto ExitProcessing;
  724.     }
  725.     if (Gdebug == True)
  726.     {
  727.         (void) fprintf (stderr," \nparsed QS--\n");
  728.         (void) fprintf (stderr," buf1=%s\n",buf1);
  729.         (void) fprintf (stderr," ft=%d\n",thickness);
  730.         (void) fprintf (stderr," buf2=%s\n",buf2);
  731.         (void) fprintf (stderr," rgb=%d,%d,%d\n",r,g,b);
  732.         (void) fprintf (stderr," buf3=%s\n",buf3);
  733.         (void) fprintf (stderr," tr=%d\n",digit_info->alpha);
  734.         (void) fprintf (stderr," buf4=%s\n",buf4);
  735.         (void) fprintf (stderr," ar=%d\n",digit_info->alpha_red);
  736.         (void) fprintf (stderr," ag=%d\n",digit_info->alpha_green);
  737.         (void) fprintf (stderr," ab=%d\n",digit_info->alpha_blue);
  738.         (void) fprintf (stderr," buf5=%s\n",buf5);
  739.         (void) fprintf (stderr," wxh=%dx%d\n",width,height);
  740.         (void) fprintf (stderr," buf6=%s\n",buf6);
  741.         (void) fprintf (stderr," md=%d\n",maxdigits);
  742.         (void) fprintf (stderr," buf7=%s\n",buf7);
  743.         (void) fprintf (stderr," ddhead=%s\n",digit_info->ddhead);
  744.         (void) fprintf (stderr," buf8=%s\n",buf8);
  745.         (void) fprintf (stderr," st=%d\n",digit_info->st);
  746.         (void) fprintf (stderr," buf9=%s\n",buf9);
  747.         (void) fprintf (stderr," sh=%d\n",sh);
  748.         (void) fprintf (stderr," buf10=%s\n",buf10);
  749.         (void) fprintf (stderr," df=%s\n",digit_info->datafile);
  750.         (void) fprintf (stderr," parsed QS--\n");
  751.  
  752.     }
  753.  
  754.     if (rc == 25)
  755.         rc=0;
  756.  
  757. ExitProcessing:
  758.     return(rc);
  759. }
  760.  
  761. /*
  762. ** get current time
  763. */
  764.  
  765. char *GetTime ()
  766. {
  767.     time_t
  768.         tm;
  769.  
  770.     char
  771.         *times;
  772.  
  773.     tm = time (NULL);
  774.     times = ctime(&tm);
  775.     times[(int) strlen(times)-1] = '\0';
  776.     return (times);
  777. }
  778.  
  779. void Warning (message)
  780. char
  781.     *message;
  782. {
  783.     char
  784.         *times;
  785.     FILE
  786.         *fp= (FILE *) NULL;
  787.     char
  788.         buf[1024];
  789.  
  790.     *buf='\0';
  791.     (void) sprintf(buf,"%s/%s",LogDir,LogFile);
  792.     times = GetTime();
  793.     fp = fopen(buf, "a");
  794.  
  795.     if (fp == (FILE *) NULL)
  796.     {
  797.         (void) fprintf (stderr,"[%s] Count %s: Could not open CountLog file %s/%s\n ",times, Version, LogDir,LogFile);
  798.         fp = stderr;
  799.     }
  800.         (void) fprintf (fp,"[%s] Count %s: %s\n", 
  801.             times, Version,message);
  802.     if (fp != stderr)
  803.         (void) fclose (fp);
  804. }
  805.  
  806. /*
  807.  *  GetRemoteReferer - returns the remote referer
  808.  *
  809.  *  RCS:
  810.  *      $Revision$
  811.  *      $Date$
  812.  *
  813.  *  Security:
  814.  *      Unclassified
  815.  *
  816.  *  Description:
  817.  *      text
  818.  *
  819.  *  Input Parameters:
  820.  *      type    identifier  description
  821.  *
  822.  *      char    *host       remote referer HTML
  823.  *
  824.  *
  825.  *  Output Parameters:
  826.  *      type    identifier  description
  827.  *
  828.  *      char    *rem_host   retuns the host
  829.  *
  830.  *  Return Values:
  831.  *      value   description
  832.  *
  833.  *  Side Effects:
  834.  *      text
  835.  *
  836.  *  Limitations and Comments:
  837.  *      text
  838.  *
  839.  *  Development History:
  840.  *      who                 when        why
  841.  *      muquit@semcor.com   12-Aug-95   first cut
  842.  */
  843.  
  844. #include <stdio.h>
  845. #include <string.h>
  846.  
  847. void GetRemoteReferer (host, rem_host)
  848. char
  849.     *host;
  850. char
  851.     *rem_host;
  852. {
  853.     register char
  854.         *p,
  855.         *q;
  856.  
  857.     int
  858.         x;
  859.  
  860.     *rem_host = '\0';
  861.     q=rem_host;
  862.  
  863.     for (p=host; *p != '\0'; p++)
  864.     {
  865.         if (*p == '/')
  866.         {
  867.             p += 2;
  868.             break;
  869.         }
  870.     }
  871.     while ((*p != '/') && (*p != '\0'))
  872.         *q++ = *p++;
  873.  
  874.     *q = '\0';
  875.  
  876.     /*
  877.     ** randerso@bite.db.uth.tmc.edu added the following lines of code
  878.     ** to account for port numbers at the end of a url
  879.     */
  880.  
  881.     x=0;
  882.     while ((x < (int) strlen(rem_host)) && (rem_host[x] != ':'))
  883.         x++;
  884.     rem_host[x]='\0';
  885. }
  886.  
  887. #ifdef USE_LOCK
  888. #include <sys/file.h>
  889. void SetLock (fd)
  890. int
  891.     fd;
  892. {
  893. #ifdef SYSV
  894.     lseek(fd,0L,0);
  895.     (void) lockf(fd,F_LOCK,0L);
  896. #else
  897.     (void) flock(fd,LOCK_EX);
  898. #endif
  899. }
  900.  
  901. void UnsetLock (fd)
  902. int
  903.     fd;
  904. {
  905. #ifdef SYSV
  906.     lseek(fd,0L,0);
  907.     (void) lockf(fd,F_ULOCK,0L);
  908. #else
  909.     (void) flock(fd,LOCK_UN);
  910. #endif
  911. }
  912. #endif /*USE_LOCK*/
  913.  
  914. int CheckDirs()
  915. {
  916.     if ((strcmp(ConfigDir,DigitDir) == 0) ||
  917.         (strcmp(ConfigDir,DataDir) == 0) ||
  918.         (strcmp(ConfigDir,LogDir) == 0) ||
  919.         (strcmp(ConfigDir,LogDir) == 0) ||
  920.         (strcmp(DigitDir,DataDir) == 0) ||
  921.         (strcmp(DigitDir,LogDir) == 0) ||
  922.         (strcmp(DataDir,LogDir) == 0))
  923.     return (1);
  924. return (0);
  925. }
  926.  
  927.  
  928. /*
  929.  *  CheckRemoteIP - checks if remote host in the ignore list
  930.  *
  931.  *  RCS:
  932.  *      $Revision$
  933.  *      $Date$
  934.  *
  935.  *  Security:
  936.  *      Unclassified
  937.  *
  938.  *  Description:
  939.  *      text
  940.  *
  941.  *  Input Parameters:
  942.  *      type    identifier  description
  943.  *
  944.  *      text
  945.  *
  946.  *  Output Parameters:
  947.  *      type    identifier  description
  948.  *
  949.  *      text
  950.  *
  951.  *  Return Values:
  952.  *      value   description
  953.  *
  954.  *      True if the remote IP should be ignored (a match)
  955.  *      False remote IP should be counted
  956.  *
  957.  *  Side Effects:
  958.  *      text
  959.  *
  960.  *  Limitations and Comments:
  961.  *      text
  962.  *
  963.  *  Development History:
  964.  *      who                 when        why
  965.  *      muquit@semcor.com   13-Sep-95   -
  966.  */
  967.  
  968. unsigned int CheckRemoteIP(remote_ip,ip_ign)
  969. char
  970.     *remote_ip,
  971.     *ip_ign;
  972. {
  973.     char
  974.         tmp[10],
  975.         buf[100],
  976.         buf2[100];
  977.  
  978.     int
  979.         x,
  980.         y,
  981.         z,
  982.         w,
  983.         xx,
  984.         yy,
  985.         zz,
  986.         ww;
  987.  
  988.     int
  989.         rc;
  990.  
  991.     *tmp='\0';
  992.     *buf='\0';
  993.     *buf2='\0';
  994.  
  995.     /*
  996.     ** REMOTE_ADDR
  997.     */
  998.  
  999.     (void) strcpy(buf,remote_ip);
  1000.     rc=sscanf(buf,"%d.%d.%d.%d",&xx,&yy,&zz,&ww);
  1001.     if (rc != 4)
  1002.         return(False);
  1003.  
  1004.     /*
  1005.     ** IP from conf file, we'll compare the remote IP with this one.
  1006.     ** we'll check for wildcard in the IP from conf file as well
  1007.     ** we'll check for all 3 classes of network
  1008.     */
  1009.  
  1010.     rc=sscanf(ip_ign,"%d.%d.%d.%d",&x,&y,&z,&w);
  1011.     if (rc != 4) /* possible wildcard */
  1012.     {
  1013.         /*
  1014.         ** check wildcard for a Class C network
  1015.         */
  1016.         if ((x >= 192) && (x <= 223))
  1017.         {
  1018.             /*
  1019.             ** we'r concerned with 4th octet
  1020.             */
  1021.             rc=sscanf(ip_ign,"%d.%d.%d.%s",&x,&y,&z,tmp);
  1022.             if (rc != 4) /* screwed up entry, don't ignore*/
  1023.                 return (False);
  1024.             if (strcmp(tmp,"*") == 0)
  1025.             {
  1026.                 if ((x == xx) &&
  1027.                     (y == yy) &&
  1028.                     (z == zz))
  1029.                 {
  1030.                     return (True);
  1031.                 }
  1032.             }
  1033.             else
  1034.                 return (False);
  1035.         }
  1036.         else if ((x >= 128) && (x <= 191))
  1037.         {
  1038.             /*
  1039.             ** check wildcard for class B network
  1040.             ** we'll check the 3rd octet for wildcard
  1041.             */
  1042.             (void) fprintf (stderr," Class B\n");
  1043.             rc=sscanf(ip_ign,"%d.%d.%s",&x,&y,tmp);
  1044.             (void) fprintf (stderr," rc: %d\n",rc);
  1045.             (void) fprintf (stderr," tmp:%s\n",tmp);
  1046.             if (rc != 3)
  1047.                 return (False);
  1048.  
  1049.             if ((strcmp(tmp,"*") == 0) ||
  1050.                 (strcmp(tmp,"*.*") == 0))
  1051.             {
  1052.                 (void) fprintf (stderr," xx,yy:%d,%d\n",xx,yy);
  1053.                 if ((x == xx) &&
  1054.                     (y == yy))
  1055.                 {
  1056.                     return (True);
  1057.                 }
  1058.                 else
  1059.                     return (False);
  1060.             }
  1061.         }
  1062.         else /* x < 128, got to be a Class A network */
  1063.         {
  1064.             /*
  1065.             ** we'll check the 2nd octet for wildcard
  1066.             */
  1067.             rc=sscanf(ip_ign,"%d.%s",&x,tmp);
  1068.             if (rc != 2)
  1069.                 return (False);
  1070.  
  1071.             if ((strcmp(tmp,"*") == 0) ||
  1072.                 (strcmp(tmp,"*.*") == 0) ||
  1073.                 (strcmp(tmp,"*.*.*") == 0))
  1074.             {
  1075.                 if (x == xx)
  1076.                     return (True);
  1077.                  else
  1078.                     return (False);
  1079.             }
  1080.         }
  1081.     }
  1082.     else
  1083.     {
  1084.         /*
  1085.         ** compare directly
  1086.         */
  1087.         if (strcmp(buf,ip_ign) == 0)
  1088.             return (True);
  1089.     }
  1090.  
  1091. return (False);
  1092. }
  1093.