kalasag.org

kalasag.org

Git

This blob has been accessed 2 times via Git panel.

  1. #include "kalasag.h"
  2. #include "kalasag_io.h"
  3. #include "kalasag_util.h"
  4.  
  5. /* Stored PID file path for cleanup on exit */
  6. static char gblPidFile[MAXBUF] = "";
  7.  
  8. /* Main logging function to surrogate syslog */
  9. void Log(char *logentry, ...)
  10. {
  11.     char logbuffer[MAXBUF];
  12.  
  13.     va_list argsPtr;
  14.     va_start(argsPtr, logentry);
  15.  
  16.     vsnprintf(logbuffer, MAXBUF, logentry, argsPtr);
  17.  
  18.     va_end(argsPtr);
  19.  
  20.     openlog("kalasag", LOG_PID, SYSLOG_FACILITY);
  21.     syslog(SYSLOG_LEVEL, "%s", logbuffer);
  22.     closelog();
  23. }
  24.  
  25.  
  26. void Exit(int status)
  27. {
  28.     if (gblPidFile[0])
  29.         unlink(gblPidFile);
  30.     Log("securityalert: Kalasag is shutting down\n");
  31.     Log("adminalert: Kalasag is shutting down\n");
  32.     exit(status);
  33. }
  34.  
  35.  
  36. void Start(void)
  37. {
  38.     Log("adminalert: Kalasag %s is starting.\n", VERSION);
  39. #ifdef DEBUG
  40.     printf("Compiled: " __DATE__ " at " __TIME__ "\n");
  41. #endif
  42. }
  43.  
  44.  
  45.  
  46. /* The daemonizing code copied from Advanced Programming */
  47. /* in the UNIX Environment by W. Richard Stevens with minor changes */
  48. int DaemonSeed(char *detectionType)
  49. {
  50.     int childpid;
  51.  
  52.     signal(SIGALRM, SIG_IGN);
  53.     signal(SIGHUP, SIG_IGN);
  54.     signal(SIGPIPE, SIG_IGN);
  55.     signal(SIGTERM, Exit);
  56.     signal(SIGABRT, Exit);
  57.     signal(SIGURG, Exit);
  58.     signal(SIGKILL, Exit);
  59.  
  60.     if ((childpid = fork()) < 0)
  61.         return (ERROR);
  62.     else if (childpid > 0)
  63.         exit(0);
  64.  
  65.     setsid();
  66.     { int unused __attribute__((unused)) = chdir("/"); }
  67.     umask(077);
  68.  
  69.     /* Write PID file for systemd tracking */
  70.     snprintf(gblPidFile, MAXBUF, "/var/run/kalasag-%s.pid", detectionType);
  71.     FILE *pidFile = fopen(gblPidFile, "w");
  72.     if (pidFile) {
  73.         fprintf(pidFile, "%d\n", getpid());
  74.         fclose(pidFile);
  75.     }
  76.  
  77.     /* close stdout, stdin, stderr */
  78.     close(0);
  79.     close(1);
  80.     close(2);
  81.  
  82.     return (TRUE);
  83. }
  84.  
  85.  
  86. /* Compares an IP address against a listed address and its netmask*/
  87. int CompareIPs(char *target, char *ignoreAddr, int ignoreNetmaskBits)
  88. {
  89.     unsigned long int netmaskAddr, ipAddr, targetAddr;
  90.  
  91.     ipAddr = inet_addr(ignoreAddr);
  92.     targetAddr = inet_addr(target);
  93.     netmaskAddr = htonl(0xFFFFFFFF << (32 - ignoreNetmaskBits));
  94.  
  95.  
  96. #ifdef DEBUG
  97.     Log("debug: target %s\n", target);
  98.     Log("debug: ignoreAddr %s\n", ignoreAddr);
  99.     Log("debug: ignoreNetmaskBits %d\n", ignoreNetmaskBits);
  100.     Log("debug: ipAddr %lu\n", ipAddr);
  101.     Log("debug: targetAddr %lu\n", targetAddr);
  102.     Log("debug: netmask %x\n", netmaskAddr);
  103.     Log("debug: mix ipAddr %lu\n", (ipAddr & netmaskAddr));
  104.     Log("debug: mix target %lu\n", (targetAddr & netmaskAddr));
  105. #endif
  106.  
  107.     /* Network portion mask & op and return */
  108.     if ((ipAddr & netmaskAddr) == (targetAddr & netmaskAddr))
  109.         return (TRUE);
  110.     else
  111.         return (FALSE);
  112. }
  113.  
  114.  
  115.  
  116. /* check hosts that should never be blocked */
  117. int NeverBlock(char *target, char *filename)
  118. {
  119.     FILE *input;
  120.     char buffer[MAXBUF], tempBuffer[MAXBUF], netmaskBuffer[MAXBUF];
  121.     char *slashPos;
  122.     size_t count = 0;
  123.     int dest = 0, netmaskBits = 0;
  124.  
  125. #ifdef DEBUG
  126.     Log("debug: NeverBlock: Opening ignore file: %s \n", filename);
  127. #endif
  128.     if ((input = fopen(filename, "r")) == NULL)
  129.         return (ERROR);
  130.  
  131. #ifdef DEBUG
  132.     Log("debug: NeverBlock: Doing lookup for host: %s \n", target);
  133. #endif
  134.  
  135.     while (fgets(buffer, MAXBUF, input) != NULL) {
  136.         /* Reset destination counter */
  137.         dest = 0;
  138.  
  139.         if ((buffer[0] == '#') || (buffer[0] == '\n'))
  140.             continue;
  141.  
  142.         for (count = 0; count < strlen(buffer); count++) {
  143.             /* Parse out digits, colons, and slashes. Everything else rejected */
  144.             if ((isdigit(buffer[count])) ||
  145.                 (buffer[count] == '.') || (buffer[count] == ':')
  146.                 || (buffer[count] == '/')) {
  147.                 tempBuffer[dest++] = buffer[count];
  148.             } else {
  149.                 tempBuffer[dest] = '\0';
  150.                 break;
  151.             }
  152.         }
  153.  
  154.         /* Return pointer to slash if it exists and copy data to buffer */
  155.         slashPos = strchr(tempBuffer, '/');
  156.         if (slashPos) {
  157.             SafeStrncpy(netmaskBuffer, slashPos + 1, MAXBUF);
  158.             /* Terminate tempBuffer string at delimeter for later use */
  159.             *slashPos = '\0';
  160.         } else
  161.             /* Copy in a 32 bit netmask if none given */
  162.             SafeStrncpy(netmaskBuffer, "32", MAXBUF);
  163.  
  164.  
  165.         /* Convert netmaskBuffer to bits in netmask */
  166.         netmaskBits = atoi(netmaskBuffer);
  167.         if ((netmaskBits < 0) || (netmaskBits > 32)) {
  168.             Log("adminalert: Invalid netmask in config file: %s  Ignoring entry.\n", buffer);
  169.             continue;
  170.         }
  171.  
  172.         if (CompareIPs(target, tempBuffer, netmaskBits)) {
  173. #ifdef DEBUG
  174.             Log("debug: NeverBlock: Host: %s found in ignore file with netmask %s\n", target, netmaskBuffer);
  175. #endif
  176.  
  177.             fclose(input);
  178.             return (TRUE);
  179.         }
  180.  
  181.     }                           /* end while() */
  182.  
  183. #ifdef DEBUG
  184.     Log("debug: NeverBlock: Host: %s NOT found in ignore file\n", target);
  185. #endif
  186.  
  187.     fclose(input);
  188.     return (FALSE);
  189. }
  190.  
  191.  
  192. /* Make sure the config file is available */
  193. int CheckConfig(void)
  194. {
  195.     FILE *input;
  196.  
  197.     if ((input = fopen(CONFIG_FILE, "r")) == NULL) {
  198.         Log("adminalert: Cannot open config file: %s. Exiting\n",
  199.             CONFIG_FILE);
  200.         return (FALSE);
  201.     } else
  202.         fclose(input);
  203.  
  204.     return (TRUE);
  205. }
  206.  
  207.  
  208. /* This writes out blocked hosts to the blocked file. It adds the hostname */
  209. /* time stamp, and port connection that was acted on */
  210. int
  211. WriteBlocked(char *target, char *resolvedHost, int port,
  212.              char *blockedFilename, char *historyFilename, char *portType)
  213. {
  214.     FILE *output;
  215.     int blockedStatus = TRUE, historyStatus = TRUE;
  216.  
  217.     struct tm *tmptr;
  218.  
  219.     time_t current_time;
  220.     current_time = time(0);
  221.     tmptr = localtime(&current_time);
  222.  
  223.  
  224. #ifdef DEBUG
  225.     Log("debug: WriteBlocked: Opening block file: %s \n", blockedFilename);
  226. #endif
  227.  
  228.  
  229.     if ((output = fopen(blockedFilename, "a")) == NULL) {
  230.         Log("adminalert: ERROR: Cannot open blocked file: %s.\n",
  231.             blockedFilename);
  232.         blockedStatus = FALSE;
  233.     } else {
  234.         fprintf(output,
  235.                 "%ld - %02d/%02d/%04d %02d:%02d:%02d Host: %s/%s Port: %d %s Blocked\n",
  236.                 current_time, tmptr->tm_mon + 1, tmptr->tm_mday,
  237.                 tmptr->tm_year + 1900, tmptr->tm_hour, tmptr->tm_min,
  238.                 tmptr->tm_sec, resolvedHost, target, port, portType);
  239.         fclose(output);
  240.         blockedStatus = TRUE;
  241.     }
  242.  
  243. #ifdef DEBUG
  244.     Log("debug: WriteBlocked: Opening history file: %s \n",
  245.         historyFilename);
  246. #endif
  247.     if ((output = fopen(historyFilename, "a")) == NULL) {
  248.         Log("adminalert: ERROR: Cannot open history file: %s.\n",
  249.             historyFilename);
  250.         historyStatus = FALSE;
  251.     } else {
  252.         fprintf(output,
  253.                 "%ld - %02d/%02d/%04d %02d:%02d:%02d Host: %s/%s Port: %d %s Blocked\n",
  254.                 current_time, tmptr->tm_mon + 1, tmptr->tm_mday,
  255.                 tmptr->tm_year + 1900, tmptr->tm_hour, tmptr->tm_min,
  256.                 tmptr->tm_sec, resolvedHost, target, port, portType);
  257.         fclose(output);
  258.         historyStatus = TRUE;
  259.     }
  260.  
  261.     if (historyStatus || blockedStatus == FALSE)
  262.         return (FALSE);
  263.     else
  264.         return (TRUE);
  265. }
  266.  
  267.  
  268.  
  269.  
  270. /* This reads a token from the config file up to the "=" and returns the string */
  271. /* up to the first space or NULL */
  272. int ConfigTokenRetrieve(char *token, char *configToken)
  273. {
  274.     FILE *config;
  275.     char buffer[MAXBUF], tokenBuffer[MAXBUF];
  276.     int count = 0;
  277.  
  278.     if ((config = fopen(CONFIG_FILE, "r")) == NULL) {
  279.         Log("adminalert: ERROR: Cannot open config file: %s.\n",
  280.             CONFIG_FILE);
  281.         return (ERROR);
  282.     } else {
  283. #ifdef DEBUG
  284.         Log("debug: ConfigTokenRetrieve: checking for token %s", token);
  285. #endif
  286.         while ((fgets(buffer, MAXBUF, config)) != NULL) {
  287.             /* this skips comments */
  288.             if (buffer[0] != '#') {
  289. #ifdef DEBUG
  290.                 Log("debug: ConfigTokenRetrieve: data: %s", buffer);
  291. #endif
  292.                 /* search for the token and make sure the trailing character */
  293.                 /* is a " " or "=" to make sure the entire token was found */
  294.                 if ((strstr(buffer, token) != NULL) &&
  295.                     ((buffer[strlen(token)] == '=')
  296.                      || (buffer[strlen(token)] == ' '))) {      /* cut off the '=' and send it back */
  297.                     if (strstr(buffer, "\"") == NULL) {
  298.                         Log("adminalert: Quotes missing from %s token. Option skipped\n", token);
  299.                         fclose(config);
  300.                         return (FALSE);
  301.                     }
  302.  
  303.                     SafeStrncpy(tokenBuffer, strstr(buffer, "\"") + 1,
  304.                                 MAXBUF);
  305.  
  306.                     /* strip off unprintables/linefeeds (if any) */
  307.                     count = 0;
  308.                     while (count < MAXBUF - 1) {
  309.                         if ((isprint(tokenBuffer[count]))
  310.                             && tokenBuffer[count] != '"')
  311.                             configToken[count] = tokenBuffer[count];
  312.                         else {
  313.                             configToken[count] = '\0';
  314.                             break;
  315.                         }
  316.                         count++;
  317.                     }
  318.  
  319. #ifdef DEBUG
  320.                     Log("debug: ConfigTokenRetrieved token: %s\n",
  321.                         configToken);
  322. #endif
  323.                     configToken[MAXBUF - 1] = '\0';
  324.                     fclose(config);
  325.                     return (TRUE);
  326.                 }
  327.             }
  328.         }
  329.         fclose(config);
  330.         return (FALSE);
  331.     }
  332.  
  333. }
  334.  
  335.  
  336.  
  337.  
  338. /* This will bind a socket to a port. It works for UDP/TCP */
  339. int
  340. BindSocket(int sockfd, struct sockaddr_in client,
  341.            struct sockaddr_in server, int port)
  342. {
  343.     (void)client;
  344. #ifdef DEBUG
  345.     Log("debug: BindSocket: Binding to port: %d\n", port);
  346. #endif
  347.  
  348.     bzero((char *) &server, sizeof(server));
  349.     server.sin_family = AF_INET;
  350.     server.sin_addr.s_addr = htonl(INADDR_ANY);
  351.     server.sin_port = htons(port);
  352.  
  353.     if (bind(sockfd, (struct sockaddr *) &server, sizeof(server)) < 0) {
  354. #ifdef DEBUG
  355.         Log("debug: BindSocket: Binding failed\n");
  356. #endif
  357.         return (ERROR);
  358.     } else {
  359. #ifdef DEBUG
  360.         Log("debug: BindSocket: Binding successful. Doing listen\n");
  361. #endif
  362.         listen(sockfd, 5);
  363.         return (TRUE);
  364.     }
  365. }
  366.  
  367.  
  368. /* Open a TCP Socket */
  369. int OpenTCPSocket(void)
  370. {
  371.     int sockfd;
  372.  
  373. #ifdef DEBUG
  374.     Log("debug: OpenTCPSocket: opening TCP socket\n");
  375. #endif
  376.  
  377.     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  378.         return (ERROR);
  379.     else
  380.         return (sockfd);
  381. }
  382.  
  383.  
  384. /* Open a UDP Socket */
  385. int OpenUDPSocket(void)
  386. {
  387.     int sockfd;
  388.  
  389. #ifdef DEBUG
  390.     Log("debug: openUDPSocket opening UDP socket\n");
  391. #endif
  392.  
  393.     if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
  394.         return (ERROR);
  395.     else
  396.         return (sockfd);
  397. }
  398.  
  399. #ifdef SUPPORT_STEALTH
  400. /* Open a RAW TCPSocket */
  401. int OpenRAWTCPSocket(void)
  402. {
  403.     int sockfd;
  404.  
  405. #ifdef DEBUG
  406.     Log("debug: OpenRAWTCPSocket: opening RAW TCP socket\n");
  407. #endif
  408.  
  409.     if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) < 0)
  410.         return (ERROR);
  411.     else
  412.         return (sockfd);
  413. }
  414.  
  415. /* Open a RAW UDP Socket */
  416. int OpenRAWUDPSocket(void)
  417. {
  418.     int sockfd;
  419.  
  420. #ifdef DEBUG
  421.     Log("debug: OpenRAWUDPSocket: opening RAW UDP socket\n");
  422. #endif
  423.  
  424.     if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP)) < 0)
  425.         return (ERROR);
  426.     else
  427.         return (sockfd);
  428. }
  429. #endif
  430.  
  431. /* This will use a system() call to change the route of the target host to */
  432. /* a dead IP address on your LOCAL SUBNET. */
  433. int
  434. KillRoute(char *target, int port, char *killString, char *detectionType)
  435. {
  436.     char cleanAddr[MAXBUF], commandStringTemp[MAXBUF];
  437.     char commandStringTemp2[MAXBUF], commandStringFinal[MAXBUF];
  438.     char portString[MAXBUF];
  439.     int killStatus = ERROR, substStatus = ERROR;
  440.  
  441.     CleanIpAddr(cleanAddr, target);
  442.     snprintf(portString, MAXBUF, "%d", port);
  443.  
  444.     substStatus =
  445.         SubstString(cleanAddr, "$TARGET$", killString, commandStringTemp);
  446.     if (substStatus == 0) {
  447.         Log("adminalert: No target variable specified in KILL_ROUTE option. Skipping.\n");
  448.         return (ERROR);
  449.     } else if (substStatus == ERROR) {
  450.         Log("adminalert: Error trying to parse $TARGET$ Token for KILL_ROUTE. Skipping.\n");
  451.         return (ERROR);
  452.     }
  453.  
  454.     if (SubstString
  455.         (portString, "$PORT$", commandStringTemp,
  456.          commandStringTemp2) == ERROR) {
  457.         Log("adminalert: Error trying to parse $PORT$ Token for KILL_ROUTE. Skipping.\n");
  458.         return (ERROR);
  459.     }
  460.  
  461.     if (SubstString
  462.         (detectionType, "$MODE$", commandStringTemp2,
  463.          commandStringFinal) == ERROR) {
  464.         Log("adminalert: Error trying to parse $MODE$ Token for KILL_ROUTE. Skipping.\n");
  465.         return (ERROR);
  466.     }
  467.  
  468. #ifdef DEBUG
  469.     Log("debug: KillRoute: running route command: %s\n",
  470.         commandStringFinal);
  471. #endif
  472.  
  473.     /* Kill the bastard and report a status */
  474.     killStatus = system(commandStringFinal);
  475.  
  476.     if (killStatus == 127) {
  477.         Log("adminalert: ERROR: There was an error trying to block host (exec fail) %s", target);
  478.         return (ERROR);
  479.     } else if (killStatus < 0) {
  480.         Log("adminalert: ERROR: There was an error trying to block host (system fail) %s", target);
  481.         return (ERROR);
  482.     } else {
  483.         Log("attackalert: Host %s has been blocked via dropped route using command: \"%s\"", target, commandStringFinal);
  484.         return (TRUE);
  485.     }
  486. }
  487.  
  488.  
  489.  
  490. /* This will run a specified command with TARGET as the option if one is given. */
  491. int
  492. KillRunCmd(char *target, int port, char *killString, char *detectionType)
  493. {
  494.     char cleanAddr[MAXBUF], commandStringTemp[MAXBUF];
  495.     char commandStringTemp2[MAXBUF], commandStringFinal[MAXBUF];
  496.     char portString[MAXBUF];
  497.     int killStatus = ERROR;
  498.  
  499.     CleanIpAddr(cleanAddr, target);
  500.     snprintf(portString, MAXBUF, "%d", port);
  501.  
  502.     /* Tokens are not required, but we check for an error anyway */
  503.     if (SubstString(cleanAddr, "$TARGET$", killString, commandStringTemp)
  504.         == ERROR) {
  505.         Log("adminalert: Error trying to parse $TARGET$ Token for KILL_RUN_CMD. Skipping.\n");
  506.         return (ERROR);
  507.     }
  508.  
  509.     if (SubstString
  510.         (portString, "$PORT$", commandStringTemp,
  511.          commandStringTemp2) == ERROR) {
  512.         Log("adminalert: Error trying to parse $PORT$ Token for KILL_RUN_CMD. Skipping.\n");
  513.         return (ERROR);
  514.     }
  515.  
  516.     if (SubstString
  517.         (detectionType, "$MODE$", commandStringTemp2,
  518.          commandStringFinal) == ERROR) {
  519.         Log("adminalert: Error trying to parse $MODE$ Token for KILL_RUN_CMD. Skipping.\n");
  520.         return (ERROR);
  521.     }
  522.  
  523.  
  524.     /* Kill the bastard and report a status */
  525.     killStatus = system(commandStringFinal);
  526.  
  527.     if (killStatus == 127) {
  528.         Log("adminalert: ERROR: There was an error trying to run command (exec fail) %s", target);
  529.         return (ERROR);
  530.     } else if (killStatus < 0) {
  531.         Log("adminalert: ERROR: There was an error trying to run command (system fail) %s", target);
  532.         return (ERROR);
  533.     } else {
  534.         /* report success */
  535.         Log("attackalert: External command run for host: %s using command: \"%s\"", target, commandStringFinal);
  536.         return (TRUE);
  537.     }
  538. }
  539.  
  540.  
  541. /* this function will drop the host into the TCP wrappers hosts.deny file to deny */
  542. /* all access. The drop route method is preferred as this stops UDP attacks as well */
  543. /* as TCP. You may find though that host.deny will be a more permanent home.. */
  544. int
  545. KillHostsDeny(char *target, int port, char *killString,
  546.               char *detectionType)
  547. {
  548.  
  549.     FILE *output;
  550.     char cleanAddr[MAXBUF], commandStringTemp[MAXBUF];
  551.     char commandStringTemp2[MAXBUF], commandStringFinal[MAXBUF];
  552.     char portString[MAXBUF];
  553.     int substStatus = ERROR;
  554.  
  555.     CleanIpAddr(cleanAddr, target);
  556.  
  557.     snprintf(portString, MAXBUF, "%d", port);
  558.  
  559. #ifdef DEBUG
  560.     Log("debug: KillHostsDeny: parsing string for block: %s\n",
  561.         killString);
  562. #endif
  563.  
  564.     substStatus =
  565.         SubstString(cleanAddr, "$TARGET$", killString, commandStringTemp);
  566.     if (substStatus == 0) {
  567.         Log("adminalert: No target variable specified in KILL_HOSTS_DENY option. Skipping.\n");
  568.         return (ERROR);
  569.     } else if (substStatus == ERROR) {
  570.         Log("adminalert: Error trying to parse $TARGET$ Token for KILL_HOSTS_DENY. Skipping.\n");
  571.         return (ERROR);
  572.     }
  573.  
  574.     if (SubstString
  575.         (portString, "$PORT$", commandStringTemp,
  576.          commandStringTemp2) == ERROR) {
  577.         Log("adminalert: Error trying to parse $PORT$ Token for KILL_HOSTS_DENY. Skipping.\n");
  578.         return (ERROR);
  579.     }
  580.  
  581.     if (SubstString
  582.         (detectionType, "$MODE$", commandStringTemp2,
  583.          commandStringFinal) == ERROR) {
  584.         Log("adminalert: Error trying to parse $MODE$ Token for KILL_HOSTS_DENY. Skipping.\n");
  585.         return (ERROR);
  586.     }
  587. #ifdef DEBUG
  588.     Log("debug: KillHostsDeny: result string for block: %s\n",
  589.         commandStringFinal);
  590. #endif
  591.  
  592.     if ((output = fopen(WRAPPER_HOSTS_DENY, "a")) == NULL) {
  593.         Log("adminalert: cannot open hosts.deny file: %s for blocking.",
  594.             WRAPPER_HOSTS_DENY);
  595.         Log("securityalert: ERROR: There was an error trying to block host %s", target);
  596.         return (FALSE);
  597.     } else {
  598.         fprintf(output, "%s\n", commandStringFinal);
  599.         fclose(output);
  600.         Log("attackalert: Host %s has been blocked via wrappers with string: \"%s\"", target, commandStringFinal);
  601.         return (TRUE);
  602.     }
  603. }
  604.  
  605.  
  606. /* check if the host is already blocked */
  607. int IsBlocked(char *target, char *filename)
  608. {
  609.     FILE *input;
  610.     char buffer[MAXBUF], tempBuffer[MAXBUF];
  611.     char *ipOffset;
  612.     size_t count;
  613.  
  614.  
  615. #ifdef DEBUG
  616.     Log("debug: IsBlocked: Opening block file: %s \n", filename);
  617. #endif
  618.     if ((input = fopen(filename, "r")) == NULL) {
  619.         Log("adminalert: ERROR: Cannot open blocked file: %s for reading. Will create.\n", filename);
  620.         return (FALSE);
  621.     }
  622.  
  623.     while (fgets(buffer, MAXBUF, input) != NULL) {
  624.         if ((ipOffset = strstr(buffer, target)) != NULL) {
  625.             for (count = 0; count < strlen(ipOffset); count++) {
  626.                 if ((isdigit(ipOffset[count])) || (ipOffset[count] == '.')) {
  627.                     tempBuffer[count] = ipOffset[count];
  628.                 } else {
  629.                     tempBuffer[count] = '\0';
  630.                     break;
  631.                 }
  632.             }
  633.             if (strcmp(target, tempBuffer) == 0) {
  634. #ifdef DEBUG
  635.                 Log("debug: isBlocked: Host: %s found in blocked  file\n",
  636.                     target);
  637. #endif
  638.                 fclose(input);
  639.                 return (TRUE);
  640.             }
  641.         }
  642.  
  643.     }
  644. #ifdef DEBUG
  645.     Log("debug: IsBlocked: Host: %s NOT found in blocked file\n", target);
  646. #endif
  647.     fclose(input);
  648.     return (FALSE);
  649. }
  650.  
  651. /*********************************************************************************
  652. * String substitute function
  653. *
  654. * This function takes:
  655. *
  656. * 1) A token to use for replacement.
  657. * 2) A token to find.
  658. * 3) A string with the tokens in it.
  659. * 4) A string to write the replaced result.
  660. *
  661. * It returns the number of substitutions made during the operation.
  662. **********************************************************************************/
  663. int
  664. SubstString(const char *replace, const char *find, const char *target,
  665.             char *result)
  666. {
  667.     size_t replaceCount = 0;
  668.     int count = 0, findCount = 0, findLen =
  669.         0, numberOfSubst = 0;
  670.     char tempString[MAXBUF], *tempStringPtr;
  671.  
  672. #ifdef DEBUG
  673.     Log("debug: SubstString: Processing string: %s %d", target,
  674.         strlen(target));
  675.     Log("debug: SubstString: Processing search text: %s %d", replace,
  676.         strlen(replace));
  677.     Log("debug: SubstString: Processing replace text: %s %d", find,
  678.         strlen(find));
  679. #endif
  680.  
  681.     /* string not found in target */
  682.     if (strstr(target, find) == NULL) {
  683.         strncpy(result, target, MAXBUF);
  684. #ifdef DEBUG
  685.         Log("debug: SubstString: Result string: %s", result);
  686. #endif
  687.         return (numberOfSubst);
  688.     }
  689.     /* String/victim/target too long */
  690.     else if ((strlen(target)) + (strlen(replace)) + (strlen(find)) >
  691.              MAXBUF)
  692.         return (ERROR);
  693.  
  694.     memset(tempString, '\0', MAXBUF);
  695.     memset(result, '\0', MAXBUF);
  696.     findLen = strlen(find);
  697.     tempStringPtr = tempString;
  698.  
  699.     for (count = 0; count < MAXBUF; count++) {
  700.         if (*target == '\0')
  701.             break;
  702.         else if ((strncmp(target, find, findLen)) != 0)
  703.             *tempStringPtr++ = *target++;
  704.         else {
  705.             numberOfSubst++;
  706.             for (replaceCount = 0; replaceCount < strlen(replace);
  707.                  replaceCount++)
  708.                 *tempStringPtr++ = replace[replaceCount];
  709.             for (findCount = 0; findCount < findLen; findCount++)
  710.                 target++;
  711.         }
  712.     }
  713.  
  714.     strncpy(result, tempString, MAXBUF);
  715. #ifdef DEBUG
  716.     Log("debug: SubstString: Result string: %s", result);
  717. #endif
  718.     return (numberOfSubst);
  719. }
  720.  
  721.  
  722.  
  723. /* This function checks a config variable for a numerical flag and returns it */
  724. int CheckFlag(char *flagName)
  725. {
  726.     char configToken[MAXBUF];
  727.  
  728.     if ((ConfigTokenRetrieve(flagName, configToken)) == TRUE) {
  729. #ifdef DEBUG
  730.         Log("debug: CheckFlag: found %s string.\n", flagName);
  731. #endif
  732.         return (atoi(configToken));
  733.     } else {
  734. #ifdef DEBUG
  735.         Log("debug: CheckFlag: %s option not found. Assuming FALSE.\n",
  736.             flagName);
  737. #endif
  738.         return (FALSE);
  739.     }
  740. }
  741.  
  742.  
  743.  
  744.  
filedropkalasag.git-8eed43a.tar.bz2 new
21.31 KB
2 downloads
filedropkalasag.git-8eed43a.zip
26.47 KB
1 download
filedropkalasag.git-83beb57.tar.bz2
21.32 KB
3 downloads
filedropkalasag.git-83beb57.zip
26.49 KB
1 download
filedropkalasag.git-89d9746.tar.bz2
21.25 KB
3 downloads
filedropkalasag.git-89d9746.zip
26.41 KB
2 downloads
filedropkalasag.git-a1e8b2b.tar.bz2
21.14 KB
2 downloads
filedropkalasag.git-a1e8b2b.zip
26.30 KB
2 downloads
filedropkalasag.git-8c3a288.tar.bz2
21.20 KB
3 downloads
filedropkalasag.git-8c3a288.zip
26.46 KB
2 downloads
filedropkalasag.git-1c9f013.tar.bz2
20.95 KB
131 downloads
filedropkalasag.git-1c9f013.zip
25.81 KB
76 downloads
filedropkalasag.git-3ca3612.tar.bz2
20.80 KB
34 downloads
filedropkalasag.git-3ca3612.zip
25.66 KB
19 downloads
filedropkalasag.git-2ffeaa6.tar.bz2
20.80 KB
36 downloads
filedropkalasag.git-2ffeaa6.zip
25.65 KB
90 downloads
filedropkalasag.git-2834a11.tar.bz2
20.84 KB
139 downloads
filedropkalasag.git-2834a11.zip
25.72 KB
18 downloads
filedropkalasag.git-afd7b31.tar.bz2
20.84 KB
119 downloads
filedropkalasag.git-afd7b31.zip
25.71 KB
98 downloads
filedropkalasag.git-97c89e1.tar.bz2
20.82 KB
116 downloads
filedropkalasag.git-97c89e1.zip
25.68 KB
96 downloads
filedropkalasag.git-1141d13.tar.bz2
20.65 KB
122 downloads
filedropkalasag.git-1141d13.zip
25.37 KB
82 downloads
filedropkalasag.git-ee3c17b.tar.bz2
20.65 KB
113 downloads
filedropkalasag.git-ee3c17b.zip
25.34 KB
24 downloads
filedropkalasag.git-4032c54.tar.bz2
20.63 KB
32 downloads
filedropkalasag.git-4032c54.zip
25.13 KB
87 downloads
filedropkalasag.git-e51a2a6.tar.bz2
20.65 KB
108 downloads
filedropkalasag.git-e51a2a6.zip
25.13 KB
19 downloads
filedropkalasag.git-599c93a.tar.bz2
20.63 KB
104 downloads
filedropkalasag.git-599c93a.zip
25.11 KB
1,612 downloads
filedropkalasag.git-acdc640.tar.bz2
20.63 KB
105 downloads
filedropkalasag.git-acdc640.zip
25.10 KB
14 downloads