File: sanitizestring.c

package info (click to toggle)
openswan 1%3A2.4.6%2Bdfsg.2-1.1%2Betch2
  • links: PTS
  • area: main
  • in suites: etch
  • size: 25,000 kB
  • ctags: 16,877
  • sloc: ansic: 121,112; sh: 19,782; xml: 9,699; asm: 4,422; perl: 4,087; makefile: 3,367; tcl: 713; exp: 657; yacc: 396; pascal: 328; lex: 289; sed: 265; awk: 124; lisp: 3
file content (177 lines) | stat: -rw-r--r-- 4,293 bytes parent folder | download | duplicates (2)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * sanitize a string into a printable format.
 *
 * Copyright (C) 1998-2002  D. Hugh Redelmeier.
 * Copyright (C) 2003  Michael Richardson <mcr@freeswan.org>
 * 
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Library General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/lgpl.txt>.
 * 
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
 * License for more details.
 *
 * RCSID $Id: sanitizestring.c,v 1.3 2004/10/17 23:57:06 mcr Exp $
 */

#include <ctype.h>
#include <string.h>
#include "openswan.h"
#include "openswan/passert.h"
#include "constants.h"
#include "oswlog.h"

/* Sanitize character string in situ: turns dangerous characters into \OOO.
 * With a bit of work, we could use simpler reps for \\, \r, etc.,
 * but this is only to protect against something that shouldn't be used.
 * Truncate resulting string to what fits in buffer.
 */
size_t
sanitize_string(char *buf, size_t size)
{
#   define UGLY_WIDTH	4	/* width for ugly character: \OOO */
    size_t len;
    size_t added = 0;
    char *p;

    passert(size >= UGLY_WIDTH);	/* need room to swing cat */

    /* find right side of string to be sanitized and count
     * number of columns to be added.  Stop on end of string
     * or lack of room for more result.
     */
    for (p = buf; *p != '\0' && &p[added] < &buf[size - UGLY_WIDTH]; p++)
    {
	unsigned char c = *p;

	/* exception is that all veritical space just becomes white space */
	if (c == '\n' || c == '\r') {
	  *p = ' ';
	  continue;
	}

	if (c == '\\' || !isprint(c))
	    added += UGLY_WIDTH - 1;
    }

    /* at this point, p points after last original character to be
     * included.  added is how many characters are added to sanitize.
     * so p[added] will point after last sanitized character.
     */

    p[added] = '\0';
    len = &p[added] - buf;

    /* scan backwards, copying characters to their new home
     * and inserting the expansions for ugly characters.
     *
     * vertical space is changed to horizontal.
     *
     * It is finished when no more shifting is required.
     * This is a predecrement loop.
     */
    while (added != 0)
    {
	char fmtd[UGLY_WIDTH + 1];
	unsigned char c;

	while ((c = *--p) != '\\' && isprint(c))
	    p[added] = c;

	added -= UGLY_WIDTH - 1;
	snprintf(fmtd, sizeof(fmtd), "\\%03o", c);
	memcpy(p + added, fmtd, UGLY_WIDTH);
    }
    return len;
#   undef UGLY_WIDTH
}

#ifdef SANITIZESTRING_MAIN

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

void regress(void);

void openswan_passert_fail(const char *pred_str
	     , const char *file_str
	     , unsigned long line_no)
{
  fprintf(stderr, "Passert failed: %s: %d, %s\n",
	  file_str, line_no, pred_str);
  exit(20);
}

void pexpect_log(const char *pred_str
		 , const char *file_str, unsigned long line_no)
{
  fprintf(stderr, "Passert failed: %s: %d, %s\n",
	  file_str, line_no, pred_str);
}

void switch_fail(int n
    , const char *file_str, unsigned long line_no) 
{
  fprintf(stderr, "switch failed: %s: %d, %s\n",
	  file_str, line_no, pred_str);
  exit(20);
}


int
main(int argc, char *argv[])
{
	if (argc < 2) {
		fprintf(stderr, "Usage: %s -r\n", argv[0]);
		exit(2);
	}

	if (strcmp(argv[1], "-r") == 0) {
		regress();
		fprintf(stderr, "regress() returned?!?\n");
		exit(1);
	}
	exit(0);
}

struct rtab {
	char *input;
	char *output;			/* NULL means error expected */
} rtab[] = {
	{"there\001 \002 \003\n\rhi",	"1.2.3.0"},
	{"there\231\t\n\177\\\n\rhi",	"1.2.3.0"},
	{"there\001 \002 \003\n\rhi",	"1.2.3.0"},
	{NULL,				NULL}
};

void
regress()
{
	struct rtab *r;
	ip_address a;
	char in[256];
	int  count, status;

	count = 0;
	status = 0;

	for (r = rtab; r->input != NULL; r++) {
		strcpy(in, r->input);

		sanitize(in, sizeof(in));

		if(strcmp(in, r->output) != 0) {
		  printf("Item %d failed; %s vs %s\n",
			 count, in, r->output);
		  status = 1;
		}
	}
	exit(status);
}

#endif /* SANITIZESTRING_MAIN */