~ubuntu-branches/ubuntu/hardy/openswan/hardy-updates

« back to all changes in this revision

Viewing changes to programs/pluto/defs.c

  • Committer: Bazaar Package Importer
  • Author(s): Rene Mayrhofer
  • Date: 2005-01-27 16:10:11 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050127161011-idgybmyz3vwhpfiq
Tags: 2.3.0-2
Urgency HIGH due to security issue and problems with build-deps in sarge.
* Fix the security issue. Please see
  http://www.idefense.com/application/poi/display?id=190&
      type=vulnerabilities&flashstatus=false
  for more details. Thanks to Martin Schulze for informing me about
  this issue.
  Closes: #292458: Openswan XAUTH/PAM Buffer Overflow Vulnerability
* Added a Build-Dependency to lynx.
  Closes: #291143: openswan: FTBFS: Missing build dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12
12
 * for more details.
13
13
 *
14
 
 * RCSID $Id: defs.c,v 1.24.6.1 2004/03/21 05:23:32 mcr Exp $
 
14
 * RCSID $Id: defs.c,v 1.30 2004/06/27 22:32:45 mcr Exp $
15
15
 */
16
16
 
17
17
#include <stdlib.h>
24
24
#include <openswan.h>
25
25
 
26
26
#include "constants.h"
 
27
#include "openswan/ipsec_policy.h"
27
28
#include "defs.h"
28
29
#include "log.h"
29
30
#include "whack.h"      /* for RC_LOG_SERIOUS */
30
31
 
31
 
const chunk_t empty_chunk = { NULL, 0 };
32
 
 
33
 
bool
34
 
all_zero(const unsigned char *m, size_t len)
35
 
{
36
 
    size_t i;
37
 
 
38
 
    for (i = 0; i != len; i++)
39
 
        if (m[i] != '\0')
40
 
            return FALSE;
41
 
    return TRUE;
42
 
}
43
 
 
44
32
/* Convert MP_INT to network form (binary octets, big-endian).
45
33
 * We do the malloc; caller must eventually do free.
46
34
 */
89
77
    }
90
78
}
91
79
 
92
 
 
93
 
/* memory allocation
94
 
 *
95
 
 * LEAK_DETECTIVE puts a wrapper around each allocation and maintains
96
 
 * a list of live ones.  If a dead one is freed, an assertion MIGHT fail.
97
 
 * If the live list is currupted, that will often be detected.
98
 
 * In the end, report_leaks() is called, and the names of remaining
99
 
 * live allocations are printed.  At the moment, it is hoped, not that
100
 
 * the list is empty, but that there will be no surprises.
101
 
 *
102
 
 * Accepted Leaks:
103
 
 * - "struct iface" and "device name" (for "discovered" net interfaces)
104
 
 * - "struct event in event_schedule()" (events not associated with states)
105
 
 * - "Pluto lock name" (one only, needed until end -- why bother?)
106
 
 */
107
 
 
108
 
#ifdef LEAK_DETECTIVE
109
 
 
110
 
/* this magic number is 3671129837 decimal (623837458 complemented) */
111
 
#define LEAK_MAGIC 0xDAD0FEEDul
112
 
 
113
 
union mhdr {
114
 
    struct {
115
 
        const char *name;
116
 
        union mhdr *older, *newer;
117
 
        unsigned long magic;
118
 
    } i;    /* info */
119
 
    unsigned long junk; /* force maximal alignment */
120
 
};
121
 
 
122
 
static union mhdr *allocs = NULL;
123
 
 
124
 
void *alloc_bytes(size_t size, const char *name)
125
 
{
126
 
    union mhdr *p = malloc(sizeof(union mhdr) + size);
127
 
 
128
 
    if (p == NULL)
129
 
        exit_log("unable to malloc %lu bytes for %s"
130
 
            , (unsigned long) size, name);
131
 
    p->i.name = name;
132
 
    p->i.older = allocs;
133
 
    if (allocs != NULL)
134
 
        allocs->i.newer = p;
135
 
    allocs = p;
136
 
    p->i.newer = NULL;
137
 
    p->i.magic = LEAK_MAGIC;
138
 
 
139
 
    memset(p+1, '\0', size);
140
 
    return p+1;
141
 
}
142
 
 
143
 
void *
144
 
clone_bytes(const void *orig, size_t size, const char *name)
145
 
{
146
 
    void *p = alloc_bytes(size, name);
147
 
 
148
 
    memcpy(p, orig, size);
149
 
    return p;
150
 
}
151
 
 
152
 
void
153
 
pfree(void *ptr)
154
 
{
155
 
    union mhdr *p;
156
 
 
157
 
    passert(ptr != NULL);
158
 
    p = ((union mhdr *)ptr) - 1;
159
 
    passert(p->i.magic == LEAK_MAGIC);
160
 
    if (p->i.older != NULL)
161
 
    {
162
 
        passert(p->i.older->i.newer == p);
163
 
        p->i.older->i.newer = p->i.newer;
164
 
    }
165
 
    if (p->i.newer == NULL)
166
 
    {
167
 
        passert(p == allocs);
168
 
        allocs = p->i.older;
169
 
    }
170
 
    else
171
 
    {
172
 
        passert(p->i.newer->i.older == p);
173
 
        p->i.newer->i.older = p->i.older;
174
 
    }
175
 
    p->i.magic = ~LEAK_MAGIC;
176
 
    free(p);
177
 
}
178
 
 
179
 
void
180
 
report_leaks(void)
181
 
{
182
 
    union mhdr
183
 
        *p = allocs,
184
 
        *pprev = NULL;
185
 
    unsigned long n = 0;
186
 
 
187
 
    while (p != NULL)
188
 
    {
189
 
        passert(p->i.magic == LEAK_MAGIC);
190
 
        passert(pprev == p->i.newer);
191
 
        pprev = p;
192
 
        p = p->i.older;
193
 
        n++;
194
 
        if (p == NULL || pprev->i.name != p->i.name)
195
 
        {
196
 
            if (n != 1)
197
 
                plog("leak: %lu * %s", n, pprev->i.name);
198
 
            else
199
 
                plog("leak: %s", pprev->i.name);
200
 
            n = 0;
201
 
        }
202
 
    }
203
 
}
204
 
 
205
 
#else /* !LEAK_DETECTIVE */
206
 
 
207
 
void *alloc_bytes(size_t size, const char *name)
208
 
{
209
 
    void *p = malloc(size);
210
 
 
211
 
    if (p == NULL)
212
 
        exit_log("unable to malloc %lu bytes for %s"
213
 
            , (unsigned long) size, name);
214
 
    memset(p, '\0', size);
215
 
    return p;
216
 
}
217
 
 
218
 
void *clone_bytes(const void *orig, size_t size, const char *name)
219
 
{
220
 
    void *p = malloc(size);
221
 
 
222
 
    if (p == NULL)
223
 
        exit_log("unable to malloc %lu bytes for %s"
224
 
            , (unsigned long) size, name);
225
 
    memcpy(p, orig, size);
226
 
    return p;
227
 
}
228
 
#endif /* !LEAK_DETECTIVE */
229
 
 
230
 
 
231
80
/* Names of the months */
232
81
 
233
82
static const char* months[] = {
239
88
/*
240
89
 *  Display a date either in local or UTC time
241
90
 */
242
 
char*
243
 
timetoa(const time_t *time, bool utc)
 
91
char *
 
92
timetoa(const time_t *time, bool utc, char *b, size_t blen)
244
93
{
245
 
    static char buf[TIMETOA_BUF];
246
 
 
247
94
    if (*time == UNDEFINED_TIME)
248
 
        sprintf(buf, "--- -- --:--:--%s----", (utc)?" UTC ":" ");
 
95
        snprintf(b, blen, "--- -- --:--:--%s----", (utc)?" UTC ":" ");
249
96
    else
250
97
    {
251
98
        struct tm *t = (utc)? gmtime(time) : localtime(time);
252
99
 
253
 
        sprintf(buf, "%s %02d %02d:%02d:%02d%s%04d",
 
100
        snprintf(b, blen, "%s %02d %02d:%02d:%02d%s%04d",
254
101
            months[t->tm_mon], t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec,
255
102
            (utc)?" UTC ":" ", t->tm_year + 1900
256
103
        );
257
104
    }
258
 
    return buf;
 
105
    return b;
259
106
}
260
107
 
261
108
/*  checks if the expiration date has been reached and
285
132
        static char buf[35]; /* temporary storage */
286
133
        const char* unit = "second";
287
134
 
288
 
        if (time_left > 86400)
 
135
        if (time_left > 172800)
289
136
        {
290
137
            time_left /= 86400;
291
138
            unit = "day";
292
139
        }
293
 
        else if (time_left > 3600)
 
140
        else if (time_left > 7200)
294
141
        {
295
142
            time_left /= 3600;
296
143
            unit = "hour";
297
144
        }
298
 
        else if (time_left > 60)
 
145
        else if (time_left > 120)
299
146
        {
300
147
            time_left /= 60;
301
148
            unit = "minute";
327
174
 * End:
328
175
 */
329
176
 
 
177
/*  compare two chunks, returns zero if a equals b
 
178
 *  negative/positive if a is earlier/later in the alphabet than b
 
179
 */
 
180
bool
 
181
cmp_chunk(chunk_t a, chunk_t b)
 
182
{
 
183
    int cmp_len, len, cmp_value;
 
184
    
 
185
    cmp_len = a.len - b.len;
 
186
    len = (cmp_len < 0)? a.len : b.len;
 
187
    cmp_value = memcmp(a.ptr, b.ptr, len);
 
188
 
 
189
    return (cmp_value == 0)? cmp_len : cmp_value;
 
190
};
330
191