c2f4333fc783f3172dcf77bc2046b63a9de287d6
[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 uint8_t charhex2bin(char c) {
49         uint8_t cu = (uint8_t) c;
50
51         if(isdigit(cu)) {
52                 return cu - '0';
53         } else {
54                 return toupper(cu) - 'A' + 10;
55         }
56 }
57
58 size_t hex2bin(const char *src, void *vdst, size_t length) {
59         uint8_t *dst = vdst;
60         size_t i;
61
62         for(i = 0; i < length && isxdigit((uint8_t) src[i * 2]) && isxdigit((uint8_t) src[i * 2 + 1]); i++) {
63                 dst[i] = charhex2bin(src[i * 2]) * 16 + charhex2bin(src[i * 2 + 1]);
64         }
65
66         return i;
67 }
68
69 size_t bin2hex(const void *vsrc, char *dst, size_t length) {
70         const char *src = vsrc;
71
72         for(size_t i = length; i > 0;) {
73                 --i;
74                 dst[i * 2 + 1] = hexadecimals[(unsigned char) src[i] & 15];
75                 dst[i * 2] = hexadecimals[(unsigned char) src[i] >> 4];
76         }
77
78         dst[length * 2] = 0;
79         return length * 2;
80 }
81
82 size_t b64decode_tinc(const char *src, void *dst, size_t length) {
83         size_t i;
84         uint32_t triplet = 0;
85         unsigned char *udst = (unsigned char *)dst;
86
87         for(i = 0; i < length && src[i]; i++) {
88                 triplet |= base64_decode[src[i] & 0xff] << (6 * (i & 3));
89
90                 if((i & 3) == 3) {
91                         if(triplet & 0xff000000U) {
92                                 return 0;
93                         }
94
95                         udst[0] = triplet & 0xff;
96                         triplet >>= 8;
97                         udst[1] = triplet & 0xff;
98                         triplet >>= 8;
99                         udst[2] = triplet;
100                         triplet = 0;
101                         udst += 3;
102                 }
103         }
104
105         if(triplet & 0xff000000U) {
106                 return 0;
107         }
108
109         if((i & 3) == 3) {
110                 udst[0] = triplet & 0xff;
111                 triplet >>= 8;
112                 udst[1] = triplet & 0xff;
113                 return i / 4 * 3 + 2;
114         } else if((i & 3) == 2) {
115                 udst[0] = triplet & 0xff;
116                 return i / 4 * 3 + 1;
117         } else {
118                 return i / 4 * 3;
119         }
120 }
121
122 bool is_decimal(const char *str) {
123         if(!str) {
124                 return false;
125         }
126
127         errno = 0;
128         char *badchar = NULL;
129         strtol(str, &badchar, 10);
130         return !errno && badchar != str && !*badchar;
131 }
132
133 // itoa() conflicts with a similarly named function under MinGW.
134 char *int_to_str(int num) {
135         char *str = NULL;
136         xasprintf(&str, "%d", num);
137         return str;
138 }
139
140 static size_t b64encode_tinc_internal(const void *src, char *dst, size_t length, const char *alphabet) {
141         uint32_t triplet;
142         const unsigned char *usrc = (unsigned char *)src;
143         size_t si = length / 3 * 3;
144         size_t di = length / 3 * 4;
145
146         switch(length % 3) {
147         case 2:
148                 triplet = usrc[si] | usrc[si + 1] << 8;
149                 dst[di] = alphabet[triplet & 63];
150                 triplet >>= 6;
151                 dst[di + 1] = alphabet[triplet & 63];
152                 triplet >>= 6;
153                 dst[di + 2] = alphabet[triplet];
154                 dst[di + 3] = 0;
155                 length = di + 3;
156                 break;
157
158         case 1:
159                 triplet = usrc[si];
160                 dst[di] = alphabet[triplet & 63];
161                 triplet >>= 6;
162                 dst[di + 1] = alphabet[triplet];
163                 dst[di + 2] = 0;
164                 length = di + 2;
165                 break;
166
167         default:
168                 dst[di] = 0;
169                 length = di;
170                 break;
171         }
172
173         while(si > 0) {
174                 di -= 4;
175                 si -= 3;
176                 triplet = usrc[si] | usrc[si + 1] << 8 | usrc[si + 2] << 16;
177                 dst[di] = alphabet[triplet & 63];
178                 triplet >>= 6;
179                 dst[di + 1] = alphabet[triplet & 63];
180                 triplet >>= 6;
181                 dst[di + 2] = alphabet[triplet & 63];
182                 triplet >>= 6;
183                 dst[di + 3] = alphabet[triplet];
184         }
185
186         return length;
187 }
188
189 size_t b64encode_tinc(const void *src, char *dst, size_t length) {
190         return b64encode_tinc_internal(src, dst, length, base64_original);
191 }
192
193 size_t b64encode_tinc_urlsafe(const void *src, char *dst, size_t length) {
194         return b64encode_tinc_internal(src, dst, length, base64_urlsafe);
195 }
196
197 #ifdef HAVE_WINDOWS
198 const char *winerror(int err) {
199         static char buf[1024], *ptr;
200
201         ptr = buf + snprintf(buf, sizeof(buf), "(%d) ", err);
202
203         if(!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
204                           NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), ptr, sizeof(buf) - (ptr - buf), NULL)) {
205                 strncpy(buf, "(unable to format errormessage)", sizeof(buf));
206         };
207
208         if((ptr = strchr(buf, '\r'))) {
209                 *ptr = '\0';
210         }
211
212         return buf;
213 }
214 #endif
215
216 bool check_id(const char *id) {
217         if(!id || !*id) {
218                 return false;
219         }
220
221         for(; *id; id++)
222                 if(!isalnum((uint8_t) *id) && *id != '_') {
223                         return false;
224                 }
225
226         return true;
227 }
228
229 bool check_netname(const char *netname, bool strict) {
230         if(!netname || !*netname || *netname == '.') {
231                 return false;
232         }
233
234         for(const char *c = netname; *c; c++) {
235                 if(iscntrl((uint8_t) *c)) {
236                         return false;
237                 }
238
239                 if(*c == '/' || *c == '\\') {
240                         return false;
241                 }
242
243                 if(strict && strchr(" $%<>:`\"|?*", *c)) {
244                         return false;
245                 }
246         }
247
248         return true;
249 }
250
251 /* Windows doesn't define HOST_NAME_MAX. */
252 #ifndef HOST_NAME_MAX
253 #define HOST_NAME_MAX 255
254 #endif
255
256 char *replace_name(const char *name) {
257         char *ret_name;
258
259         if(name[0] == '$') {
260                 char *envname = getenv(name + 1);
261                 char hostname[HOST_NAME_MAX + 1];
262
263                 if(!envname) {
264                         if(strcmp(name + 1, "HOST")) {
265                                 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid Name: environment variable %s does not exist\n", name + 1);
266                                 return NULL;
267                         }
268
269                         if(gethostname(hostname, sizeof(hostname)) || !*hostname) {
270                                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get hostname: %s\n", sockstrerror(sockerrno));
271                                 return NULL;
272                         }
273
274                         hostname[HOST_NAME_MAX] = 0;
275                         envname = hostname;
276                 }
277
278                 ret_name = xstrdup(envname);
279
280                 for(char *c = ret_name; *c; c++)
281                         if(!isalnum((uint8_t) *c)) {
282                                 *c = '_';
283                         }
284         } else {
285                 ret_name = xstrdup(name);
286         }
287
288         if(!check_id(ret_name)) {
289                 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!");
290                 free(ret_name);
291                 return NULL;
292         }
293
294         return ret_name;
295 }
296
297 /* Open a file with the desired permissions, minus the umask.
298    Also, if we want to create an executable file, we call fchmod()
299    to set the executable bits. */
300
301 FILE *fopenmask(const char *filename, const char *mode, mode_t perms) {
302         mode_t mask = umask(0);
303         perms &= ~mask;
304         umask(~perms & 0777);
305         FILE *f = fopen(filename, mode);
306
307         if(!f) {
308                 fprintf(stderr, "Could not open %s: %s\n", filename, strerror(errno));
309                 return NULL;
310         }
311
312 #ifdef HAVE_FCHMOD
313
314         if(perms & 0444) {
315                 fchmod(fileno(f), perms);
316         }
317
318 #endif
319         umask(mask);
320         return f;
321 }
322