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