services.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* $Id: services.c 1212 2005-03-18 19:24: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. /*
  37. * parses /etc/services so we know which ports are service ports
  38. */
  39. void
  40. parse_services(const char *file, services_t *services)
  41. {
  42. FILE *service = NULL;
  43. char service_line[MAXLINE], port[10], proto[10];
  44. regex_t preg;
  45. u_int16_t portc;
  46. size_t nmatch = 3;
  47. regmatch_t pmatch[3];
  48. char regex[] = "([0-9]+)/(tcp|udp)"; /* matches the port as pmatch[1], service pmatch[2] */
  49. dbg(1, "Parsing %s", file);
  50. memset(service_line, '\0', MAXLINE);
  51. /* mark all ports not a service */
  52. memset(services->tcp, '\0', NUM_PORTS);
  53. memset(services->udp, '\0', NUM_PORTS);
  54. if ((service = fopen(file, "r")) == NULL) {
  55. errx(1, "Unable to open service file: %s\n%s", file, strerror(errno));
  56. }
  57. /* compile our regexes */
  58. if ((regcomp(&preg, regex, REG_ICASE|REG_EXTENDED)) != 0) {
  59. errx(1, "Unable to compile regex: %s", regex);
  60. }
  61. /* parse the entire file */
  62. while ((fgets(service_line, MAXLINE, service)) != NULL) {
  63. /* zero out our vars */
  64. memset(port, '\0', 10);
  65. memset(proto, '\0', 10);
  66. portc = 0;
  67. dbg(4, "Procesing: %s", service_line);
  68. /* look for format of 1234/tcp */
  69. if ((regexec(&preg, service_line, nmatch, pmatch, 0)) == 0) { /* matches */
  70. if (nmatch < 2) {
  71. err(1, "WTF? I matched the line, but I don't know where!");
  72. }
  73. /* strip out the port & proto from the line */
  74. strncpy(port, &service_line[pmatch[1].rm_so], (pmatch[1].rm_eo - pmatch[1].rm_so));
  75. strncpy(proto, &service_line[pmatch[2].rm_so], (pmatch[2].rm_eo - pmatch[2].rm_so));
  76. /* convert port[] into an integer */
  77. portc = (u_int16_t)atoi(port);
  78. /* update appropriate service array with the server port */
  79. if (strcmp(proto, "tcp") == 0) {
  80. dbg(3, "Setting TCP/%d as a server port", portc);
  81. services->tcp[portc] = 1; /* mark it as a service port */
  82. } else if (strcmp(proto, "udp") == 0) {
  83. dbg(3, "Setting UDP/%d as a server port", portc);
  84. services->udp[portc] = 1;
  85. } else {
  86. warnx("Skipping unknown protocol service %s/%d", proto, portc);
  87. }
  88. }
  89. }
  90. }
  91. /*
  92. Local Variables:
  93. mode:c
  94. indent-tabs-mode:nil
  95. c-basic-offset:4
  96. End:
  97. */