File: portsentry_util.c

package info (click to toggle)
portsentry 1.2-16
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 648 kB
  • sloc: ansic: 3,670; makefile: 414; sh: 202; perl: 94; csh: 31
file content (145 lines) | stat: -rw-r--r-- 4,618 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/************************************************************************/
/*                                                                      */
/* PortSentry								*/
/*                                                                      */
/* Created: 10-12-1997                                                  */
/* Modified: 05-23-2003                                                 */
/*                                                                      */
/* Send all changes/modifications/bugfixes to:				*/
/* craigrowland at users dot sourceforge dot net    			*/
/*                                                                      */
/*                                                                      */
/* This software is Copyright(c) 1997-2003 Craig Rowland	        */
/*                                                                      */
/* This software is covered under the Common Public License v1.0	*/
/* See the enclosed LICENSE file for more information.			*/
/* $Id: portsentry_util.c,v 1.11 2003/05/23 17:41:59 crowland Exp crowland $ */
/************************************************************************/


#include "portsentry.h"
#include "portsentry_io.h"
#include "portsentry_util.h"

/* A replacement for strncpy that covers mistakes a little better */
char *
SafeStrncpy (char *dest, const char *src, size_t size)
{
  if (!dest)
    {
      dest = NULL;
      return (NULL);
    }
  else if (size < 1)
    {
      dest = NULL;
      return (NULL);
    }

  /* Null terminate string. Why the hell strncpy doesn't do this */
  /* for you is mystery to me. God I hate C. */
  memset (dest, '\0', size);
  strncpy (dest, src, size - 1);

  return (dest);
}


/************************************************************************/
/* Generic safety function to process an IP address and remove anything */
/* that is:                                                             */
/* 1) Not a number.                                                     */
/* 2) Not a period.                                                     */
/* 3) Greater than IPMAXBUF (15)                                        */
/************************************************************************/
char *
CleanIpAddr (char *cleanAddr, const char *dirtyAddr)
{
  int count = 0, maxdot = 0, maxoctet = 0;

#ifdef DEBUG
  Log("debug: cleanAddr: Cleaning Ip address: %s", dirtyAddr);
#endif

  memset (cleanAddr, '\0', IPMAXBUF);
  /* dirtyAddr must be valid */
  if(dirtyAddr == NULL)
	return(cleanAddr);

  for (count = 0; count < IPMAXBUF - 1; count++)
    {
      if (isdigit (dirtyAddr[count]))
	{
	  if (++maxoctet > 3)
	    {
	      cleanAddr[count] = '\0';
	      break;
	    }
	  cleanAddr[count] = dirtyAddr[count];
	}
      else if (dirtyAddr[count] == '.')
	{
	  if (++maxdot > 3)
	    {
	      cleanAddr[count] = '\0';
	      break;
	    }
	  maxoctet = 0;
	  cleanAddr[count] = dirtyAddr[count];
	}
      else
	{
	  cleanAddr[count] = '\0';
	  break;
	}
    }

#ifdef DEBUG
  Log("debug: cleanAddr: Cleaned IpAddress: %s Dirty IpAddress: %s", cleanAddr, dirtyAddr);
#endif

  return (cleanAddr);
}


/************************************************************************/
/* Generic safety function to process an unresolved address and remove  */
/* anything that is:                                                    */
/* 1) Not a number.                                                     */
/* 2) Not a period.                                                     */
/* 3) Greater than DNSMAXBUF (255)                                      */
/* 4) Not a legal DNS character (a-z, A-Z, 0-9, - )			*/
/* 									*/
/* XXX THIS FUNCTION IS NOT COMPLETE 					*/
/************************************************************************/
int CleanAndResolve (char *resolvedHost, const char *unresolvedHost)
{
  struct hostent *hostPtr = NULL;
  struct in_addr addr;

#ifdef DEBUG
  Log("debug: CleanAndResolv: Resolving address: %s", unresolvedHost);
#endif

  memset (resolvedHost, '\0', DNSMAXBUF);
  /* unresolvedHost must be valid */
  if(unresolvedHost == NULL)
	return(ERROR);
  
  /* Not a valid address */
  if ((inet_aton(unresolvedHost, &addr)) == 0)
	return(ERROR);

  hostPtr = gethostbyaddr ((char *) &addr.s_addr, sizeof (addr.s_addr), AF_INET);
  if (hostPtr != NULL)
  	snprintf (resolvedHost, DNSMAXBUF, "%s", hostPtr->h_name);
  else
  	snprintf (resolvedHost, DNSMAXBUF, "%s", unresolvedHost);

#ifdef DEBUG
  Log("debug: CleanAndResolve: Cleaned Resolved: %s Dirty Unresolved: %s", resolvedHost, unresolvedHost);
#endif

  return (TRUE);
}