This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.
[ira/wip.git] / source / include / safe_string.h
1 /* 
2    Unix SMB/CIFS implementation.
3    Safe string handling routines.
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #ifndef _SAFE_STRING_H
23 #define _SAFE_STRING_H
24
25 #ifndef _SPLINT_ /* http://www.splint.org */
26
27 /* Some macros to ensure people don't use buffer overflow vulnerable string
28    functions. */
29
30 #ifdef bcopy
31 #undef bcopy
32 #endif /* bcopy */
33 #define bcopy(src,dest,size) __ERROR__XX__NEVER_USE_BCOPY___;
34
35 #ifdef strcpy
36 #undef strcpy
37 #endif /* strcpy */
38 #define strcpy(dest,src) __ERROR__XX__NEVER_USE_STRCPY___;
39
40 #ifdef strcat
41 #undef strcat
42 #endif /* strcat */
43 #define strcat(dest,src) __ERROR__XX__NEVER_USE_STRCAT___;
44
45 #ifdef sprintf
46 #undef sprintf
47 #endif /* sprintf */
48 #define sprintf __ERROR__XX__NEVER_USE_SPRINTF__;
49
50 #endif /* !_SPLINT_ */
51
52 #ifdef DEVELOPER
53 #define SAFE_STRING_FUNCTION_NAME FUNCTION_MACRO
54 #define SAFE_STRING_LINE __LINE__
55 #else
56 #define SAFE_STRING_FUNCTION_NAME ("")
57 #define SAFE_STRING_LINE (0)
58 #endif
59
60 /* We need a number of different prototypes for our 
61    non-existant fuctions */
62 char * __unsafe_string_function_usage_here__(void);
63
64 size_t __unsafe_string_function_usage_here_size_t__(void);
65
66 size_t __unsafe_string_function_usage_here_char__(void);
67
68 #ifdef HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS
69
70 /* if the compiler will optimize out function calls, then use this to tell if we are 
71    have the correct types (this works only where sizeof() returns the size of the buffer, not
72    the size of the pointer). */
73
74 #define CHECK_STRING_SIZE(d, len) (sizeof(d) != (len) && sizeof(d) != sizeof(char *))
75
76 #define fstrterminate(d) (CHECK_STRING_SIZE(d, sizeof(fstring)) \
77     ? __unsafe_string_function_usage_here_char__() \
78     : (((d)[sizeof(fstring)-1]) = '\0'))
79 #define pstrterminate(d) (CHECK_STRING_SIZE(d, sizeof(pstring)) \
80     ? __unsafe_string_function_usage_here_char__() \
81     : (((d)[sizeof(pstring)-1]) = '\0'))
82
83 #define wpstrcpy(d,s) ((sizeof(d) != sizeof(wpstring) && sizeof(d) != sizeof(smb_ucs2_t *)) \
84     ? __unsafe_string_function_usage_here__() \
85     : safe_strcpy_w((d),(s),sizeof(wpstring)))
86 #define wpstrcat(d,s) ((sizeof(d) != sizeof(wpstring) && sizeof(d) != sizeof(smb_ucs2_t *)) \
87     ? __unsafe_string_function_usage_here__() \
88     : safe_strcat_w((d),(s),sizeof(wpstring)))
89 #define wfstrcpy(d,s) ((sizeof(d) != sizeof(wfstring) && sizeof(d) != sizeof(smb_ucs2_t *)) \
90     ? __unsafe_string_function_usage_here__() \
91     : safe_strcpy_w((d),(s),sizeof(wfstring)))
92 #define wfstrcat(d,s) ((sizeof(d) != sizeof(wfstring) && sizeof(d) != sizeof(smb_ucs2_t *)) \
93     ? __unsafe_string_function_usage_here__() \
94     : safe_strcat_w((d),(s),sizeof(wfstring)))
95
96 #define push_pstring_base(dest, src, pstring_base) \
97     (CHECK_STRING_SIZE(pstring_base, sizeof(pstring)) \
98     ? __unsafe_string_function_usage_here_size_t__() \
99     : push_ascii(dest, src, sizeof(pstring)-PTR_DIFF(dest,pstring_base)-1, STR_TERMINATE))
100
101 #else /* HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS */
102
103 #define fstrterminate(d) (((d)[sizeof(fstring)-1]) = '\0')
104 #define pstrterminate(d) (((d)[sizeof(pstring)-1]) = '\0')
105
106 #define wpstrcpy(d,s) safe_strcpy_w((d),(s),sizeof(wpstring))
107 #define wpstrcat(d,s) safe_strcat_w((d),(s),sizeof(wpstring))
108 #define wfstrcpy(d,s) safe_strcpy_w((d),(s),sizeof(wfstring))
109 #define wfstrcat(d,s) safe_strcat_w((d),(s),sizeof(wfstring))
110
111 #define push_pstring_base(dest, src, pstring_base) \
112     push_ascii(dest, src, sizeof(pstring)-PTR_DIFF(dest,pstring_base)-1, STR_TERMINATE)
113
114 #endif /* HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS */
115
116 #define safe_strcpy_base(dest, src, base, size) \
117     safe_strcpy(dest, src, size-PTR_DIFF(dest,base)-1)
118
119 /* String copy functions - macro hell below adds 'type checking' (limited,
120    but the best we can do in C) and may tag with function name/number to
121    record the last 'clobber region' on that string */
122
123 #define pstrcpy(d,s) safe_strcpy((d), (s),sizeof(pstring)-1)
124 #define pstrcat(d,s) safe_strcat((d), (s),sizeof(pstring)-1)
125 #define fstrcpy(d,s) safe_strcpy((d),(s),sizeof(fstring)-1)
126 #define fstrcat(d,s) safe_strcat((d),(s),sizeof(fstring)-1)
127
128 /* the addition of the DEVELOPER checks in safe_strcpy means we must
129  * update a lot of code. To make this a little easier here are some
130  * functions that provide the lengths with less pain */
131 #define pstrcpy_base(dest, src, pstring_base) \
132     safe_strcpy(dest, src, sizeof(pstring)-PTR_DIFF(dest,pstring_base)-1)
133
134
135 /* Inside the _fn variants of these is a call to clobber_region(), -
136  * which might destroy the stack on a buggy function.  We help the
137  * debugging process by putting the function and line who last caused
138  * a clobbering into a static buffer.  If the program crashes at
139  * address 0xf1f1f1f1 then this function is probably, but not
140  * necessarily, to blame. */
141
142 /* overmalloc_safe_strcpy: DEPRECATED!  Used when you know the
143  * destination buffer is longer than maxlength, but you don't know how
144  * long.  This is not a good situation, because we can't do the normal
145  * sanity checks. Don't use in new code! */
146
147 #define overmalloc_safe_strcpy(dest,src,maxlength)      safe_strcpy_fn(SAFE_STRING_FUNCTION_NAME, SAFE_STRING_LINE,dest,src,maxlength)
148 #define safe_strcpy(dest,src,maxlength) safe_strcpy_fn2(SAFE_STRING_FUNCTION_NAME, SAFE_STRING_LINE,dest,src,maxlength)
149 #define safe_strcat(dest,src,maxlength) safe_strcat_fn2(SAFE_STRING_FUNCTION_NAME, SAFE_STRING_LINE,dest,src,maxlength)
150 #define push_string(base_ptr, dest, src, dest_len, flags) push_string_fn2(SAFE_STRING_FUNCTION_NAME, SAFE_STRING_LINE, base_ptr, dest, src, dest_len, flags)
151 #define pull_string(base_ptr, dest, src, dest_len, src_len, flags) pull_string_fn2(SAFE_STRING_FUNCTION_NAME, SAFE_STRING_LINE, base_ptr, dest, src, dest_len, src_len, flags)
152 #define clistr_push(cli, dest, src, dest_len, flags) clistr_push_fn2(SAFE_STRING_FUNCTION_NAME, SAFE_STRING_LINE, cli, dest, src, dest_len, flags)
153 #define clistr_pull(cli, dest, src, dest_len, src_len, flags) clistr_pull_fn2(SAFE_STRING_FUNCTION_NAME, SAFE_STRING_LINE, cli, dest, src, dest_len, src_len, flags)
154 #define srvstr_push(base_ptr, dest, src, dest_len, flags) srvstr_push_fn2(SAFE_STRING_FUNCTION_NAME, SAFE_STRING_LINE, base_ptr, dest, src, dest_len, flags)
155
156 #define alpha_strcpy(dest,src,other_safe_chars,maxlength) alpha_strcpy_fn(SAFE_STRING_FUNCTION_NAME,SAFE_STRING_LINE,dest,src,other_safe_chars,maxlength)
157 #define StrnCpy(dest,src,n)             StrnCpy_fn(SAFE_STRING_FUNCTION_NAME,SAFE_STRING_LINE,dest,src,n)
158
159 #ifdef HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS
160
161 /* if the compiler will optimize out function calls, then use this to tell if we are 
162    have the correct types (this works only where sizeof() returns the size of the buffer, not
163    the size of the pointer). */
164
165 #define safe_strcpy_fn2(fn_name, fn_line, d, s, max_len) \
166     (CHECK_STRING_SIZE(d, max_len+1) \
167     ? __unsafe_string_function_usage_here__() \
168     : safe_strcpy_fn(fn_name, fn_line, (d), (s), (max_len)))
169
170 #define safe_strcat_fn2(fn_name, fn_line, d, s, max_len) \
171     (CHECK_STRING_SIZE(d, max_len+1) \
172     ? __unsafe_string_function_usage_here__() \
173     : safe_strcat_fn(fn_name, fn_line, (d), (s), (max_len)))
174
175 #define push_string_fn2(fn_name, fn_line, base_ptr, dest, src, dest_len, flags) \
176     (CHECK_STRING_SIZE(dest, dest_len) \
177     ? __unsafe_string_function_usage_here_size_t__() \
178     : push_string_fn(fn_name, fn_line, base_ptr, dest, src, dest_len, flags))
179
180 #define pull_string_fn2(fn_name, fn_line, base_ptr, dest, src, dest_len, src_len, flags) \
181     (CHECK_STRING_SIZE(dest, dest_len) \
182     ? __unsafe_string_function_usage_here_size_t__() \
183     : pull_string_fn(fn_name, fn_line, base_ptr, dest, src, dest_len, src_len, flags))
184
185 #define clistr_push_fn2(fn_name, fn_line, cli, dest, src, dest_len, flags) \
186     (CHECK_STRING_SIZE(dest, dest_len) \
187     ? __unsafe_string_function_usage_here_size_t__() \
188     : clistr_push_fn(fn_name, fn_line, cli, dest, src, dest_len, flags))
189
190 #define clistr_pull_fn2(fn_name, fn_line, cli, dest, src, dest_len, srclen, flags) \
191     (CHECK_STRING_SIZE(dest, dest_len) \
192     ? __unsafe_string_function_usage_here_size_t__() \
193     : clistr_pull_fn(fn_name, fn_line, cli, dest, src, dest_len, srclen, flags))
194
195 #define srvstr_push_fn2(fn_name, fn_line, base_ptr, dest, src, dest_len, flags) \
196     (CHECK_STRING_SIZE(dest, dest_len) \
197     ? __unsafe_string_function_usage_here_size_t__() \
198     : srvstr_push_fn(fn_name, fn_line, base_ptr, dest, src, dest_len, flags))
199
200 #else
201
202 #define safe_strcpy_fn2 safe_strcpy_fn
203 #define safe_strcat_fn2 safe_strcat_fn
204 #define push_string_fn2 push_string_fn
205 #define pull_string_fn2 pull_string_fn
206 #define clistr_push_fn2 clistr_push_fn
207 #define clistr_pull_fn2 clistr_pull_fn
208 #define srvstr_push_fn2 srvstr_push_fn
209
210 #endif
211
212 /* replace some string functions with multi-byte
213    versions */
214 #define strlower(s) strlower_m(s)
215 #define strupper(s) strupper_m(s)
216
217 #endif