da8c4331c1db8186fe936306a4c550997a334246
[tinc] / src / utils.c
1 /*
2     utils.c -- gathering of some stupid small functions
3     Copyright (C) 1999-2005 Ivo Timmermans
4                   2000-2013 Guus Sliepen <guus@tinc-vpn.org>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "logger.h"
22 #include "system.h"
23 #include "utils.h"
24 #include "xalloc.h"
25
26 static const char hexadecimals[] = "0123456789ABCDEF";
27 static const char base64_original[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
28 static const char base64_urlsafe[] =  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
29 static const char base64_decode[256] = {
30         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
31         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
32         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63,
33         52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
34         -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
35         15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63,
36         -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
37         41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
38         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
39         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
40         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
41         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
42         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
43         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
44         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
45         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
46 };
47
48 static int charhex2bin(char c) {
49         if(isdigit(c)) {
50                 return c - '0';
51         } else {
52                 return toupper(c) - 'A' + 10;
53         }
54 }
55
56 int hex2bin(const char *src, void *vdst, int length) {
57         char *dst = vdst;
58         int i;
59
60         for(i = 0; i < length && isxdigit(src[i * 2]) && isxdigit(src[i * 2 + 1]); i++) {
61                 dst[i] = charhex2bin(src[i * 2]) * 16 + charhex2bin(src[i * 2 + 1]);
62         }
63
64         return i;
65 }
66
67 int bin2hex(const void *vsrc, char *dst, int length) {
68         const char *src = vsrc;
69
70         for(int i = length - 1; i >= 0; i--) {
71                 dst[i * 2 + 1] = hexadecimals[(unsigned char) src[i] & 15];
72                 dst[i * 2] = hexadecimals[(unsigned char) src[i] >> 4];
73         }
74
75         dst[length * 2] = 0;
76         return length * 2;
77 }
78
79 int b64decode(const char *src, void *dst, int length) {
80         int i;
81         uint32_t triplet = 0;
82         unsigned char *udst = (unsigned char *)dst;
83
84         for(i = 0; i < length && src[i]; i++) {
85                 triplet |= base64_decode[src[i] & 0xff] << (6 * (i & 3));
86
87                 if((i & 3) == 3) {
88                         if(triplet & 0xff000000U) {
89                                 return 0;
90                         }
91
92                         udst[0] = triplet & 0xff;
93                         triplet >>= 8;
94                         udst[1] = triplet & 0xff;
95                         triplet >>= 8;
96                         udst[2] = triplet;
97                         triplet = 0;
98                         udst += 3;
99                 }
100         }
101
102         if(triplet & 0xff000000U) {
103                 return 0;
104         }
105
106         if((i & 3) == 3) {
107                 udst[0] = triplet & 0xff;
108                 triplet >>= 8;
109                 udst[1] = triplet & 0xff;
110                 return i / 4 * 3 + 2;
111         } else if((i & 3) == 2) {
112                 udst[0] = triplet & 0xff;
113                 return i / 4 * 3 + 1;
114         } else {
115                 return i / 4 * 3;
116         }
117 }
118
119 static int b64encode_internal(const void *src, char *dst, int length, const char *alphabet) {
120         uint32_t triplet;
121         const unsigned char *usrc = (unsigned char *)src;
122         int si = length / 3 * 3;
123         int di = length / 3 * 4;
124
125         switch(length % 3) {
126         case 2:
127                 triplet = usrc[si] | usrc[si + 1] << 8;
128                 dst[di] = alphabet[triplet & 63];
129                 triplet >>= 6;
130                 dst[di + 1] = alphabet[triplet & 63];
131                 triplet >>= 6;
132                 dst[di + 2] = alphabet[triplet];
133                 dst[di + 3] = 0;
134                 length = di + 3;
135                 break;
136
137         case 1:
138                 triplet = usrc[si];
139                 dst[di] = alphabet[triplet & 63];
140                 triplet >>= 6;
141                 dst[di + 1] = alphabet[triplet];
142                 dst[di + 2] = 0;
143                 length = di + 2;
144                 break;
145
146         default:
147                 dst[di] = 0;
148                 length = di;
149                 break;
150         }
151
152         while(si > 0) {
153                 di -= 4;
154                 si -= 3;
155                 triplet = usrc[si] | usrc[si + 1] << 8 | usrc[si + 2] << 16;
156                 dst[di] = alphabet[triplet & 63];
157                 triplet >>= 6;
158                 dst[di + 1] = alphabet[triplet & 63];
159                 triplet >>= 6;
160                 dst[di + 2] = alphabet[triplet & 63];
161                 triplet >>= 6;
162                 dst[di + 3] = alphabet[triplet];
163         }
164
165         return length;
166 }
167
168 int b64encode(const void *src, char *dst, int length) {
169         return b64encode_internal(src, dst, length, base64_original);
170 }
171
172 int b64encode_urlsafe(const void *src, char *dst, int length) {
173         return b64encode_internal(src, dst, length, base64_urlsafe);
174 }
175
176 #if defined(HAVE_MINGW) || defined(HAVE_CYGWIN)
177 #ifdef HAVE_CYGWIN
178 #include <w32api/windows.h>
179 #endif
180
181 const char *winerror(int err) {
182         static char buf[1024], *ptr;
183
184         ptr = buf + snprintf(buf, sizeof(buf), "(%d) ", err);
185
186         if(!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
187                           NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), ptr, sizeof(buf) - (ptr - buf), NULL)) {
188                 strncpy(buf, "(unable to format errormessage)", sizeof(buf));
189         };
190
191         if((ptr = strchr(buf, '\r'))) {
192                 *ptr = '\0';
193         }
194
195         return buf;
196 }
197 #endif
198
199 unsigned int bitfield_to_int(const void *bitfield, size_t size) {
200         unsigned int value = 0;
201
202         if(size > sizeof(value)) {
203                 size = sizeof(value);
204         }
205
206         memcpy(&value, bitfield, size);
207         return value;
208 }
209
210 bool check_id(const char *id) {
211         if(!id || !*id) {
212                 return false;
213         }
214
215         for(; *id; id++)
216                 if(!isalnum(*id) && *id != '_') {
217                         return false;
218                 }
219
220         return true;
221 }
222
223 bool check_netname(const char *netname, bool strict) {
224         if(!netname || !*netname || *netname == '.') {
225                 return false;
226         }
227
228         for(const char *c = netname; *c; c++) {
229                 if(iscntrl(*c)) {
230                         return false;
231                 }
232
233                 if(*c == '/' || *c == '\\') {
234                         return false;
235                 }
236
237                 if(strict && strchr(" $%<>:`\"|?*", *c)) {
238                         return false;
239                 }
240         }
241
242         return true;
243 }
244
245 /* Windows doesn't define HOST_NAME_MAX. */
246 #ifndef HOST_NAME_MAX
247 #define HOST_NAME_MAX 255
248 #endif
249
250 char *replace_name(const char *name) {
251         char *ret_name;
252
253         if(name[0] == '$') {
254                 char *envname = getenv(name + 1);
255                 char hostname[HOST_NAME_MAX + 1];
256
257                 if(!envname) {
258                         if(strcmp(name + 1, "HOST")) {
259                                 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid Name: environment variable %s does not exist\n", name + 1);
260                                 return NULL;
261                         }
262
263                         if(gethostname(hostname, sizeof(hostname)) || !*hostname) {
264                                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get hostname: %s\n", sockstrerror(sockerrno));
265                                 return NULL;
266                         }
267
268                         hostname[HOST_NAME_MAX] = 0;
269                         envname = hostname;
270                 }
271
272                 ret_name = xstrdup(envname);
273
274                 for(char *c = ret_name; *c; c++)
275                         if(!isalnum(*c)) {
276                                 *c = '_';
277                         }
278         } else {
279                 ret_name = xstrdup(name);
280         }
281
282         if(!check_id(ret_name)) {
283                 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!");
284                 free(ret_name);
285                 return NULL;
286         }
287
288         return ret_name;
289 }