utils: Refactor get_name's functionality into util for global access
[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 "system.h"
22 #include "xalloc.h"
23
24 #include "../src/logger.h"
25 #include "utils.h"
26
27 static const char hexadecimals[] = "0123456789ABCDEF";
28 static const char base64_original[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
29 static const char base64_urlsafe[] =  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
30 static const char base64_decode[256] = {
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, -1, -1, -1, -1, -1,
33         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63,
34         52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
35         -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
36         15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63,
37         -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
38         41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -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         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
47 };
48
49 static int charhex2bin(char c) {
50         if(isdigit(c))
51                 return c - '0';
52         else
53                 return toupper(c) - 'A' + 10;
54 }
55
56 int hex2bin(const char *src, void *vdst, int length) {
57         char *dst = vdst;
58         int i;
59         for(i = 0; i < length && isxdigit(src[i * 2]) && isxdigit(src[i * 2 + 1]); i++)
60                 dst[i] = charhex2bin(src[i * 2]) * 16 + charhex2bin(src[i * 2 + 1]);
61         return i;
62 }
63
64 int bin2hex(const void *vsrc, char *dst, int length) {
65         const char *src = vsrc;
66         for(int i = length - 1; i >= 0; i--) {
67                 dst[i * 2 + 1] = hexadecimals[(unsigned char) src[i] & 15];
68                 dst[i * 2] = hexadecimals[(unsigned char) src[i] >> 4];
69         }
70         dst[length * 2] = 0;
71         return length * 2;
72 }
73
74 int b64decode(const char *src, void *dst, int length) {
75         int i;
76         uint32_t triplet = 0;
77         unsigned char *udst = (unsigned char *)dst;
78
79         for(i = 0; i < length && src[i]; i++) {
80                 triplet |= base64_decode[src[i] & 0xff] << (6 * (i & 3));
81                 if((i & 3) == 3) {
82                         if(triplet & 0xff000000U)
83                                 return 0;
84                         udst[0] = triplet & 0xff; triplet >>= 8;
85                         udst[1] = triplet & 0xff; triplet >>= 8;
86                         udst[2] = triplet;
87                         triplet = 0;
88                         udst += 3;
89                 }
90         }
91         if(triplet & 0xff000000U)
92                 return 0;
93         if((i & 3) == 3) {
94                 udst[0] = triplet & 0xff; triplet >>= 8;
95                 udst[1] = triplet & 0xff;
96                 return i / 4 * 3 + 2;
97         } else if((i & 3) == 2) {
98                 udst[0] = triplet & 0xff;
99                 return i / 4 * 3 + 1;
100         } else {
101                 return i / 4 * 3;
102         }
103 }
104
105 static int b64encode_internal(const void *src, char *dst, int length, const char *alphabet) {
106         uint32_t triplet;
107         const unsigned char *usrc = (unsigned char *)src;
108         int si = length / 3 * 3;
109         int di = length / 3 * 4;
110
111         switch(length % 3) {
112                 case 2:
113                         triplet = usrc[si] | usrc[si + 1] << 8;
114                         dst[di] = alphabet[triplet & 63]; triplet >>= 6;
115                         dst[di + 1] = alphabet[triplet & 63]; triplet >>= 6;
116                         dst[di + 2] = alphabet[triplet];
117                         dst[di + 3] = 0;
118                         length = di + 3;
119                         break;
120                 case 1:
121                         triplet = usrc[si];
122                         dst[di] = alphabet[triplet & 63]; triplet >>= 6;
123                         dst[di + 1] = alphabet[triplet];
124                         dst[di + 2] = 0;
125                         length = di + 2;
126                         break;
127                 default:
128                         dst[di] = 0;
129                         length = di;
130                         break;
131         }
132
133         while(si > 0) {
134                 di -= 4;
135                 si -= 3;
136                 triplet = usrc[si] | usrc[si + 1] << 8 | usrc[si + 2] << 16;
137                 dst[di] = alphabet[triplet & 63]; triplet >>= 6;
138                 dst[di + 1] = alphabet[triplet & 63]; triplet >>= 6;
139                 dst[di + 2] = alphabet[triplet & 63]; triplet >>= 6;
140                 dst[di + 3] = alphabet[triplet];
141         }
142
143         return length;
144 }
145
146 int b64encode(const void *src, char *dst, int length) {
147         return b64encode_internal(src, dst, length, base64_original);
148 }
149
150 int b64encode_urlsafe(const void *src, char *dst, int length) {
151         return b64encode_internal(src, dst, length, base64_urlsafe);
152 }
153
154 #if defined(HAVE_MINGW) || defined(HAVE_CYGWIN)
155 #ifdef HAVE_CYGWIN
156 #include <w32api/windows.h>
157 #endif
158
159 const char *winerror(int err) {
160         static char buf[1024], *ptr;
161
162         ptr = buf + sprintf(buf, "(%d) ", err);
163
164         if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
165                 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), ptr, sizeof(buf) - (ptr - buf), NULL)) {
166                 strncpy(buf, "(unable to format errormessage)", sizeof(buf));
167         };
168
169         if((ptr = strchr(buf, '\r')))
170                 *ptr = '\0';
171
172         return buf;
173 }
174 #endif
175
176 unsigned int bitfield_to_int(const void *bitfield, size_t size) {
177         unsigned int value = 0;
178         if(size > sizeof value)
179                 size = sizeof value;
180         memcpy(&value, bitfield, size);
181         return value;
182 }
183
184 char *replace_name(const char *name) {
185         char *ret_name;
186
187         if (name[0] == '$') {
188                 char *envname = getenv(name + 1);
189                 char hostname[HOST_NAME_MAX+1];
190                 if (!envname) {
191                         if (strcmp(name + 1, "HOST")) {
192                                 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid Name: environment variable %s does not exist\n", name + 1);
193                                 return NULL;
194                         }
195                         if (gethostname(hostname, sizeof hostname) || !*hostname) {
196                                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get hostname: %s\n", sockstrerror(sockerrno));
197                                 return NULL;
198                         }
199                         hostname[HOST_NAME_MAX] = 0;
200                         envname = hostname;
201                 }
202                 ret_name = xstrdup(envname);
203                 for (char *c = ret_name; *c; c++)
204                         if (!isalnum(*c))
205                                 *c = '_';
206         } else {
207                 ret_name = xstrdup(name);
208         }
209
210         if (!check_id(ret_name)) {
211                 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!");
212                 free(ret_name);
213                 return NULL;
214         }
215
216         return ret_name;
217 }