]>
git.saurik.com Git - apple/ipsec.git/blob - ipsec-tools/racoon/backupsa.c
1 /* $KAME: backupsa.c,v 1.16 2001/12/31 20:13:40 thorpej Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #ifndef HAVE_NETINET6_IPSEC
45 #include <netinet/ipsec.h>
47 #include <netinet6/ipsec.h>
50 #if TIME_WITH_SYS_TIME
51 # include <sys/time.h>
55 # include <sys/time.h>
68 #include "localconf.h"
75 * (time string)%(sa parameter)
76 * (time string) := ex. Nov 24 18:22:48 1986
78 * src dst satype spi mode reqid wsize \
79 * e_type e_keylen a_type a_keylen flags \
80 * l_alloc l_bytes l_addtime l_usetime seq keymat
82 static char *format
= "%b %d %T %Y"; /* time format */
83 static char *strmon
[12] = {
84 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
85 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
88 static char *str2tmx
__P((char *, struct tm
*));
89 static int str2num
__P((char *, int));
92 * output the sa parameter.
95 backupsa_to_file(satype
, mode
, src
, dst
, spi
, reqid
, wsize
,
96 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
97 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)
98 u_int satype
, mode
, wsize
;
99 struct sockaddr
*src
, *dst
;
100 u_int32_t spi
, reqid
;
102 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
104 u_int64_t l_bytes
, l_addtime
, l_usetime
;
119 l
= strftime(p
, len
, format
, tm
);
125 l
= snprintf(p
, len
, "%%");
126 if (l
< 0 || l
>= len
)
133 i
= getnameinfo(src
, sysdep_sa_len(src
), p
, len
, NULL
, 0, NIFLAGS
);
142 l
= snprintf(p
, len
, " ");
143 if (l
< 0 || l
>= len
)
150 i
= getnameinfo(dst
, sysdep_sa_len(dst
), p
, len
, NULL
, 0, NIFLAGS
);
162 "%u %llu %llu %llu %u",
163 satype
, (unsigned long)ntohl(spi
), mode
, reqid
, wsize
,
164 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
165 l_alloc
, (unsigned long long)l_bytes
,
166 (unsigned long long)l_addtime
, (unsigned long long)l_usetime
,
168 if (l
< 0 || l
>= len
)
175 k
= val2str(keymat
, e_keylen
+ a_keylen
);
176 l
= snprintf(p
, len
, " %s", k
);
177 if (l
< 0 || l
>= len
)
185 /* open the file and write the SA parameter */
186 if (safefile(lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
], 1) != 0 ||
187 (fp
= fopen(lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
], "a")) == NULL
) {
188 plog(LLV_ERROR
, LOCATION
, NULL
,
189 "failed to open the backup file %s.\n",
190 lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
]);
193 fprintf(fp
, "%s\n", buf
);
199 plog(LLV_ERROR
, LOCATION
, NULL
,
200 "SA cannot be saved to a file.\n");
210 time_t created
, current
;
213 struct sockaddr
*src
, *dst
;
214 u_int32_t spi
, reqid
;
217 u_int wsize
, e_type
, e_keylen
, a_type
, a_keylen
, flags
;
219 u_int64_t l_bytes
, l_addtime
, l_usetime
;
223 if (safefile(lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
], 1) == 0)
224 fp
= fopen(lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
], "r");
228 plog(LLV_ERROR
, LOCATION
, NULL
,
229 "failed to open the backup file %s.\n",
230 lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
]);
234 current
= time(NULL
);
236 for(line
= 1; fgets(buf
, sizeof(buf
), fp
) != NULL
; line
++) {
241 memset(&tm
, 0, sizeof(tm
));
242 p
= str2tmx(buf
, &tm
);
245 plog(LLV_ERROR
, LOCATION
, NULL
,
246 "illegal format line#%d in %s: %s\n",
247 line
, lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
], buf
);
250 created
= mktime(&tm
);
253 for (q
= p
; *q
!= '\0' && !isspace((int)*q
); q
++)
256 src
= str2saddr(p
, NULL
);
261 for (q
= p
; *q
!= '\0' && !isspace((int)*q
); q
++)
264 dst
= str2saddr(p
, NULL
);
271 #define GETNEXTNUM(value, function) \
274 for (q = p; *q != '\0' && !isspace((int)*q); q++) \
277 (value) = function(p, &y, 10); \
278 if ((value) == 0 && *y != '\0') \
283 GETNEXTNUM(satype
, strtoul
);
284 GETNEXTNUM(spi
, strtoul
);
286 GETNEXTNUM(mode
, strtoul
);
287 GETNEXTNUM(reqid
, strtoul
);
288 GETNEXTNUM(wsize
, strtoul
);
289 GETNEXTNUM(e_type
, strtoul
);
290 GETNEXTNUM(e_keylen
, strtoul
);
291 GETNEXTNUM(a_type
, strtoul
);
292 GETNEXTNUM(a_keylen
, strtoul
);
293 GETNEXTNUM(flags
, strtoul
);
294 GETNEXTNUM(l_alloc
, strtoul
);
295 GETNEXTNUM(l_bytes
, strtouq
);
296 GETNEXTNUM(l_addtime
, strtouq
);
297 GETNEXTNUM(l_usetime
, strtouq
);
298 GETNEXTNUM(seq
, strtoul
);
302 keymat
= str2val(p
, 16, &keymatlen
);
303 if (keymat
== NULL
) {
304 plog(LLV_ERROR
, LOCATION
, NULL
,
305 "illegal format(keymat) line#%d in %s: %s\n",
306 line
, lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
], buf
);
312 if (created
+ l_addtime
< current
) {
313 plog(LLV_DEBUG
, LOCATION
, NULL
,
314 "ignore this line#%d in %s due to expiration\n",
315 line
, lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
]);
321 l_addtime
-= current
- created
;
333 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
334 0, l_bytes
, l_addtime
, 0, seq
, 0) < 0) {
335 plog(LLV_ERROR
, LOCATION
, NULL
,
336 "restore SA filed line#%d in %s: %s\n",
337 line
, lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
], ipsec_strerror());
350 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
351 0, l_bytes
, l_addtime
, 0, seq
) < 0) {
352 plog(LLV_ERROR
, LOCATION
, NULL
,
353 "restore SA filed line#%d in %s: %s\n",
354 line
, lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
], ipsec_strerror());
365 * There is a possibility that an abnormal system down will happen
366 * again before new negotiation will be started. so racoon clears
367 * the backup file here. it's ok that old SAs are remained in the
368 * file. any old SA will not be installed because racoon checks the
369 * lifetime and compare with current time.
380 /* simply return if the file is not defined. */
381 if (!lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
])
384 fp
= fopen(lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
], "w+");
386 plog(LLV_ERROR
, LOCATION
, NULL
,
387 "failed to clean the backup file %s.\n",
388 lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
]);
396 * convert fixed string into the tm structure.
397 * The fixed string is like 'Nov 24 18:22:48 1986'.
398 * static char *format = "%b %d %T %Y";
401 str2tmx(char *p
, struct tm
*tm
)
406 for (i
= 0; i
< sizeof(strmon
)/sizeof(strmon
[0]); i
++) {
407 if (strncasecmp(p
, strmon
[i
], strlen(strmon
[i
])) == 0) {
412 if (i
== sizeof(strmon
)/sizeof(strmon
[0]))
414 p
+= strlen(strmon
[i
]);
420 tm
->tm_mday
= str2num(p
, len
);
421 if (tm
->tm_mday
== -1 || tm
->tm_mday
> 31)
429 tm
->tm_hour
= str2num(p
, len
);
430 if (tm
->tm_hour
== -1 || tm
->tm_hour
> 24)
438 tm
->tm_min
= str2num(p
, len
);
439 if (tm
->tm_min
== -1 || tm
->tm_min
> 60)
447 tm
->tm_sec
= str2num(p
, len
);
448 if (tm
->tm_sec
== -1 || tm
->tm_sec
> 60)
456 tm
->tm_year
= str2num(p
, len
);
457 if (tm
->tm_year
== -1 || tm
->tm_year
< 1900)
473 for (i
= len
; i
> 0; i
--) {
474 if (!isdigit((int)*p
))
491 char *buf
= "Nov 24 18:22:48 1986 ";
494 memset(&tm
, 0, sizeof(tm
));
495 p
= str2tmx(buf
, &tm
);
496 printf("[%x]\n", *p
);
499 printf("mktime failed.");