services.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* $Id: services.c 1897 2007-08-25 04:57:38Z aturner $ */
  2. /*
  3. * Copyright (c) 2004 Aaron Turner.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "config.h"
  32. #include "defines.h"
  33. #include "common.h"
  34. #include <sys/types.h>
  35. #include <regex.h>
  36. #include <errno.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39. /**
  40. * parses /etc/services so we know which ports are service ports
  41. */
  42. void
  43. parse_services(const char *file, tcpr_services_t *services)
  44. {
  45. FILE *service = NULL;
  46. char service_line[MAXLINE], port[10], proto[10];
  47. regex_t preg;
  48. u_int16_t portc;
  49. size_t nmatch = 3;
  50. regmatch_t pmatch[3];
  51. char regex[] = "([0-9]+)/(tcp|udp)"; /* matches the port as pmatch[1], service pmatch[2] */
  52. dbgx(1, "Parsing %s", file);
  53. memset(service_line, '\0', MAXLINE);
  54. /* mark all ports not a service */
  55. memset(services->tcp, '\0', NUM_PORTS);
  56. memset(services->udp, '\0', NUM_PORTS);
  57. if ((service = fopen(file, "r")) == NULL) {
  58. errx(1, "Unable to open service file: %s\n%s", file, strerror(errno));
  59. }
  60. /* compile our regexes */
  61. if ((regcomp(&preg, regex, REG_ICASE|REG_EXTENDED)) != 0) {
  62. errx(1, "Unable to compile regex: %s", regex);
  63. }
  64. /* parse the entire file */
  65. while ((fgets(service_line, MAXLINE, service)) != NULL) {
  66. /* zero out our vars */
  67. memset(port, '\0', 10);
  68. memset(proto, '\0', 10);
  69. portc = 0;
  70. dbgx(4, "Procesing: %s", service_line);
  71. /* look for format of 1234/tcp */
  72. if ((regexec(&preg, service_line, nmatch, pmatch, 0)) == 0) { /* matches */
  73. if (nmatch < 2) {
  74. err(1, "WTF? I matched the line, but I don't know where!");
  75. }
  76. /* strip out the port & proto from the line */
  77. strncpy(port, &service_line[pmatch[1].rm_so], (pmatch[1].rm_eo - pmatch[1].rm_so));
  78. strncpy(proto, &service_line[pmatch[2].rm_so], (pmatch[2].rm_eo - pmatch[2].rm_so));
  79. /* convert port[] into an integer */
  80. portc = (u_int16_t)atoi(port);
  81. /* update appropriate service array with the server port */
  82. if (strcmp(proto, "tcp") == 0) {
  83. dbgx(3, "Setting TCP/%d as a server port", portc);
  84. services->tcp[portc] = 1; /* mark it as a service port */
  85. } else if (strcmp(proto, "udp") == 0) {
  86. dbgx(3, "Setting UDP/%d as a server port", portc);
  87. services->udp[portc] = 1;
  88. } else {
  89. warnx("Skipping unknown protocol service %s/%d", proto, portc);
  90. }
  91. }
  92. }
  93. }
  94. /*
  95. Local Variables:
  96. mode:c
  97. indent-tabs-mode:nil
  98. c-basic-offset:4
  99. End:
  100. */