home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / nmap254b.zip / portlist.c < prev    next >
C/C++ Source or Header  |  2001-07-30  |  12KB  |  318 lines

  1.  
  2. /***********************************************************************/
  3. /* portlist.c -- Functions for manipulating various lists of ports     */
  4. /* maintained internally by Nmap.                                      */
  5. /*                                                                     */
  6. /***********************************************************************/
  7. /*  The Nmap Security Scanner is (C) 1995-2001 Insecure.Com LLC. This  */
  8. /*  program is free software; you can redistribute it and/or modify    */
  9. /*  it under the terms of the GNU General Public License as published  */
  10. /*  by the Free Software Foundation; Version 2.  This guarantees your  */
  11. /*  right to use, modify, and redistribute this software under certain */
  12. /*  conditions.  If this license is unacceptable to you, we may be     */
  13. /*  willing to sell alternative licenses (contact sales@insecure.com). */
  14. /*                                                                     */
  15. /*  If you received these files with a written license agreement       */
  16. /*  stating terms other than the (GPL) terms above, then that          */
  17. /*  alternative license agreement takes precendence over this comment. */
  18. /*                                                                     */
  19. /*  Source is provided to this software because we believe users have  */
  20. /*  a right to know exactly what a program is going to do before they  */
  21. /*  run it.  This also allows you to audit the software for security   */
  22. /*  holes (none have been found so far).                               */
  23. /*                                                                     */
  24. /*  Source code also allows you to port Nmap to new platforms, fix     */
  25. /*  bugs, and add new features.  You are highly encouraged to send     */
  26. /*  your changes to fyodor@insecure.org for possible incorporation     */
  27. /*  into the main distribution.  By sending these changes to Fyodor or */
  28. /*  one the insecure.org development mailing lists, it is assumed that */
  29. /*  you are offering Fyodor the unlimited, non-exclusive right to      */
  30. /*  reuse, modify, and relicense the code.  This is important because  */
  31. /*  the inability to relicense code has caused devastating problems    */
  32. /*  for other Free Software projects (such as KDE and NASM).  Nmap     */
  33. /*  will always be available Open Source.  If you wish to specify      */
  34. /*  special license conditions of your contributions, just say so      */
  35. /*  when you send them.                                                */
  36. /*                                                                     */
  37. /*  This program is distributed in the hope that it will be useful,    */
  38. /*  but WITHOUT ANY WARRANTY; without even the implied warranty of     */
  39. /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  */
  40. /*  General Public License for more details (                          */
  41. /*  http://www.gnu.org/copyleft/gpl.html ).                            */
  42. /*                                                                     */
  43. /***********************************************************************/
  44.  
  45. /* $Id: portlist.c,v 1.15 2001/07/30 06:08:00 fyodor Exp $ */
  46.  
  47.  
  48. #include "portlist.h"
  49. #include "nmap_error.h"
  50. #include "nmap.h"
  51.  
  52. #include <strings.h>
  53.  
  54. extern struct ops o;  /* option structure */
  55. static struct port *freeportlist = NULL;
  56.  
  57. /* gawd, my next project will be in c++ so I don't have to deal with
  58.    this crap ... simple linked list implementation */
  59. int addport(portlist *plist, u16 portno, u8 protocol, char *owner, int state) {
  60.   struct port *current = NULL;
  61.   struct port **portarray = NULL;
  62.   char msg[128];
  63.  
  64.   if ((state == PORT_OPEN && o.verbose) || (o.debugging > 1)) {
  65.     if (owner && *owner) {
  66.       snprintf(msg, sizeof(msg), " (owner: %s)", owner);
  67.     } else msg[0] = '\0';
  68.  
  69.     log_write(LOG_STDOUT, "Adding %s port %hu/%s%s\n",
  70.           statenum2str(state), portno, 
  71.           (protocol == IPPROTO_TCP)? "tcp" : "udp", msg);
  72.     log_flush(LOG_STDOUT);
  73.   }
  74.  
  75. /* Make sure state is OK */
  76.   if (state != PORT_OPEN && state != PORT_CLOSED && state != PORT_FIREWALLED &&
  77.       state != PORT_UNFIREWALLED)
  78.     fatal("addport: attempt to add port number %d with illegal state %d\n", portno, state);
  79.  
  80.   if (protocol == IPPROTO_TCP) {
  81.     if (!plist->tcp_ports) {
  82.       plist->tcp_ports = (struct port **) safe_malloc(65536 * sizeof(struct port *));
  83.       bzero(plist->tcp_ports, 65536 * sizeof(struct port *));
  84.     }
  85.     portarray = plist->tcp_ports;
  86.   } else if (protocol == IPPROTO_UDP) {
  87.     if (!plist->udp_ports) {
  88.       plist->udp_ports = (struct port **) safe_malloc(65536 * sizeof(struct port *));
  89.       bzero(plist->udp_ports, 65536 * sizeof(struct port *));
  90.     }
  91.     portarray = plist->udp_ports;
  92.   } else if (protocol == IPPROTO_IP) {
  93.     if (!plist->ip_prots) {
  94.       plist->ip_prots = (struct port **) safe_malloc(256 * sizeof(struct port *));
  95.       bzero(plist->ip_prots, 256 * sizeof(struct port *));
  96.     }
  97.     portarray = plist->ip_prots;
  98.   } else fatal("addport: attempted port insertion with invalid protocol");
  99.  
  100.   if (portarray[portno]) {
  101.     /* We must discount our statistics from the old values.  Also warn
  102.        if a complete duplicate */
  103.     current = portarray[portno];    
  104.     if (o.debugging && current->state == state && (!owner || !*owner)) {
  105.       error("Duplicate port (%hu/%s)\n", portno ,
  106.         (protocol == IPPROTO_TCP)? "tcp":
  107.         (protocol == IPPROTO_UDP)? "udp": "ip");
  108.     } 
  109.     plist->state_counts[current->state]--;
  110.     if (current->proto == IPPROTO_TCP) {
  111.       plist->state_counts_tcp[current->state]--;
  112.     } else if (current->proto == IPPROTO_UDP) {
  113.       plist->state_counts_udp[current->state]--;
  114.     } else
  115.       plist->state_counts_ip[current->state]--;
  116.   } else {
  117.     portarray[portno] = make_empty_port();
  118.     current = portarray[portno];
  119.     plist->numports++;
  120.     current->rpc_status = RPC_STATUS_UNTESTED;
  121.     current->confidence = CONF_HIGH;
  122.     current->portno = portno;
  123.   }
  124.   
  125.   plist->state_counts[state]++;
  126.   current->state = state;
  127.   if (protocol == IPPROTO_TCP) {
  128.     plist->state_counts_tcp[state]++;
  129.   } else if (protocol == IPPROTO_UDP) {
  130.     plist->state_counts_udp[state]++;
  131.   } else
  132.     plist->state_counts_ip[state]++;
  133.   current->proto = protocol;
  134.  
  135.   if (owner && *owner) {
  136.     if (current->owner)
  137.       free(current->owner);
  138.     current->owner = strdup(owner);
  139.   }
  140.  
  141.   return 0; /*success */
  142. }
  143.  
  144. int deleteport(portlist *plist, u16 portno, u8 protocol) {
  145.   struct port *answer = NULL;
  146.  
  147.   if (protocol == IPPROTO_TCP && plist->tcp_ports) {
  148.    answer = plist->tcp_ports[portno];
  149.    plist->tcp_ports[portno] = NULL;
  150.   }
  151.  
  152.   if (protocol == IPPROTO_UDP && plist->udp_ports) {  
  153.     answer = plist->udp_ports[portno];
  154.     plist->udp_ports[portno] = NULL;
  155.   } else if (protocol == IPPROTO_IP && plist->ip_prots) {
  156.     answer = plist->ip_prots[portno] = NULL;
  157.   }
  158.  
  159.   if (!answer)
  160.     return -1;
  161.  
  162.   if (o.verbose) {  
  163.     log_write(LOG_STDOUT, "Deleting port %hu/%s, which we thought was %s\n",
  164.           portno, (answer->proto == IPPROTO_TCP)? "tcp" : "udp", 
  165.           statenum2str(answer->state));
  166.     log_flush(LOG_STDOUT);
  167.   }    
  168.  
  169.   free_port(answer);
  170.   return 0;
  171. }
  172.  
  173.  
  174. struct port *lookupport(portlist *ports, u16 portno, u8 protocol) {
  175.  
  176.   if (protocol == IPPROTO_TCP && ports->tcp_ports)
  177.     return ports->tcp_ports[portno];
  178.  
  179.   if (protocol == IPPROTO_UDP && ports->udp_ports)
  180.     return ports->udp_ports[portno];
  181.  
  182.   if (protocol == IPPROTO_IP && ports->ip_prots)
  183.     return ports->ip_prots[portno];
  184.  
  185.   return NULL;
  186. }
  187.  
  188.  
  189. /* RECYCLES the port so that it can later be obtained again using 
  190.    make_port_structure */
  191. void free_port(struct port *pt) {
  192.   struct port *tmp;
  193.   if (pt->owner)
  194.     free(pt->owner);
  195.   tmp = freeportlist;
  196.   freeportlist = pt;
  197.   pt->next = tmp;
  198. }
  199.  
  200. struct port *make_empty_port() {
  201. int i;
  202. struct port *newpt;
  203.  
  204.  if (!freeportlist) {
  205.    freeportlist = (struct port *) safe_malloc(sizeof(struct port) * 1024);
  206.    for(i=0; i < 1023; i++)
  207.      freeportlist[i].next = &freeportlist[i+1];
  208.    freeportlist[1023].next = NULL;
  209.  }
  210.  
  211.  newpt = freeportlist;
  212.  freeportlist = freeportlist->next;
  213.  bzero(newpt, sizeof(struct port));
  214.  return newpt;
  215. }
  216.  
  217. /* Empties out a portlist so that it can be reused (or freed).  All the 
  218.    internal structures that must be freed are done so here. */
  219. void resetportlist(portlist *plist) {
  220.   int i;
  221.   if (plist->tcp_ports) {  
  222.     for(i=0; i < 65536; i++) {
  223.       if (plist->tcp_ports[i])
  224.     free_port(plist->tcp_ports[i]);
  225.     }
  226.     free(plist->tcp_ports);
  227.   }
  228.  
  229.   if (plist->udp_ports) {  
  230.     for(i=0; i < 65536; i++) {
  231.       if (plist->udp_ports[i])
  232.     free_port(plist->udp_ports[i]);
  233.     }
  234.     free(plist->udp_ports);
  235.   }
  236.  
  237.   if (plist->ip_prots) {
  238.     for(i=0; i < 256; ++i) {
  239.       if (plist->ip_prots[i])
  240.     free_port(plist->ip_prots[i]);
  241.     }
  242.     free(plist->ip_prots);
  243.   }
  244.  
  245.   bzero(plist, sizeof(portlist));
  246. }
  247.  
  248.  
  249. /* Decide which port we want to ignore in output (for example, we don't want
  250.  to show closed ports if there are 40,000 of them.) */
  251. void assignignoredportstate(portlist *plist) {
  252.  
  253.   if (plist->state_counts[PORT_FIREWALLED] > 10 + 
  254.       MAX(plist->state_counts[PORT_UNFIREWALLED], 
  255.       plist->state_counts[PORT_CLOSED])) {
  256.     plist->ignored_port_state = PORT_FIREWALLED;
  257.   } else if (plist->state_counts[PORT_UNFIREWALLED] > 
  258.          plist->state_counts[PORT_CLOSED]) {
  259.     plist->ignored_port_state = PORT_UNFIREWALLED;
  260.   } else plist->ignored_port_state = PORT_CLOSED;
  261. }
  262.  
  263.  
  264.  
  265. /* A function for iterating through the ports.  Give NULL for the
  266.    first "afterthisport".  Then supply the most recent returned port
  267.    for each subsequent call.  When no more matching ports remain, NULL
  268.    will be returned.  To restrict returned ports to just one protocol,
  269.    specify IPPROTO_TCP or IPPROTO_UDP for allowed_protocol.  A 0 for
  270.    allowed_protocol matches either.  allowed_state works in the same
  271.    fashion as allowed_protocol. This function returns ports in numeric
  272.    order from lowest to highest, except that if you ask for both TCP &
  273.    UDP, every TCP port will be returned before we start returning UDP
  274.    ports */
  275.  
  276. struct port *nextport(portlist *plist, struct port *afterthisport, 
  277.               u8 allowed_protocol, int allowed_state) {
  278.  
  279.   /* These two are chosen because they come right "before" port 1/tcp */
  280. unsigned int current_portno = 0;
  281. unsigned int current_proto = IPPROTO_TCP;
  282.  
  283. if (afterthisport) {
  284.   current_portno = afterthisport->portno;
  285.   current_proto = afterthisport->proto;  /* (afterthisport->proto == IPPROTO_TCP)? IPPROTO_TCP : IPPROTO_UDP; */
  286.  
  287.  current_portno++; /* Start on the port after the one we were given */
  288.  
  289. /* First we look for TCP ports ... */
  290. if (current_proto == IPPROTO_TCP) {
  291.  if ((allowed_protocol == 0 || allowed_protocol == IPPROTO_TCP) && 
  292.     current_proto == IPPROTO_TCP && plist->tcp_ports)
  293.   for(; current_portno < 65536; current_portno++) {
  294.     if (plist->tcp_ports[current_portno] &&
  295.     (!allowed_state || plist->tcp_ports[current_portno]->state == allowed_state))
  296.       return plist->tcp_ports[current_portno];
  297.   }
  298.  
  299.   /*  Uh-oh.  We have tried all tcp ports, lets move to udp */
  300.   current_portno = 0;
  301.   current_proto = IPPROTO_UDP;
  302. }
  303.  
  304. if ((allowed_protocol == 0 || allowed_protocol == IPPROTO_UDP) && 
  305.     current_proto == IPPROTO_UDP && plist->udp_ports) {
  306.   for(; current_portno < 65536; current_portno++) {
  307.     if (plist->udp_ports[current_portno] &&
  308.     (!allowed_state || plist->udp_ports[current_portno]->state == allowed_state))
  309.       return plist->udp_ports[current_portno];
  310.   }
  311. }
  312.  
  313. /*  No more ports */
  314. return NULL;
  315. }
  316.  
  317.