home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / nmap254b.zip / utils.c < prev    next >
C/C++ Source or Header  |  2002-02-25  |  16KB  |  585 lines

  1.  
  2. /***********************************************************************/
  3. /* utils.c -- Various miscellaneous utility functions which defy       */
  4. /* categorization :)                                                   */
  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: utils.c,v 1.27 2001/07/30 06:08:00 fyodor Exp $ */
  46.  
  47.  
  48. #include "utils.h"
  49.  
  50.  
  51. void *safe_malloc(int size)
  52. {
  53.   void *mymem;
  54.   if (size < 0)
  55.     fatal("Tried to malloc negative amount of memory!!!");
  56.   mymem = malloc(size);
  57.   if (mymem == NULL)
  58.     fatal("Malloc Failed! Probably out of space.");
  59.   return mymem;
  60. }
  61.  
  62.  
  63. /* Hex dump */
  64. void hdump(unsigned char *packet, unsigned int len) {
  65. unsigned int i=0, j=0;
  66.  
  67. printf("Here it is:\n");
  68.  
  69. for(i=0; i < len; i++){
  70.   j = (unsigned) (packet[i]);
  71.   printf("%-2X ", j);
  72.   if (!((i+1)%16))
  73.     printf("\n");
  74.   else if (!((i+1)%4))
  75.     printf("  ");
  76. }
  77. printf("\n");
  78. }
  79.  
  80. /* A better version of hdump, from Lamont Granquist.  Modified slightly
  81.    by Fyodor (fyodor@insecure.org) */
  82. void lamont_hdump(unsigned char *bp, unsigned int length) {
  83.  
  84.   /* stolen from tcpdump, then kludged extensively */
  85.  
  86.   static const char asciify[] = "................................ !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~.................................................................................................................................";
  87.  
  88.   register const u_short *sp;
  89.   register const u_char *ap;
  90.   register u_int i, j;
  91.   register int nshorts, nshorts2;
  92.   register int padding;
  93.  
  94.   printf("\n\t");
  95.   padding = 0;
  96.   sp = (u_short *)bp;
  97.   ap = (u_char *)bp;
  98.   nshorts = (u_int) length / sizeof(u_short);
  99.   nshorts2 = (u_int) length / sizeof(u_short);
  100.   i = 0;
  101.   j = 0;
  102.   while(1) {
  103.     while (--nshorts >= 0) {
  104.       printf(" %04x", ntohs(*sp));
  105.       sp++;
  106.       if ((++i % 8) == 0)
  107.         break;
  108.     }
  109.     if (nshorts < 0) {
  110.       if ((length & 1) && (((i-1) % 8) != 0)) {
  111.         printf(" %02x  ", *(u_char *)sp);
  112.         padding++;
  113.       }
  114.       nshorts = (8 - (nshorts2 - nshorts));
  115.       while(--nshorts >= 0) {
  116.         printf("     ");
  117.       }
  118.       if (!padding) printf("     ");
  119.     }
  120.     printf("  ");
  121.  
  122.     while (--nshorts2 >= 0) {
  123.       printf("%c%c", asciify[*ap], asciify[*(ap+1)]);
  124.       ap += 2;
  125.       if ((++j % 8) == 0) {
  126.         printf("\n\t");
  127.         break;
  128.       }
  129.     }
  130.     if (nshorts2 < 0) {
  131.       if ((length & 1) && (((j-1) % 8) != 0)) {
  132.         printf("%c", asciify[*ap]);
  133.       }
  134.       break;
  135.     }
  136.   }
  137.   if ((length & 1) && (((i-1) % 8) == 0)) {
  138.     printf(" %02x", *(u_char *)sp);
  139.     printf("                                       %c", asciify[*ap]);
  140.   }
  141.   printf("\n");
  142. }
  143.  
  144. #ifndef HAVE_STRERROR
  145. char *strerror(int errnum) {
  146.   static char buf[1024];
  147.   sprintf(buf, "your system is too old for strerror of errno %d\n", errnum);
  148.   return buf;
  149. }
  150. #endif
  151.  
  152. /* Like the perl equivialent -- It removes the terminating newline from string
  153.    IF one exists.  It then returns the POSSIBLY MODIFIED string */
  154. char *chomp(char *string) {
  155.   int len;
  156.   len = strlen(string);
  157.   if (len < 1)
  158.     return string;
  159.   if (string[len - 1] != '\n')
  160.     return string;  
  161.   if (len > 1 && string[len-2] == '\r') {
  162.     string[len-2] = '\0';
  163.   } else string[len-1] = '\0';
  164.   return string;
  165. }
  166.  
  167.  
  168. int get_random_int() {
  169. int i;
  170. get_random_bytes(&i, sizeof(int));
  171. return i;
  172. }
  173.  
  174. unsigned int get_random_uint() {
  175. unsigned int i;
  176. get_random_bytes(&i, sizeof(unsigned int));
  177. return i;
  178. }
  179.  
  180. u32 get_random_u32() {
  181.   u32 i;
  182.   get_random_bytes(&i, sizeof(i));
  183.   return i;
  184. }
  185.  
  186. u8 get_random_u8() {
  187.   u8 i;
  188.   get_random_bytes(&i, sizeof(i));
  189.   return i;
  190. }
  191.  
  192. unsigned short get_random_ushort() {
  193. unsigned short s;
  194. get_random_bytes(&s, sizeof(unsigned short));
  195. return s;
  196. }
  197.  
  198. int get_random_bytes(void *buf, int numbytes) {
  199. static char bytebuf[2048];
  200. static char badrandomwarning = 0;
  201. static int bytesleft = 0;
  202. int res;
  203. int tmp;
  204. struct timeval tv;
  205. FILE *fp = NULL;
  206. unsigned int i;
  207. short *iptr;
  208.  
  209. if (numbytes < 0 || numbytes > 0xFFFF) return -1;
  210.  
  211. if (bytesleft == 0) {
  212.   fp = fopen("/dev/urandom", "r");
  213.   if (!fp) fp = fopen("/dev/random", "r");
  214.   if (fp) {
  215.     res = fread(bytebuf, 1, sizeof(bytebuf), fp);
  216.     if (res != sizeof(bytebuf)) {    
  217.       error("Failed to read from /dev/urandom or /dev/random");
  218.       fclose(fp);
  219.       fp = NULL;
  220.     }      
  221.     bytesleft = sizeof(bytebuf);
  222.   }
  223.   if (!fp) {  
  224.     if (badrandomwarning == 0) {
  225.       badrandomwarning++;
  226.       /*      error("WARNING: your system apparently does not offer /dev/urandom or /dev/random.  Reverting to less secure version."); */
  227.  
  228.       /* Seed our random generator */
  229.       gettimeofday(&tv, NULL);
  230.       srand((tv.tv_sec ^ tv.tv_usec) ^ getpid());
  231.     }
  232.  
  233.     for(i=0; i < sizeof(bytebuf) / sizeof(short); i++) {
  234.       iptr = (short *) ((char *)bytebuf + i * sizeof(short));
  235.       *iptr = rand();
  236.     }
  237.     bytesleft = (sizeof(bytebuf) / sizeof(short)) * sizeof(short);
  238.     /*    ^^^^^^^^^^^^^^^not as meaningless as it looks  */
  239.   } else fclose(fp);
  240. }
  241.  
  242. if (numbytes <= bytesleft) { /* we can cover it */
  243.   memcpy(buf, bytebuf + (sizeof(bytebuf) - bytesleft), numbytes);
  244.   bytesleft -= numbytes;
  245.   return 0;
  246. }
  247.  
  248. /* We don't have enough */
  249. memcpy(buf, bytebuf + (sizeof(bytebuf) - bytesleft), bytesleft);
  250. tmp = bytesleft;
  251. bytesleft = 0;
  252. return get_random_bytes((char *)buf + tmp, numbytes - tmp);
  253. }
  254.  
  255. /* Scramble the contents of an array*/
  256. void genfry(unsigned char *arr, int elem_sz, int num_elem) {
  257. int i;
  258. unsigned int pos;
  259. unsigned char *bytes;
  260. unsigned char *cptr;
  261. unsigned short *sptr;
  262. unsigned int *iptr;
  263. unsigned char *tmp;
  264. int bpe;
  265.  
  266. if (sizeof(unsigned char) != 1)
  267.   fatal("genfry() requires 1 byte chars");
  268.  
  269. if (num_elem < 2)
  270.   return;
  271.  
  272.  if (elem_sz == sizeof(unsigned short)) {
  273.    shortfry((unsigned short *)arr, num_elem);
  274.    return;
  275.  }
  276.  
  277. /* OK, so I am stingy with the random bytes! */
  278. if (num_elem < 256) 
  279.   bpe = sizeof(unsigned char);
  280. else if (num_elem < 65536)
  281.   bpe = sizeof(unsigned short);
  282. else bpe = sizeof(unsigned int);
  283.  
  284. bytes = (unsigned char *) malloc(bpe * num_elem);
  285. tmp = (unsigned char *) malloc(elem_sz);
  286.  
  287. get_random_bytes(bytes, bpe * num_elem);
  288. cptr = bytes;
  289. sptr = (unsigned short *)bytes;
  290. iptr = (unsigned int *) bytes;
  291.  
  292.  for(i=num_elem - 1; i > 0; i--) {
  293.    if (num_elem < 256) {
  294.      pos = *cptr; cptr++;
  295.    }
  296.    else if (num_elem < 65536) {
  297.      pos = *sptr; sptr++;
  298.    } else {
  299.      pos = *iptr; iptr++;
  300.    }
  301.    pos %= i+1;
  302.    memcpy(tmp, arr + elem_sz * i, elem_sz);
  303.    memcpy(arr + elem_sz * i, arr + elem_sz * pos, elem_sz);
  304.    memcpy(arr + elem_sz * pos, tmp, elem_sz);
  305.  }
  306.  free(bytes);
  307.  free(tmp);
  308. }
  309.  
  310. void shortfry(unsigned short *arr, int num_elem) {
  311. int num;
  312. unsigned short tmp;
  313. int i;
  314.  
  315. if (num_elem < 2)
  316.   return;
  317.  
  318.  for(i= num_elem - 1; i > 0 ; i--) {
  319.    num = get_random_ushort() % (i + 1);
  320.    tmp = arr[i];
  321.    arr[i] = arr[num];
  322.    arr[num] = tmp;
  323.  } 
  324.  
  325.  return;
  326. }
  327.  
  328. ssize_t Write(int fd, const void *buf, size_t count) {
  329.   int res;
  330.   unsigned int len;
  331.  
  332.   len = 0;
  333.   do {
  334.     res = write(fd,(char *) buf + len,count - len);
  335.     if (res > 0)
  336.       len += res;
  337.   } while(len < count && (res != -1 || errno == EINTR));
  338.  
  339.   return res;
  340. }
  341.  
  342.  
  343. /* gcd_1 and gcd_n_long were sent in by Peter Kosinar <goober@gjh.sk> 
  344.    Not needed for gcd_n_long, just for the case you'd want to have gcd
  345.    for two arguments too. */
  346. unsigned long gcd_ulong(unsigned long a, unsigned long b)
  347. {
  348.   /* Shorter
  349.      while (b) { a%=b; if (!a) return b; b%=a; } */
  350.   
  351.   /* Faster */
  352.   unsigned long c; 
  353.   if (a<b) { c=a; a=b; b=c; }
  354.   while (b) { c=a%b; a=b; b=c; }
  355.   
  356.   /* Common for both */
  357.   return a;
  358. }
  359.  
  360. unsigned long gcd_n_ulong(long nvals, unsigned long *val)
  361.  {
  362.    unsigned long a,b,c;
  363.    
  364.    if (!nvals) return 1;
  365.    a=*val;
  366.    for (nvals--;nvals;nvals--)
  367.      {
  368.        b=*++val;
  369.        if (a<b) { c=a; a=b; b=c; }
  370.        while (b) { c=a%b; a=b; b=c; }
  371.      }
  372.    return a;
  373.  }
  374.  
  375. unsigned int gcd_uint(unsigned int a, unsigned int b)
  376. {
  377.   /* Shorter
  378.      while (b) { a%=b; if (!a) return b; b%=a; } */
  379.   
  380.   /* Faster */
  381.   unsigned int c; 
  382.   if (a<b) { c=a; a=b; b=c; }
  383.   while (b) { c=a%b; a=b; b=c; }
  384.   
  385.   /* Common for both */
  386.   return a;
  387. }
  388.  
  389. unsigned int gcd_n_uint(int nvals, unsigned int *val)
  390.  {
  391.    unsigned int a,b,c;
  392.    
  393.    if (!nvals) return 1;
  394.    a=*val;
  395.    for (nvals--;nvals;nvals--)
  396.      {
  397.        b=*++val;
  398.        if (a<b) { c=a; a=b; b=c; }
  399.        while (b) { c=a%b; a=b; b=c; }
  400.      }
  401.    return a;
  402.  }
  403.  
  404. /* This function takes a command and the address of an uninitialized
  405.    char ** .  It parses the command (by seperating out whitespace)
  406.    into an argv[] style char **, which it sets the argv parameter to.
  407.    The function returns the number of items filled up in the array
  408.    (argc), or -1 in the case of an error.  This function allocates
  409.    memmory for argv and thus it must be freed -- use argv_parse_free()
  410.    for that.  If arg_parse returns <1, then argv does not need to be freed.
  411.    The returned arrays are always terminated with a NULL pointer */
  412. int arg_parse(const char *command, char ***argv) {
  413.   char **myargv = NULL;
  414.   int argc = 0;
  415.   char mycommand[4096];
  416.   char *start, *end;
  417.   char oldend;
  418.  
  419.   *argv = NULL;
  420.   if (Strncpy(mycommand, command, 4096) == -1) {      
  421.     return -1;
  422.   }
  423.   myargv = (char **) malloc((MAX_PARSE_ARGS + 2) * sizeof(char *));
  424.   bzero(myargv, (MAX_PARSE_ARGS+2) * sizeof(char *));
  425.   myargv[0] = (char *) 0x123456; /* Integrity checker */
  426.   myargv++;
  427.   start = mycommand;
  428.   while(start && *start) {
  429.     while(*start && isspace((int) *start))
  430.       start++;
  431.     if (*start == '"') {
  432.       start++;
  433.       end = strchr(start, '"');
  434.     } else if (*start == '\'') {
  435.       start++;
  436.       end = strchr(start, '\'');      
  437.     } else if (!*start) {
  438.       continue;
  439.     } else {
  440.       end = start+1;
  441.       while(*end && !isspace((int) *end)) {      
  442.     end++;
  443.       }
  444.     }
  445.     if (!end) {
  446.       arg_parse_free(myargv);
  447.       return -1;
  448.     }
  449.     if (argc >= MAX_PARSE_ARGS) {
  450.       arg_parse_free(myargv);
  451.       return -1;
  452.     }
  453.     oldend = *end;
  454.     *end = '\0';
  455.     myargv[argc++] = strdup(start);
  456.     if (oldend)
  457.       start = end + 1;
  458.     else start = end;
  459.   }
  460.   myargv[argc+1] = 0;
  461.   *argv = myargv;
  462.   return argc;
  463. }
  464.  
  465. /* Free an argv allocated inside arg_parse */
  466. void arg_parse_free(char **argv) {
  467.   char **current;
  468.   /* Integrity check */
  469.   argv--;
  470.   assert(argv[0] == (char *) 0x123456);
  471.   current = argv + 1;
  472.   while(*current) {
  473.     free(*current);
  474.     current++;
  475.   }
  476.   free(argv);
  477. }
  478.  
  479.  
  480. /* mmap() an entire file into the address space.  Returns a pointer
  481.    to the beginning of the file.  The mmap'ed length is returned
  482.    inside the length parameter.  If there is a problem, NULL is
  483.    returned, the value of length is undefined, and errno is set to
  484.    something appropriate.  The user is responsible for doing
  485.    an munmap(ptr, length) when finished with it.  openflags should 
  486.    be O_RDONLY or O_RDWR, or O_WRONLY
  487. */
  488.  
  489. #if !defined(WIN32) && !defined(__EMX__)
  490. char *mmapfile(char *fname, int *length, int openflags) {
  491.   struct stat st;
  492.   int fd;
  493.   char *fileptr;
  494.  
  495.   if (!length || !fname) {
  496.     errno = EINVAL;
  497.     return NULL;
  498.   }
  499.  
  500.   *length = -1;
  501.  
  502.   if (stat(fname, &st) == -1) {
  503.     errno = ENOENT;
  504.     return NULL;
  505.   }
  506.  
  507.   fd = open(fname, openflags);
  508.   if (fd == -1) {
  509.     return NULL;
  510.   }
  511.  
  512.   fileptr = (char *) mmap(0, st.st_size, (openflags & O_RDONLY)? PROT_READ :
  513.                  (openflags & O_RDWR)? (PROT_READ|PROT_WRITE) : PROT_WRITE,
  514.                  MAP_SHARED, fd, 0);
  515.  
  516.   close(fd);
  517.  
  518. #ifdef MAP_FAILED
  519.   if (fileptr == MAP_FAILED) return NULL;
  520. #else
  521.   if (fileptr == (char *) -1) return NULL;
  522. #endif
  523.  
  524.   *length = st.st_size;
  525.   return fileptr;
  526. }
  527. #elif defined(__EMX__)
  528. char *mmapfile(char *fname, int *length, int openflags) {
  529.    return NULL;
  530. }
  531. #else /* WIN32 */
  532. /* FIXME:  From the looks of it, this function can only handle one mmaped 
  533.    file at a time (note how gmap is used).*/
  534. /* I believe this was written by Ryan Permeh ( ryan@eeye.com) */
  535.  
  536. HANDLE gmap = 0;
  537. char *mmapfile(char *fname, int *length, int openflags) {
  538.     HANDLE fd;
  539.     char *fileptr;
  540.  
  541.     if (!length || !fname) {
  542.         WSASetLastError(EINVAL);
  543.         return NULL;
  544.     }
  545.  
  546.     *length = -1;
  547.  
  548.     fd= CreateFile(fname,
  549.         openflags,                // open for writing 
  550.         0,                            // do not share 
  551.         NULL,                         // no security 
  552.         OPEN_EXISTING,                // overwrite existing 
  553.         FILE_ATTRIBUTE_NORMAL,
  554.         NULL);                        // no attr. template 
  555.  
  556.     gmap=CreateFileMapping(fd,NULL, (openflags & O_RDONLY)? PAGE_READONLY:(openflags & O_RDWR)? (PAGE_READONLY|PAGE_READWRITE) : PAGE_READWRITE,0,0,NULL);
  557.  
  558.     fileptr = (char *)MapViewOfFile(gmap, FILE_MAP_ALL_ACCESS,0,0,0);
  559.     *length = (int) GetFileSize(fd,NULL);
  560.     CloseHandle(fd);
  561.  
  562.     #ifdef MAP_FAILED
  563.     if (fileptr == MAP_FAILED) return NULL;
  564.     #else
  565.     if (fileptr == (char *) -1) return NULL;
  566.     #endif
  567.     return fileptr;
  568. }
  569.  
  570.  
  571. /* FIXME:  This only works if the file was mapped by mmapfile (and only
  572.    works if the file is the most recently mapped one */
  573. int win32_munmap(char *filestr, int filelen)
  574. {
  575.     if(gmap == 0)
  576.         fatal("win32_munmap: no current mapping !\n");
  577.     FlushViewOfFile(filestr, filelen);
  578.     UnmapViewOfFile(filestr);
  579.     CloseHandle(gmap);
  580.     gmap = 0;
  581.     return 0;
  582. }
  583.  
  584. #endif
  585.