pluto: Print expiry time more properly.
[strongswan.git] / src / pluto / defs.c
1 /* misc. universal things
2 * Copyright (C) 1998-2001 D. Hugh Redelmeier.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 */
14
15 #include <stdlib.h>
16 #include <string.h>
17 #include <stdio.h>
18 #include <dirent.h>
19 #include <inttypes.h>
20 #include <time.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23
24 #include <freeswan.h>
25
26 #include "constants.h"
27 #include "defs.h"
28 #include "log.h"
29 #include "whack.h" /* for RC_LOG_SERIOUS */
30
31 bool
32 all_zero(const unsigned char *m, size_t len)
33 {
34 size_t i;
35
36 for (i = 0; i != len; i++)
37 if (m[i] != '\0')
38 return FALSE;
39 return TRUE;
40 }
41
42 /* Note that there may be as many as six IDs that are temporary at
43 * one time before unsharing the two ends of a connection. So we need
44 * at least six temporary buffers for DER_ASN1_DN IDs.
45 * We rotate them. Be careful!
46 */
47 #define MAX_BUF 10
48
49 char*
50 temporary_cyclic_buffer(void)
51 {
52 static char buf[MAX_BUF][BUF_LEN]; /* MAX_BUF internal buffers */
53 static int counter = 0; /* cyclic counter */
54
55 if (++counter == MAX_BUF) counter = 0; /* next internal buffer */
56 return buf[counter]; /* assign temporary buffer */
57 }
58
59 /* concatenates two sub paths into a string with a maximum size of BUF_LEN
60 * use for temporary storage only
61 */
62 char* concatenate_paths(char *a, char *b)
63 {
64 char *c;
65
66 if (*b == '/' || *b == '.')
67 return b;
68
69 c = temporary_cyclic_buffer();
70 snprintf(c, BUF_LEN, "%s/%s", a, b);
71 return c;
72 }
73
74 /* moves a chunk to a memory position, chunk is freed afterwards
75 * position pointer is advanced after the insertion point
76 */
77 void
78 mv_chunk(u_char **pos, chunk_t content)
79 {
80 if (content.len > 0)
81 {
82 chunkcpy(*pos, content);
83 free(content.ptr);
84 }
85 }
86
87 /* checks if the expiration date has been reached and
88 * warns during the warning_interval of the imminent
89 * expiry. strict=TRUE declares a fatal error,
90 * strict=FALSE issues a warning upon expiry.
91 */
92 const char*
93 check_expiry(time_t expiration_date, int warning_interval, bool strict)
94 {
95 time_t now, time_left;
96
97 if (expiration_date == UNDEFINED_TIME)
98 return "ok (expires never)";
99
100 /* determine the current time */
101 time(&now);
102
103 time_left = (expiration_date - now);
104 if (time_left < 0)
105 return strict? "fatal (expired)" : "warning (expired)";
106
107 if (time_left > 86400*warning_interval)
108 return "ok";
109 {
110 static char buf[35]; /* temporary storage */
111 const char* unit = "second";
112
113 if (time_left > 172800)
114 {
115 time_left /= 86400;
116 unit = "day";
117 }
118 else if (time_left > 7200)
119 {
120 time_left /= 3600;
121 unit = "hour";
122 }
123 else if (time_left > 120)
124 {
125 time_left /= 60;
126 unit = "minute";
127 }
128 snprintf(buf, 35, "warning (expires in %" PRIu64 " %s%s)",
129 (u_int64_t)time_left, unit, (time_left == 1) ? "" : "s");
130 return buf;
131 }
132 }
133
134
135 /*
136 * Filter eliminating the directory entries '.' and '..'
137 */
138 int
139 file_select(const struct dirent *entry)
140 {
141 return strcmp(entry->d_name, "." ) &&
142 strcmp(entry->d_name, "..");
143 }
144
145