404194b8192ceea18d958a5cb5ccc3d2d86a1212
[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 <time.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22
23 #include <freeswan.h>
24
25 #include "constants.h"
26 #include "defs.h"
27 #include "log.h"
28 #include "whack.h" /* for RC_LOG_SERIOUS */
29
30 bool
31 all_zero(const unsigned char *m, size_t len)
32 {
33 size_t i;
34
35 for (i = 0; i != len; i++)
36 if (m[i] != '\0')
37 return FALSE;
38 return TRUE;
39 }
40
41 /* Note that there may be as many as six IDs that are temporary at
42 * one time before unsharing the two ends of a connection. So we need
43 * at least six temporary buffers for DER_ASN1_DN IDs.
44 * We rotate them. Be careful!
45 */
46 #define MAX_BUF 10
47
48 char*
49 temporary_cyclic_buffer(void)
50 {
51 static char buf[MAX_BUF][BUF_LEN]; /* MAX_BUF internal buffers */
52 static int counter = 0; /* cyclic counter */
53
54 if (++counter == MAX_BUF) counter = 0; /* next internal buffer */
55 return buf[counter]; /* assign temporary buffer */
56 }
57
58 /* concatenates two sub paths into a string with a maximum size of BUF_LEN
59 * use for temporary storage only
60 */
61 char* concatenate_paths(char *a, char *b)
62 {
63 char *c;
64
65 if (*b == '/' || *b == '.')
66 return b;
67
68 c = temporary_cyclic_buffer();
69 snprintf(c, BUF_LEN, "%s/%s", a, b);
70 return c;
71 }
72
73 /* moves a chunk to a memory position, chunk is freed afterwards
74 * position pointer is advanced after the insertion point
75 */
76 void
77 mv_chunk(u_char **pos, chunk_t content)
78 {
79 if (content.len > 0)
80 {
81 chunkcpy(*pos, content);
82 free(content.ptr);
83 }
84 }
85
86 /* checks if the expiration date has been reached and
87 * warns during the warning_interval of the imminent
88 * expiry. strict=TRUE declares a fatal error,
89 * strict=FALSE issues a warning upon expiry.
90 */
91 const char*
92 check_expiry(time_t expiration_date, int warning_interval, bool strict)
93 {
94 time_t now, time_left;
95
96 if (expiration_date == UNDEFINED_TIME)
97 return "ok (expires never)";
98
99 /* determine the current time */
100 time(&now);
101
102 time_left = (expiration_date - now);
103 if (time_left < 0)
104 return strict? "fatal (expired)" : "warning (expired)";
105
106 if (time_left > 86400*warning_interval)
107 return "ok";
108 {
109 static char buf[35]; /* temporary storage */
110 const char* unit = "second";
111
112 if (time_left > 172800)
113 {
114 time_left /= 86400;
115 unit = "day";
116 }
117 else if (time_left > 7200)
118 {
119 time_left /= 3600;
120 unit = "hour";
121 }
122 else if (time_left > 120)
123 {
124 time_left /= 60;
125 unit = "minute";
126 }
127 snprintf(buf, 35, "warning (expires in %d %s%s)", time_left,
128 unit, (time_left == 1)?"":"s");
129 return buf;
130 }
131 }
132
133
134 /*
135 * Filter eliminating the directory entries '.' and '..'
136 */
137 int
138 file_select(const struct dirent *entry)
139 {
140 return strcmp(entry->d_name, "." ) &&
141 strcmp(entry->d_name, "..");
142 }
143
144