kalasag.org

kalasag.org

Git

This blob has been accessed 2,619 times via Git panel.

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