Add stricter checks for netnames.
[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 int hex2bin(const char *src, void *vdst, int length) {
56         char *dst = vdst;
57         int i;
58         for(i = 0; i < length && isxdigit(src[i * 2]) && isxdigit(src[i * 2 + 1]); i++)
59                 dst[i] = charhex2bin(src[i * 2]) * 16 + charhex2bin(src[i * 2 + 1]);
60         return i;
61 }
62
63 int bin2hex(const void *vsrc, char *dst, int length) {
64         const char *src = vsrc;
65         for(int i = length - 1; i >= 0; i--) {
66                 dst[i * 2 + 1] = hexadecimals[(unsigned char) src[i] & 15];
67                 dst[i * 2] = hexadecimals[(unsigned char) src[i] >> 4];
68         }
69         dst[length * 2] = 0;
70         return length * 2;
71 }
72
73 int b64decode(const char *src, void *dst, int length) {
74         int i;
75         uint32_t triplet = 0;
76         unsigned char *udst = (unsigned char *)dst;
77
78         for(i = 0; i < length && src[i]; i++) {
79                 triplet |= base64_decode[src[i] & 0xff] << (6 * (i & 3));
80                 if((i & 3) == 3) {
81                         if(triplet & 0xff000000U)
82                                 return 0;
83                         udst[0] = triplet & 0xff; triplet >>= 8;
84                         udst[1] = triplet & 0xff; triplet >>= 8;
85                         udst[2] = triplet;
86                         triplet = 0;
87                         udst += 3;
88                 }
89         }
90         if(triplet & 0xff000000U)
91                 return 0;
92         if((i & 3) == 3) {
93                 udst[0] = triplet & 0xff; triplet >>= 8;
94                 udst[1] = triplet & 0xff;
95                 return i / 4 * 3 + 2;
96         } else if((i & 3) == 2) {
97                 udst[0] = triplet & 0xff;
98                 return i / 4 * 3 + 1;
99         } else {
100                 return i / 4 * 3;
101         }
102 }
103
104 static int b64encode_internal(const void *src, char *dst, int length, const char *alphabet) {
105         uint32_t triplet;
106         const unsigned char *usrc = (unsigned char *)src;
107         int si = length / 3 * 3;
108         int di = length / 3 * 4;
109
110         switch(length % 3) {
111                 case 2:
112                         triplet = usrc[si] | usrc[si + 1] << 8;
113                         dst[di] = alphabet[triplet & 63]; triplet >>= 6;
114                         dst[di + 1] = alphabet[triplet & 63]; triplet >>= 6;
115                         dst[di + 2] = alphabet[triplet];
116                         dst[di + 3] = 0;
117                         length = di + 3;
118                         break;
119                 case 1:
120                         triplet = usrc[si];
121                         dst[di] = alphabet[triplet & 63]; triplet >>= 6;
122                         dst[di + 1] = alphabet[triplet];
123                         dst[di + 2] = 0;
124                         length = di + 2;
125                         break;
126                 default:
127                         dst[di] = 0;
128                         length = di;
129                         break;
130         }
131
132         while(si > 0) {
133                 di -= 4;
134                 si -= 3;
135                 triplet = usrc[si] | usrc[si + 1] << 8 | usrc[si + 2] << 16;
136                 dst[di] = alphabet[triplet & 63]; triplet >>= 6;
137                 dst[di + 1] = alphabet[triplet & 63]; triplet >>= 6;
138                 dst[di + 2] = alphabet[triplet & 63]; triplet >>= 6;
139                 dst[di + 3] = alphabet[triplet];
140         }
141
142         return length;
143 }
144
145 int b64encode(const void *src, char *dst, int length) {
146         return b64encode_internal(src, dst, length, base64_original);
147 }
148
149 int b64encode_urlsafe(const void *src, char *dst, int length) {
150         return b64encode_internal(src, dst, length, base64_urlsafe);
151 }
152
153 #if defined(HAVE_MINGW) || defined(HAVE_CYGWIN)
154 #ifdef HAVE_CYGWIN
155 #include <w32api/windows.h>
156 #endif
157
158 const char *winerror(int err) {
159         static char buf[1024], *ptr;
160
161         ptr = buf + snprintf(buf, sizeof buf, "(%d) ", err);
162
163         if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
164                 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), ptr, sizeof(buf) - (ptr - buf), NULL)) {
165                 strncpy(buf, "(unable to format errormessage)", sizeof(buf));
166         };
167
168         if((ptr = strchr(buf, '\r')))
169                 *ptr = '\0';
170
171         return buf;
172 }
173 #endif
174
175 unsigned int bitfield_to_int(const void *bitfield, size_t size) {
176         unsigned int value = 0;
177         if(size > sizeof value)
178                 size = sizeof value;
179         memcpy(&value, bitfield, size);
180         return value;
181 }
182
183 bool check_id(const char *id) {
184         if(!id || !*id)
185                 return false;
186
187         for(; *id; id++)
188                 if(!isalnum(*id) && *id != '_')
189                         return false;
190
191         return true;
192 }
193
194 bool check_netname(const char *netname, bool strict) {
195         if(!netname || !*netname || *netname == '.')
196                 return false;
197
198         for(const char *c = netname; *c; c++) {
199                 if(iscntrl(*c))
200                         return false;
201                 if(*c == '/' || *c == '\\')
202                         return false;
203                 if(strict && strchr(" $%<>:`\"|?*", *c))
204                         return false;
205         }
206
207         return true;
208 }
209
210 /* Windows doesn't define HOST_NAME_MAX. */
211 #ifndef HOST_NAME_MAX
212 #define HOST_NAME_MAX 255
213 #endif
214
215 char *replace_name(const char *name) {
216         char *ret_name;
217
218         if (name[0] == '$') {
219                 char *envname = getenv(name + 1);
220                 char hostname[HOST_NAME_MAX+1];
221                 if (!envname) {
222                         if (strcmp(name + 1, "HOST")) {
223                                 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid Name: environment variable %s does not exist\n", name + 1);
224                                 return NULL;
225                         }
226                         if (gethostname(hostname, sizeof hostname) || !*hostname) {
227                                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get hostname: %s\n", sockstrerror(sockerrno));
228                                 return NULL;
229                         }
230                         hostname[HOST_NAME_MAX] = 0;
231                         envname = hostname;
232                 }
233                 ret_name = xstrdup(envname);
234                 for (char *c = ret_name; *c; c++)
235                         if (!isalnum(*c))
236                                 *c = '_';
237         } else {
238                 ret_name = xstrdup(name);
239         }
240
241         if (!check_id(ret_name)) {
242                 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!");
243                 free(ret_name);
244                 return NULL;
245         }
246
247         return ret_name;
248 }