~ubuntu-branches/ubuntu/maverick/strongswan/maverick

« back to all changes in this revision

Viewing changes to src/libstrongswan/utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Rene Mayrhofer
  • Date: 2009-04-01 22:17:52 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20090401221752-eozrk0ctabblo94z
* New upstream release, which incorporates the fix. Removed dpatch for it.
  Closes: #521950: CVE-2009-0790: DoS
* New support for EAP RADIUS authentication, enabled for this package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C) 2008 Tobias Brunner
 
2
 * Copyright (C) 2008-2009 Tobias Brunner
3
3
 * Copyright (C) 2005-2008 Martin Willi
4
4
 * Hochschule fuer Technik Rapperswil
5
5
 *
13
13
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14
14
 * for more details.
15
15
 *
16
 
 * $Id: utils.c 4305 2008-08-28 07:47:55Z tobias $
 
16
 * $Id: utils.c 4936 2009-03-12 18:07:32Z tobias $
17
17
 */
18
18
 
19
19
#include "utils.h"
20
20
 
21
21
#include <sys/stat.h>
22
22
#include <string.h>
23
 
#include <pthread.h>
24
23
#include <stdio.h>
25
24
#include <unistd.h>
26
25
#include <dirent.h>
 
26
#include <time.h>
27
27
 
28
28
#include <enum.h>
29
29
#include <debug.h>
61
61
 */
62
62
void memxor(u_int8_t dest[], u_int8_t src[], size_t n)
63
63
{
64
 
        size_t i;
65
 
        for (i = 0; i < n; i++)
 
64
        int i = 0, m;
 
65
        
 
66
        m = n - sizeof(long);
 
67
        while (i < m)
 
68
        {
 
69
                *(long*)(dest + i) ^= *(long*)(src + i);
 
70
                i += sizeof(long);
 
71
        }
 
72
        while (i < n)
66
73
        {
67
74
                dest[i] ^= src[i];
68
 
        }
 
75
                i++;
 
76
        }
 
77
}
 
78
 
 
79
/**
 
80
 * Described in header.
 
81
 */
 
82
void *memstr(const void *haystack, const char *needle, size_t n)
 
83
{
 
84
        unsigned const char *pos = haystack;
 
85
        size_t l = strlen(needle);
 
86
        for (; n >= l; ++pos, --n)
 
87
        {
 
88
                if (memeq(pos, needle, l))
 
89
                {
 
90
                        return (void*)pos;
 
91
                }
 
92
        }
 
93
        return NULL;
69
94
}
70
95
 
71
96
/**
129
154
{
130
155
}
131
156
 
 
157
#ifndef HAVE_GCC_ATOMIC_OPERATIONS
 
158
#include <pthread.h>
 
159
 
132
160
/**
133
 
 * We use a single mutex for all refcount variables. This
134
 
 * is not optimal for performance, but the critical section
135
 
 * is not that long...
136
 
 * TODO: Consider to include a mutex in each refcount_t variable.
 
161
 * We use a single mutex for all refcount variables. 
137
162
 */
138
163
static pthread_mutex_t ref_mutex = PTHREAD_MUTEX_INITIALIZER;
139
164
 
140
165
/**
141
 
 * Described in header.
142
 
 * 
143
 
 * TODO: May be implemented with atomic CPU instructions
144
 
 * instead of a mutex.
 
166
 * Increase refcount
145
167
 */
146
168
void ref_get(refcount_t *ref)
147
169
{
151
173
}
152
174
 
153
175
/**
154
 
 * Described in header.
155
 
 * 
156
 
 * TODO: May be implemented with atomic CPU instructions
157
 
 * instead of a mutex.
 
176
 * Decrease refcount
158
177
 */
159
178
bool ref_put(refcount_t *ref)
160
179
{
165
184
        pthread_mutex_unlock(&ref_mutex);
166
185
        return !more_refs;
167
186
}
 
187
#endif /* HAVE_GCC_ATOMIC_OPERATIONS */
168
188
 
169
189
/**
170
 
 * output handler in printf() for time_t
 
190
 * Described in header.
171
191
 */
172
 
static int time_print(FILE *stream, const struct printf_info *info,
173
 
                                          const void *const *args)
 
192
int time_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
 
193
                                         const void *const *args)
174
194
{
175
195
        static const char* months[] = {
176
196
                "Jan", "Feb", "Mar", "Apr", "May", "Jun",
177
197
                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
178
198
        };
179
199
        time_t *time = *((time_t**)(args[0]));
180
 
        bool utc = TRUE;
 
200
        bool utc = *((bool*)(args[1]));;
181
201
        struct tm t;
182
202
        
183
 
        if (info->alt)
184
 
        {
185
 
                utc = *((bool*)(args[1]));
186
 
        }
187
203
        if (time == UNDEFINED_TIME)
188
204
        {
189
 
                return fprintf(stream, "--- -- --:--:--%s----",
190
 
                                           info->alt ? " UTC " : " ");
 
205
                return print_in_hook(dst, len, "--- -- --:--:--%s----",
 
206
                                                         utc ? " UTC " : " ");
191
207
        }
192
208
        if (utc)
193
209
        {
197
213
        {
198
214
                localtime_r(time, &t);
199
215
        }
200
 
        return fprintf(stream, "%s %02d %02d:%02d:%02d%s%04d",
201
 
                                   months[t.tm_mon], t.tm_mday, t.tm_hour, t.tm_min,
202
 
                                   t.tm_sec, utc ? " UTC " : " ", t.tm_year + 1900);
203
 
}
204
 
 
205
 
/**
206
 
 * arginfo handler for printf() time
207
 
 */
208
 
static int time_arginfo(const struct printf_info *info, size_t n, int *argtypes)
209
 
{
210
 
        if (info->alt)
211
 
        {
212
 
                if (n > 1)
213
 
                {
214
 
                        argtypes[0] = PA_POINTER;
215
 
                        argtypes[1] = PA_INT;
216
 
                }
217
 
                return 2;
218
 
        }
219
 
        
220
 
        if (n > 0)
221
 
        {
222
 
                argtypes[0] = PA_POINTER;
223
 
        }
224
 
        return 1;
225
 
}
226
 
 
227
 
/**
228
 
 * output handler in printf() for time deltas
229
 
 */
230
 
static int time_delta_print(FILE *stream, const struct printf_info *info,
231
 
                                                        const void *const *args)
 
216
        return print_in_hook(dst, len, "%s %02d %02d:%02d:%02d%s%04d",
 
217
                                                 months[t.tm_mon], t.tm_mday, t.tm_hour, t.tm_min,
 
218
                                                 t.tm_sec, utc ? " UTC " : " ", t.tm_year + 1900);
 
219
}
 
220
 
 
221
/**
 
222
 * Described in header.
 
223
 */
 
224
int time_delta_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
 
225
                                                   const void *const *args)
232
226
{
233
227
        char* unit = "second";
234
 
        time_t *arg1, *arg2;
235
 
        time_t delta;
 
228
        time_t *arg1 = *((time_t**)(args[0]));
 
229
        time_t *arg2 = *((time_t**)(args[1]));
 
230
        time_t delta = abs(*arg1 - *arg2);
236
231
        
237
 
        arg1 = *((time_t**)(args[0]));
238
 
        if (info->alt)
239
 
        {
240
 
                arg2 = *((time_t**)(args[1]));
241
 
                delta = abs(*arg1 - *arg2);
242
 
        }
243
 
        else
244
 
        {
245
 
                delta = *arg1;
246
 
        }
247
 
 
248
232
        if (delta > 2 * 60 * 60 * 24)
249
233
        {
250
234
                delta /= 60 * 60 * 24;
260
244
                delta /= 60;
261
245
                unit = "minute";
262
246
        }
263
 
        return fprintf(stream, "%d %s%s", delta, unit, (delta == 1)? "":"s");
264
 
}
265
 
 
266
 
/**
267
 
 * arginfo handler for printf() time deltas
268
 
 */
269
 
int time_delta_arginfo(const struct printf_info *info, size_t n, int *argtypes)
270
 
{
271
 
        if (info->alt)
272
 
        {
273
 
                if (n > 1)
274
 
                {
275
 
                        argtypes[0] = PA_POINTER;
276
 
                        argtypes[1] = PA_POINTER;
277
 
                }
278
 
                return 2;
279
 
        }
280
 
        
281
 
        if (n > 0)
282
 
        {
283
 
                argtypes[0] = PA_POINTER;
284
 
        }
285
 
        return 1;
 
247
        return print_in_hook(dst, len, "%d %s%s", delta, unit, (delta == 1)? "":"s");
286
248
}
287
249
 
288
250
/**
293
255
static char hexdig_upper[] = "0123456789ABCDEF";
294
256
 
295
257
/**
296
 
 * output handler in printf() for mem ranges
 
258
 * Described in header.
297
259
 */
298
 
static int mem_print(FILE *stream, const struct printf_info *info,
299
 
                                         const void *const *args)
 
260
int mem_printf_hook(char *dst, size_t dstlen,
 
261
                                        printf_hook_spec_t *spec, const void *const *args)
300
262
{
301
263
        char *bytes = *((void**)(args[0]));
302
264
        int len = *((size_t*)(args[1]));
310
272
        int i = 0;
311
273
        int written = 0;
312
274
        
313
 
        written += fprintf(stream, "=> %d bytes @ %p", len, bytes);
 
275
        written += print_in_hook(dst, dstlen, "=> %d bytes @ %p", len, bytes);
314
276
        
315
277
        while (bytes_pos < bytes_roof)
316
278
        {
323
285
                if (++bytes_pos == bytes_roof || i == BYTES_PER_LINE) 
324
286
                {
325
287
                        int padding = 3 * (BYTES_PER_LINE - i);
326
 
                        int written;
327
288
                        
328
289
                        while (padding--)
329
290
                        {
332
293
                        *buffer_pos++ = '\0';
333
294
                        ascii_buffer[i] = '\0';
334
295
                        
335
 
                        written += fprintf(stream, "\n%4d: %s  %s",
336
 
                                                           line_start, buffer, ascii_buffer);
337
 
 
 
296
                        written += print_in_hook(dst, dstlen, "\n%4d: %s  %s",
 
297
                                                                     line_start, buffer, ascii_buffer);
338
298
                        
339
299
                        buffer_pos = buffer;
340
300
                        line_start += BYTES_PER_LINE;
347
307
        }
348
308
        return written;
349
309
}
350
 
 
351
 
/**
352
 
 * arginfo handler for printf() mem ranges
353
 
 */
354
 
int mem_arginfo(const struct printf_info *info, size_t n, int *argtypes)
355
 
{
356
 
        if (n > 1)
357
 
        {
358
 
                argtypes[0] = PA_POINTER;
359
 
                argtypes[1] = PA_INT;
360
 
        }
361
 
        return 2;
362
 
}
363
 
 
364
 
/**
365
 
 * return printf hook functions for a time
366
 
 */
367
 
printf_hook_functions_t time_get_printf_hooks()
368
 
{
369
 
        printf_hook_functions_t hooks = {time_print, time_arginfo};
370
 
        
371
 
        return hooks;
372
 
}
373
 
 
374
 
/**
375
 
 * return printf hook functions for a time delta
376
 
 */
377
 
printf_hook_functions_t time_delta_get_printf_hooks()
378
 
{
379
 
        printf_hook_functions_t hooks = {time_delta_print, time_delta_arginfo};
380
 
        
381
 
        return hooks;
382
 
}
383
 
 
384
 
/**
385
 
 * return printf hook functions for mem ranges
386
 
 */
387
 
printf_hook_functions_t mem_get_printf_hooks()
388
 
{
389
 
        printf_hook_functions_t hooks = {mem_print, mem_arginfo};
390
 
        
391
 
        return hooks;
392
 
}
393