]>
git.saurik.com Git - apple/network_cmds.git/blob - racoon.tproj/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
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #ifdef IPV6_INRIA_VERSION
43 #include <netinet/ipsec.h>
45 #include <netinet6/ipsec.h>
48 #if TIME_WITH_SYS_TIME
49 # include <sys/time.h>
53 # include <sys/time.h>
66 #include "localconf.h"
73 * (time string)%(sa parameter)
74 * (time string) := ex. Nov 24 18:22:48 1986
76 * src dst satype spi mode reqid wsize \
77 * e_type e_keylen a_type a_keylen flags \
78 * l_alloc l_bytes l_addtime l_usetime seq keymat
80 static char *format
= "%b %d %T %Y"; /* time format */
81 static char *strmon
[12] = {
82 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
83 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
86 static char *str2tmx
__P((char *, struct tm
*));
87 static int str2num
__P((char *, int));
90 * output the sa parameter.
93 backupsa_to_file(satype
, mode
, src
, dst
, spi
, reqid
, wsize
,
94 keymat
, e_type
, e_keylen
, a_type
, a_keylen
, flags
,
95 l_alloc
, l_bytes
, l_addtime
, l_usetime
, seq
)
96 u_int satype
, mode
, wsize
;
97 struct sockaddr
*src
, *dst
;
100 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
102 u_int64_t l_bytes
, l_addtime
, l_usetime
;
117 l
= strftime(p
, len
, format
, tm
);
123 l
= snprintf(p
, len
, "%%");
124 if (l
< 0 || l
>= len
)
131 i
= getnameinfo(src
, src
->sa_len
, p
, len
, NULL
, 0, NIFLAGS
);
140 l
= snprintf(p
, len
, " ");
141 if (l
< 0 || l
>= len
)
148 i
= getnameinfo(dst
, dst
->sa_len
, p
, len
, NULL
, 0, NIFLAGS
);
160 "%u %llu %llu %llu %u",
161 satype
, (unsigned long)ntohl(spi
), mode
, reqid
, wsize
,
162 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
163 l_alloc
, (unsigned long long)l_bytes
,
164 (unsigned long long)l_addtime
, (unsigned long long)l_usetime
,
166 if (l
< 0 || l
>= len
)
173 k
= val2str(keymat
, e_keylen
+ a_keylen
);
174 l
= snprintf(p
, len
, " %s", k
);
175 if (l
< 0 || l
>= len
)
183 /* open the file and write the SA parameter */
184 if (safefile(lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
], 1) != 0 ||
185 (fp
= fopen(lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
], "a")) == NULL
) {
186 plog(LLV_ERROR
, LOCATION
, NULL
,
187 "failed to open the backup file %s.\n",
188 lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
]);
191 fprintf(fp
, "%s\n", buf
);
197 plog(LLV_ERROR
, LOCATION
, NULL
,
198 "SA cannot be saved to a file.\n");
208 time_t created
, current
;
211 struct sockaddr
*src
, *dst
;
212 u_int32_t spi
, reqid
;
215 u_int wsize
, e_type
, e_keylen
, a_type
, a_keylen
, flags
;
217 u_int64_t l_bytes
, l_addtime
, l_usetime
;
221 if (safefile(lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
], 1) == 0)
222 fp
= fopen(lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
], "r");
226 plog(LLV_ERROR
, LOCATION
, NULL
,
227 "failed to open the backup file %s.\n",
228 lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
]);
232 current
= time(NULL
);
234 for(line
= 1; fgets(buf
, sizeof(buf
), fp
) != NULL
; line
++) {
239 memset(&tm
, 0, sizeof(tm
));
240 p
= str2tmx(buf
, &tm
);
243 plog(LLV_ERROR
, LOCATION
, NULL
,
244 "illegal format line#%d in %s: %s\n",
245 line
, lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
], buf
);
248 created
= mktime(&tm
);
251 for (q
= p
; *q
!= '\0' && !isspace(*q
); q
++)
254 src
= str2saddr(p
, NULL
);
259 for (q
= p
; *q
!= '\0' && !isspace(*q
); q
++)
262 dst
= str2saddr(p
, NULL
);
269 #define GETNEXTNUM(value, function) \
272 for (q = p; *q != '\0' && !isspace(*q); q++) \
275 (value) = function(p, &y, 10); \
276 if ((value) == 0 && *y != '\0') \
281 GETNEXTNUM(satype
, strtoul
);
282 GETNEXTNUM(spi
, strtoul
);
284 GETNEXTNUM(mode
, strtoul
);
285 GETNEXTNUM(reqid
, strtoul
);
286 GETNEXTNUM(wsize
, strtoul
);
287 GETNEXTNUM(e_type
, strtoul
);
288 GETNEXTNUM(e_keylen
, strtoul
);
289 GETNEXTNUM(a_type
, strtoul
);
290 GETNEXTNUM(a_keylen
, strtoul
);
291 GETNEXTNUM(flags
, strtoul
);
292 GETNEXTNUM(l_alloc
, strtoul
);
293 GETNEXTNUM(l_bytes
, strtouq
);
294 GETNEXTNUM(l_addtime
, strtouq
);
295 GETNEXTNUM(l_usetime
, strtouq
);
296 GETNEXTNUM(seq
, strtoul
);
300 keymat
= str2val(p
, 16, &keymatlen
);
301 if (keymat
== NULL
) {
302 plog(LLV_ERROR
, LOCATION
, NULL
,
303 "illegal format(keymat) line#%d in %s: %s\n",
304 line
, lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
], buf
);
310 if (created
+ l_addtime
< current
) {
311 plog(LLV_DEBUG
, LOCATION
, NULL
,
312 "ignore this line#%d in %s due to expiration\n",
313 line
, lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
]);
319 l_addtime
-= current
- created
;
331 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
332 0, l_bytes
, l_addtime
, 0, seq
, 0) < 0) {
333 plog(LLV_ERROR
, LOCATION
, NULL
,
334 "restore SA filed line#%d in %s: %s\n",
335 line
, lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
], ipsec_strerror());
345 * There is a possibility that an abnormal system down will happen
346 * again before new negotiation will be started. so racoon clears
347 * the backup file here. it's ok that old SAs are remained in the
348 * file. any old SA will not be installed because racoon checks the
349 * lifetime and compare with current time.
360 /* simply return if the file is not defined. */
361 if (!lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
])
364 fp
= fopen(lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
], "w+");
366 plog(LLV_ERROR
, LOCATION
, NULL
,
367 "failed to clean the backup file %s.\n",
368 lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
]);
376 * convert fixed string into the tm structure.
377 * The fixed string is like 'Nov 24 18:22:48 1986'.
378 * static char *format = "%b %d %T %Y";
381 str2tmx(char *p
, struct tm
*tm
)
386 for (i
= 0; i
< sizeof(strmon
)/sizeof(strmon
[0]); i
++) {
387 if (strncasecmp(p
, strmon
[i
], strlen(strmon
[i
])) == 0) {
392 if (i
== sizeof(strmon
)/sizeof(strmon
[0]))
394 p
+= strlen(strmon
[i
]);
400 tm
->tm_mday
= str2num(p
, len
);
401 if (tm
->tm_mday
== -1 || tm
->tm_mday
> 31)
409 tm
->tm_hour
= str2num(p
, len
);
410 if (tm
->tm_hour
== -1 || tm
->tm_hour
> 24)
418 tm
->tm_min
= str2num(p
, len
);
419 if (tm
->tm_min
== -1 || tm
->tm_min
> 60)
427 tm
->tm_sec
= str2num(p
, len
);
428 if (tm
->tm_sec
== -1 || tm
->tm_sec
> 60)
436 tm
->tm_year
= str2num(p
, len
);
437 if (tm
->tm_year
== -1 || tm
->tm_year
< 1900)
453 for (i
= len
; i
> 0; i
--) {
471 char *buf
= "Nov 24 18:22:48 1986 ";
474 memset(&tm
, 0, sizeof(tm
));
475 p
= str2tmx(buf
, &tm
);
476 printf("[%x]\n", *p
);
479 printf("mktime failed.");