1 | #include "globals.h"
|
---|
2 |
|
---|
3 | static AES_KEY aeskey;
|
---|
4 |
|
---|
5 | void aes_set_key(char *key)
|
---|
6 | {
|
---|
7 | AES_set_decrypt_key((const unsigned char *)key, 128, &aeskey);
|
---|
8 | AES_set_encrypt_key((const unsigned char *)key, 128, &client[cs_idx].aeskey);
|
---|
9 | }
|
---|
10 |
|
---|
11 | void aes_decrypt(uchar *buf, int n)
|
---|
12 | {
|
---|
13 | int i;
|
---|
14 | for(i=0; i<n; i+=16)
|
---|
15 | AES_decrypt(buf+i, buf+i, &aeskey);
|
---|
16 | }
|
---|
17 |
|
---|
18 | void aes_encrypt_idx(int idx, uchar *buf, int n)
|
---|
19 | {
|
---|
20 | int i;
|
---|
21 | for(i=0; i<n; i+=16)
|
---|
22 | AES_encrypt(buf+i, buf+i, &client[idx].aeskey);
|
---|
23 | }
|
---|
24 |
|
---|
25 | char *remote_txt(void)
|
---|
26 | {
|
---|
27 | if (is_server)
|
---|
28 | return("client");
|
---|
29 | else
|
---|
30 | return("remote server");
|
---|
31 | }
|
---|
32 |
|
---|
33 | char *trim(txt)
|
---|
34 | char *txt;
|
---|
35 | {
|
---|
36 | register int l;
|
---|
37 | register char *p1, *p2;
|
---|
38 |
|
---|
39 | if (*txt==' ')
|
---|
40 | {
|
---|
41 | for (p1=p2=txt;
|
---|
42 | (*p1==' ') || (*p1=='\t') || (*p1=='\n') || (*p1=='\r');
|
---|
43 | p1++);
|
---|
44 | while (*p1)
|
---|
45 | *p2++=*p1++;
|
---|
46 | *p2='\0';
|
---|
47 | }
|
---|
48 | if ((l=strlen(txt))>0)
|
---|
49 | for (p1=txt+l-1;
|
---|
50 | (*p1==' ') || (*p1=='\t') || (*p1=='\n') || (*p1=='\r');
|
---|
51 | *p1--='\0');
|
---|
52 |
|
---|
53 | return(txt);
|
---|
54 | }
|
---|
55 |
|
---|
56 | char *strtolower(char *txt)
|
---|
57 | {
|
---|
58 | char *p;
|
---|
59 | for (p=txt; *p; p++)
|
---|
60 | if (isupper((uchar)*p)) *p=tolower((uchar)*p);
|
---|
61 | return(txt);
|
---|
62 | }
|
---|
63 |
|
---|
64 | int gethexval(char c)
|
---|
65 | {
|
---|
66 | if ((c>='0') && (c<='9')) return(c-'0');
|
---|
67 | if ((c>='A') && (c<='F')) return(c-'A'+10);
|
---|
68 | if ((c>='a') && (c<='f')) return(c-'a'+10);
|
---|
69 | return(-1);
|
---|
70 | }
|
---|
71 |
|
---|
72 | int cs_atob(uchar *buf, char *asc, int n)
|
---|
73 | {
|
---|
74 | int i, rc;
|
---|
75 | for (i=0; i<n; i++)
|
---|
76 | {
|
---|
77 | if ((rc=(gethexval(asc[i<<1])<<4)|gethexval(asc[(i<<1)+1]))&0x100)
|
---|
78 | return(-1);
|
---|
79 | buf[i]=rc;
|
---|
80 | }
|
---|
81 | return(n);
|
---|
82 | }
|
---|
83 |
|
---|
84 | ulong cs_atoi(char *asc, int l, int val_on_err)
|
---|
85 | {
|
---|
86 | int i, n=0;
|
---|
87 | ulong rc=0;
|
---|
88 | for (i=((l-1)<<1), errno=0; (i>=0) && (n<4); i-=2)
|
---|
89 | {
|
---|
90 | int b;
|
---|
91 | b=(gethexval(asc[i])<<4) | gethexval(asc[i+1]);
|
---|
92 | if (b<0)
|
---|
93 | {
|
---|
94 | errno=EINVAL;
|
---|
95 | rc=(val_on_err) ? 0xFFFFFFFF : 0;
|
---|
96 | break;
|
---|
97 | }
|
---|
98 | rc|=b<<(n<<3);
|
---|
99 | n++;
|
---|
100 | }
|
---|
101 | return(rc);
|
---|
102 | }
|
---|
103 |
|
---|
104 | int byte_atob(char *asc)
|
---|
105 | {
|
---|
106 | int rc;
|
---|
107 |
|
---|
108 | if (strlen(trim(asc))!=2)
|
---|
109 | rc=(-1);
|
---|
110 | else
|
---|
111 | if ((rc=(gethexval(asc[0])<<4)|gethexval(asc[1]))&0x100)
|
---|
112 | rc=(-1);
|
---|
113 | return(rc);
|
---|
114 | }
|
---|
115 |
|
---|
116 | long word_atob(char *asc)
|
---|
117 | {
|
---|
118 | long rc;
|
---|
119 |
|
---|
120 | if (strlen(trim(asc))!=4)
|
---|
121 | rc=(-1);
|
---|
122 | else
|
---|
123 | {
|
---|
124 | rc=gethexval(asc[0])<<12 | gethexval(asc[1])<<8 |
|
---|
125 | gethexval(asc[2])<<4 | gethexval(asc[3]);
|
---|
126 | if (rc&0x10000)
|
---|
127 | rc=(-1);
|
---|
128 | }
|
---|
129 | return(rc);
|
---|
130 | }
|
---|
131 |
|
---|
132 | int key_atob(char *asc, uchar *bin)
|
---|
133 | {
|
---|
134 | int i, n1, n2, rc;
|
---|
135 | for (i=rc=0; i<32; i+=2)
|
---|
136 | {
|
---|
137 | if ((n1=gethexval(asc[i ]))<0) rc=(-1);
|
---|
138 | if ((n2=gethexval(asc[i+1]))<0) rc=(-1);
|
---|
139 | bin[i>>1]=(n1<<4)+(n2&0xff);
|
---|
140 | }
|
---|
141 | return(rc);
|
---|
142 | }
|
---|
143 |
|
---|
144 | int key_atob14(char *asc, uchar *bin)
|
---|
145 | {
|
---|
146 | int i, n1, n2, rc;
|
---|
147 | for (i=rc=0; i<28; i+=2)
|
---|
148 | {
|
---|
149 | if ((n1=gethexval(asc[i ]))<0) rc=(-1);
|
---|
150 | if ((n2=gethexval(asc[i+1]))<0) rc=(-1);
|
---|
151 | bin[i>>1]=(n1<<4)+(n2&0xff);
|
---|
152 | }
|
---|
153 | return(rc);
|
---|
154 | }
|
---|
155 |
|
---|
156 | int key_atob_l(char *asc, uchar *bin, int l)
|
---|
157 | {
|
---|
158 | int i, n1, n2, rc;
|
---|
159 | for (i=rc=0; i<l; i+=2)
|
---|
160 | {
|
---|
161 | if ((n1=gethexval(asc[i ]))<0) rc=(-1);
|
---|
162 | if ((n2=gethexval(asc[i+1]))<0) rc=(-1);
|
---|
163 | bin[i>>1]=(n1<<4)+(n2&0xff);
|
---|
164 | }
|
---|
165 | return(rc);
|
---|
166 | }
|
---|
167 |
|
---|
168 | char *key_btoa(char *asc, uchar *bin)
|
---|
169 | {
|
---|
170 | int i;//, n1, n2, rc;
|
---|
171 | static char buf[33];
|
---|
172 | if (!asc)
|
---|
173 | asc=buf;
|
---|
174 | for (i=0; i<16; i++)
|
---|
175 | sprintf(asc+(i<<1), "%02X", bin[i]);
|
---|
176 | return(asc);
|
---|
177 | }
|
---|
178 |
|
---|
179 | char *cs_hexdump(int m, uchar *buf, int n)
|
---|
180 | {
|
---|
181 | int i;
|
---|
182 | static char dump[520];
|
---|
183 |
|
---|
184 | dump[i=0]='\0';
|
---|
185 | m=(m)?3:2;
|
---|
186 | if (m*n>=(int)sizeof(dump)) n=(sizeof(dump)/m)-1;
|
---|
187 | while (i<n)
|
---|
188 | sprintf(dump+(m*i++), "%02X%s", *buf++, (m>2)?" ":"");
|
---|
189 | return(dump);
|
---|
190 | }
|
---|
191 |
|
---|
192 | static int inet_byteorder=0;
|
---|
193 | in_addr_t cs_inet_order(in_addr_t n)
|
---|
194 | {
|
---|
195 | if (!inet_byteorder)
|
---|
196 | inet_byteorder=((inet_addr("1.2.3.4")+1)==inet_addr("1.2.3.5")) ? 1 : 2;
|
---|
197 | switch (inet_byteorder)
|
---|
198 | {
|
---|
199 | case 1:
|
---|
200 | break;
|
---|
201 | case 2:
|
---|
202 | n=((n&0xff000000) >> 24 ) |
|
---|
203 | ((n&0x00ff0000) >> 8 ) |
|
---|
204 | ((n&0x0000ff00) << 8 ) |
|
---|
205 | ((n&0x000000ff) << 24 );
|
---|
206 | break;
|
---|
207 | }
|
---|
208 | return(n);
|
---|
209 | }
|
---|
210 |
|
---|
211 | char *cs_inet_ntoa(in_addr_t n)
|
---|
212 | {
|
---|
213 | struct in_addr in;
|
---|
214 | in.s_addr=cs_inet_order(n);
|
---|
215 | return((char *)inet_ntoa(in));
|
---|
216 | }
|
---|
217 |
|
---|
218 | in_addr_t cs_inet_addr(char *txt)
|
---|
219 | {
|
---|
220 | if (!inet_byteorder)
|
---|
221 | inet_byteorder=((inet_addr("1.2.3.4")+1)==inet_addr("1.2.3.5")) ? 1 : 2;
|
---|
222 | if (inet_byteorder == 1)
|
---|
223 | return(inet_addr(txt));
|
---|
224 | else
|
---|
225 | return(inet_network(txt));
|
---|
226 | }
|
---|
227 |
|
---|
228 | ulong b2i(int n, uchar *b)
|
---|
229 | {
|
---|
230 | switch(n)
|
---|
231 | {
|
---|
232 | case 2:
|
---|
233 | return ((b[0]<<8) | b[1]);
|
---|
234 | case 3:
|
---|
235 | return ((b[0]<<16) | (b[1]<<8) | b[2]);
|
---|
236 | case 4:
|
---|
237 | return (((b[0]<<24) | (b[1]<<16) | (b[2]<<8) | b[3]) & 0xffffffffL);
|
---|
238 | default:
|
---|
239 | cs_log("Error in b2i, n=%i",n);
|
---|
240 | }
|
---|
241 | return 0;
|
---|
242 | }
|
---|
243 |
|
---|
244 | ullong b2ll(int n, uchar *b)
|
---|
245 | {
|
---|
246 | int i;
|
---|
247 | ullong k=0;
|
---|
248 | for(i=0; i<n; k+=b[i++])
|
---|
249 | k<<=8;
|
---|
250 | return(k);
|
---|
251 | }
|
---|
252 |
|
---|
253 | uchar *i2b(int n, ulong i)
|
---|
254 | {
|
---|
255 | static uchar b[4];
|
---|
256 | switch(n)
|
---|
257 | {
|
---|
258 | case 2:
|
---|
259 | b[0]=(i>> 8) & 0xff;
|
---|
260 | b[1]=(i ) & 0xff;
|
---|
261 | break;
|
---|
262 | case 3:
|
---|
263 | b[0]=(i>>16) & 0xff;
|
---|
264 | b[1]=(i>> 8) & 0xff;
|
---|
265 | b[2]=(i ) & 0xff;
|
---|
266 | case 4:
|
---|
267 | b[0]=(i>>24) & 0xff;
|
---|
268 | b[1]=(i>>16) & 0xff;
|
---|
269 | b[2]=(i>> 8) & 0xff;
|
---|
270 | b[3]=(i ) & 0xff;
|
---|
271 | break;
|
---|
272 | }
|
---|
273 | return(b);
|
---|
274 | }
|
---|
275 |
|
---|
276 | ulong a2i(char *asc, int bytes)
|
---|
277 | {
|
---|
278 | int i, n;
|
---|
279 | ulong rc;
|
---|
280 | for (rc=i=0, n=strlen(trim(asc))-1; i<(abs(bytes)<<1); n--, i++)
|
---|
281 | if (n>=0)
|
---|
282 | {
|
---|
283 | int rcl;
|
---|
284 | if ((rcl=gethexval(asc[n]))<0)
|
---|
285 | {
|
---|
286 | errno=EINVAL;
|
---|
287 | return(0x1F1F1F);
|
---|
288 | }
|
---|
289 | rc|=(rcl<<(i<<2));
|
---|
290 | }
|
---|
291 | else
|
---|
292 | if (bytes<0)
|
---|
293 | rc|=(0xf<<(i<<2));
|
---|
294 | errno=0;
|
---|
295 | return(rc);
|
---|
296 | }
|
---|
297 |
|
---|
298 | int boundary(int exp, int n)
|
---|
299 | {
|
---|
300 | return((((n-1)>>exp)+1)<<exp);
|
---|
301 | }
|
---|
302 |
|
---|
303 | void cs_ftime(struct timeb *tp)
|
---|
304 | {
|
---|
305 | #ifdef NO_FTIME
|
---|
306 | struct timeval tv;
|
---|
307 | gettimeofday(&tv, (struct timezone *)0);
|
---|
308 | tp->time=tv.tv_sec;
|
---|
309 | tp->millitm=tv.tv_usec/1000;
|
---|
310 | #else
|
---|
311 | ftime(tp);
|
---|
312 | #endif
|
---|
313 | }
|
---|
314 |
|
---|
315 | void cs_sleepms(unsigned int msec)
|
---|
316 | {
|
---|
317 | //does not interfere with signals like sleep and usleep do
|
---|
318 | struct timespec req_ts;
|
---|
319 | req_ts.tv_sec = msec/1000;
|
---|
320 | req_ts.tv_nsec = (msec % 1000) * 1000000L;
|
---|
321 | nanosleep (&req_ts, NULL);
|
---|
322 | }
|
---|
323 |
|
---|
324 | void cs_sleepus(unsigned int usec)
|
---|
325 | {
|
---|
326 | //does not interfere with signals like sleep and usleep do
|
---|
327 | struct timespec req_ts;
|
---|
328 | req_ts.tv_sec = usec/1000000;
|
---|
329 | req_ts.tv_nsec = (usec % 1000000) * 1000L;
|
---|
330 | nanosleep (&req_ts, NULL);
|
---|
331 | }
|
---|
332 |
|
---|
333 | int bytes_available(int fd)
|
---|
334 | {
|
---|
335 | struct pollfd pfds;
|
---|
336 | pfds.fd=fd;
|
---|
337 | pfds.events=POLLIN;
|
---|
338 | pfds.revents=0;
|
---|
339 | if (poll(&pfds, 1, 0)!=1)
|
---|
340 | return(0);
|
---|
341 | else
|
---|
342 | return(((pfds.revents)&POLLIN)==POLLIN);
|
---|
343 | }
|
---|
344 |
|
---|
345 |
|
---|
346 | #ifdef OS_CYGWIN32
|
---|
347 | #include <windows.h>
|
---|
348 | void cs_setpriority(int prio)
|
---|
349 | {
|
---|
350 | HANDLE WinId;
|
---|
351 | ulong wprio;
|
---|
352 | switch((prio+20)/10)
|
---|
353 | {
|
---|
354 | case 0: wprio=REALTIME_PRIORITY_CLASS; break;
|
---|
355 | case 1: wprio=HIGH_PRIORITY_CLASS; break;
|
---|
356 | case 2: wprio=NORMAL_PRIORITY_CLASS; break;
|
---|
357 | default: wprio=IDLE_PRIORITY_CLASS; break;
|
---|
358 | }
|
---|
359 | WinId=GetCurrentProcess();
|
---|
360 | SetPriorityClass(WinId, wprio);
|
---|
361 | }
|
---|
362 | #else
|
---|
363 | void cs_setpriority(int prio)
|
---|
364 | {
|
---|
365 | #ifdef PRIO_PROCESS
|
---|
366 | setpriority(PRIO_PROCESS, 0, prio); // ignore errors
|
---|
367 | #endif
|
---|
368 | }
|
---|
369 | #endif
|
---|
370 |
|
---|
371 | #ifdef WEBIF
|
---|
372 | /* Helper function for urldecode.*/
|
---|
373 | int x2i(int i){
|
---|
374 | i=toupper(i);
|
---|
375 | i = i - '0';
|
---|
376 | if(i > 9) i = i - 'A' + '9' + 1;
|
---|
377 | return i;
|
---|
378 | }
|
---|
379 |
|
---|
380 | /* Decodes values in a http url */
|
---|
381 | void urldecode(char *s){
|
---|
382 | int c, c1, n;
|
---|
383 | char *s0,*t;
|
---|
384 | t = s0 = s;
|
---|
385 | n = strlen(s);
|
---|
386 | while(n >0){
|
---|
387 | c = *s++;
|
---|
388 | if(c == '+') c = ' ';
|
---|
389 | else if(c == '%' && n > 2){
|
---|
390 | c = *s++;
|
---|
391 | c1 = c;
|
---|
392 | c = *s++;
|
---|
393 | c = 16*x2i(c1) + x2i(c);
|
---|
394 | n -= 2;
|
---|
395 | }
|
---|
396 | *t++ = c;
|
---|
397 | n--;
|
---|
398 | }
|
---|
399 | *t = 0;
|
---|
400 | }
|
---|
401 |
|
---|
402 | /* Helper function for urlencode.*/
|
---|
403 | char to_hex(char code){
|
---|
404 | static char hex[] = "0123456789abcdef";
|
---|
405 | return hex[code & 15];
|
---|
406 | }
|
---|
407 |
|
---|
408 | /* Encode values in a http url. Note: Be sure to free() the returned string after use */
|
---|
409 | char *urlencode(char *str){
|
---|
410 | char *pstr = str, *buf = (char *) malloc((strlen(str) * 3 + 1) * sizeof(char)), *pbuf = buf;
|
---|
411 | while (*pstr) {
|
---|
412 | if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') *pbuf++ = *pstr;
|
---|
413 | else if (*pstr == ' ') *pbuf++ = '+';
|
---|
414 | else {
|
---|
415 | *pbuf++ = '%';
|
---|
416 | *pbuf++ = to_hex(*pstr >> 4);
|
---|
417 | *pbuf++ = to_hex(*pstr & 15);
|
---|
418 | }
|
---|
419 | ++pstr;
|
---|
420 | }
|
---|
421 | *pbuf = '\0';
|
---|
422 | pbuf = (char *) malloc((strlen(buf) + 1) * sizeof(char));
|
---|
423 | strcpy(pbuf, buf);
|
---|
424 | free(buf);
|
---|
425 | return pbuf;
|
---|
426 | }
|
---|
427 |
|
---|
428 | /* Converts a char array to a char array with hex values (needed for example for md5). The hex2ascii
|
---|
429 | array is a lookup table with the corresponding hex string on the array position of the integer representation
|
---|
430 | of the ascii value. Note that you need to "free" the resulting array after usage or you'll get a memory leak!*/
|
---|
431 | char *char_to_hex(const unsigned char* p_array, unsigned int p_array_len, char hex2ascii[256][2]) {
|
---|
432 | unsigned char* str = (unsigned char*)malloc(p_array_len*2+1);
|
---|
433 | str[p_array_len*2] = '\0';
|
---|
434 | const unsigned char* p_end = p_array + p_array_len;
|
---|
435 | size_t pos=0;
|
---|
436 | const unsigned char* p;
|
---|
437 | for( p = p_array; p != p_end; p++, pos+=2 ) {
|
---|
438 | str[pos] = hex2ascii[*p][0];
|
---|
439 | str[pos+1] = hex2ascii[*p][1];
|
---|
440 | }
|
---|
441 | return (char*)str;
|
---|
442 | }
|
---|
443 |
|
---|
444 | /* Creates a random string with specified length. Note that dst must be one larger than size to hold the trailing \0*/
|
---|
445 | void create_rand_str(char *dst, int size){
|
---|
446 | static const char text[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
---|
447 | int i;
|
---|
448 | for (i = 0; i < size; ++i){
|
---|
449 | dst[i] = text[rand() % (sizeof(text) - 1)];
|
---|
450 | }
|
---|
451 | dst[i] = '\0';
|
---|
452 | }
|
---|
453 | #endif
|
---|
454 |
|
---|
455 | /* Converts a long value to a char array in bitwise representation.
|
---|
456 | Note that the result array MUST be at least 33 bit large and that
|
---|
457 | this function assumes long values to hold only values up to 32bits and to be positive!
|
---|
458 | the result of e.g. long 7 is 11100000000000000000000000000000 this means the array
|
---|
459 | is reversed */
|
---|
460 | void long2bitchar(long value, char *result){
|
---|
461 | int pos;
|
---|
462 | for (pos=0;pos<32;pos++) result[pos]='0';
|
---|
463 | result[pos] = '\0';
|
---|
464 |
|
---|
465 | pos=0;
|
---|
466 | while (value > 0 && pos < 32){
|
---|
467 | if(value % 2 == 1) result[pos]='1';
|
---|
468 | else result[pos]='0';
|
---|
469 | value=value / 2;
|
---|
470 | pos++;
|
---|
471 | }
|
---|
472 | }
|
---|
473 |
|
---|
474 | /* Return 1 if the file exists, else 0 */
|
---|
475 | int file_exists(const char * filename){
|
---|
476 | FILE *file;
|
---|
477 | if ((file = fopen(filename, "r"))){
|
---|
478 | fclose(file);
|
---|
479 | return 1;
|
---|
480 | }
|
---|
481 | return 0;
|
---|
482 | }
|
---|
483 |
|
---|
484 | /* Clears the s_ip structure provided. The pointer will be set to NULL so everything is cleared.*/
|
---|
485 | void clear_sip(struct s_ip **sip){
|
---|
486 | struct s_ip *cip = *sip, *lip;
|
---|
487 | for (*sip = NULL; cip != NULL; cip = lip){
|
---|
488 | lip = cip->next;
|
---|
489 | free(cip);
|
---|
490 | }
|
---|
491 | }
|
---|
492 |
|
---|
493 | /* Clears the s_ftab struct provided by setting nfilts and nprids to zero. */
|
---|
494 | void clear_ftab(struct s_ftab *ftab){
|
---|
495 | int i, j;
|
---|
496 | for (i = 0; i < CS_MAXFILTERS; i++) {
|
---|
497 | ftab->filts[i].caid = 0;
|
---|
498 | for (j = 0; j < CS_MAXPROV; j++)
|
---|
499 | ftab->filts[i].prids[j] = 0;
|
---|
500 | ftab->filts[i].nprids = 0;
|
---|
501 | }
|
---|
502 | ftab->nfilts = 0;
|
---|
503 | }
|
---|
504 |
|
---|
505 | /* Clears the s_ptab struct provided by setting nfilts and nprids to zero. */
|
---|
506 | void clear_ptab(struct s_ptab *ptab){
|
---|
507 | int i;
|
---|
508 | for (i = 0; i < ptab->nports; i++) {
|
---|
509 | ptab->ports[i].ftab.nfilts = 0;
|
---|
510 | ptab->ports[i].ftab.filts[0].nprids = 0;
|
---|
511 | }
|
---|
512 | ptab->nports = 0;
|
---|
513 | }
|
---|
514 |
|
---|
515 | /* Clears given caidtab */
|
---|
516 | void clear_caidtab(struct s_caidtab *ctab){
|
---|
517 | int i;
|
---|
518 | for (i = 0; i < CS_MAXCAIDTAB; i++) {
|
---|
519 | ctab->caid[i] = 0;
|
---|
520 | ctab->mask[i] = 0;
|
---|
521 | ctab->cmap[i] = 0;
|
---|
522 | }
|
---|
523 | }
|
---|
524 |
|
---|
525 | /* Clears given tuntab */
|
---|
526 | void clear_tuntab(struct s_tuntab *ttab){
|
---|
527 | int i;
|
---|
528 | for (i = 0; i < CS_MAXTUNTAB; i++) {
|
---|
529 | ttab->bt_caidfrom[i] = 0;
|
---|
530 | ttab->bt_caidto[i] = 0;
|
---|
531 | ttab->bt_srvid[i] = 0;
|
---|
532 | }
|
---|
533 | }
|
---|
534 | /* Overwrites destfile with tmpfile. If forceBakOverWrite = 0, the bakfile will not be overwritten if it exists, else it will be.*/
|
---|
535 | int safe_overwrite_with_bak(char *destfile, char *tmpfile, char *bakfile, int forceBakOverWrite){
|
---|
536 | if(forceBakOverWrite != 0 && file_exists(bakfile)){
|
---|
537 | if(remove(bakfile) < 0) cs_log("Error removing backup conf file %s (errno=%d)! Will try to proceed nonetheless...", bakfile, errno);
|
---|
538 | }
|
---|
539 | if(file_exists(bakfile)){
|
---|
540 | if(remove(destfile) < 0) {
|
---|
541 | cs_log("Error removing original conf file %s (errno=%d). Will maintain original one!", destfile, errno);
|
---|
542 | if(remove(tmpfile) < 0) cs_log("Error removing temp conf file %s (errno=%d)!", tmpfile, errno);
|
---|
543 | return(1);
|
---|
544 | }
|
---|
545 | } else {
|
---|
546 | if(rename(destfile, bakfile) < 0){
|
---|
547 | cs_log("Error renaming original conf file %s to %s (errno=%d). Will maintain original one!", destfile, bakfile, errno);
|
---|
548 | if(remove(tmpfile) < 0) cs_log("Error removing temp conf file %s (errno=%d)!", tmpfile, errno);
|
---|
549 | return(1);
|
---|
550 | }
|
---|
551 | }
|
---|
552 | if(rename(tmpfile, destfile) < 0){
|
---|
553 | cs_log("Error renaming new conf file %s to %s (errno=%d). The config will be missing upon next startup as this is non-recoverable!", tmpfile, destfile, errno);
|
---|
554 | return(1);
|
---|
555 | }
|
---|
556 | return(0);
|
---|
557 | }
|
---|
558 |
|
---|
559 | /* Replacement of fprintf which adds necessary whitespace to fill up the varname to a fixed width.
|
---|
560 | If varname is longer than varnameWidth, no whitespace is added*/
|
---|
561 | void fprintf_conf(FILE *f, int varnameWidth, const char *varname, const char *fmtstring, ...){
|
---|
562 | int varlen = strlen(varname);
|
---|
563 | int max = (varlen > varnameWidth) ? varlen : varnameWidth;
|
---|
564 | char varnamebuf[max + 3];
|
---|
565 | char *ptr = varnamebuf + varlen;
|
---|
566 | va_list argptr;
|
---|
567 |
|
---|
568 | strcpy(varnamebuf, varname);
|
---|
569 | while(varlen < varnameWidth){
|
---|
570 | ptr[0] = ' ';
|
---|
571 | ++ptr;
|
---|
572 | ++varlen;
|
---|
573 | }
|
---|
574 | strcpy(ptr, "= ");
|
---|
575 | fwrite(varnamebuf, sizeof(char), strlen(varnamebuf), f);
|
---|
576 | if(strlen(fmtstring) > 0){
|
---|
577 | va_start(argptr, fmtstring);
|
---|
578 | vfprintf(f, fmtstring, argptr);
|
---|
579 | va_end(argptr);
|
---|
580 | }
|
---|
581 | }
|
---|
582 |
|
---|
583 | /* Ordinary strncpy does not terminate the string if the source is exactly as long or longer as the specified size. This can raise security issues.
|
---|
584 | This function is a replacement which makes sure that a \0 is always added. num should be the real size of char array (do not subtract -1). */
|
---|
585 | void cs_strncpy(char * destination, const char * source, size_t num){
|
---|
586 | uint32 l, size = strlen(source);
|
---|
587 | if(size > num - 1) l = num - 1;
|
---|
588 | else l = size;
|
---|
589 | memcpy(destination, source, l);
|
---|
590 | destination[l] = '\0';
|
---|
591 | }
|
---|
592 |
|
---|
593 | char *get_servicename(int srvid, int caid){
|
---|
594 | int i;
|
---|
595 | struct s_srvid *this = cfg->srvid;
|
---|
596 | static char name[83];
|
---|
597 |
|
---|
598 | for (name[0] = 0; this && (!name[0]); this = this->next)
|
---|
599 | if (this->srvid == srvid)
|
---|
600 | for (i=0; i<this->ncaid; i++)
|
---|
601 | if (this->caid[i] == caid)
|
---|
602 | cs_strncpy(name, this->name, 32);
|
---|
603 |
|
---|
604 | if (!name[0]) sprintf(name, "%04X:%04X unknown", caid, srvid);
|
---|
605 | if (!srvid) name[0] = '\0';
|
---|
606 | return(name);
|
---|
607 | }
|
---|