2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <netinet/in.h>
39 static char *base64set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
40 static int base64rev[] = {
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, 62, -1, -1, -1, 63,
44 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
45 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
46 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
47 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
48 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
49 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
50 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
51 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
52 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
53 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
54 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
55 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
56 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
58 static char *base32set = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
59 static int base32rev[] = {
60 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
61 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
62 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
63 -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1, -1, -1, -1,
64 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
65 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
66 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
67 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
68 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
69 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
70 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
71 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
72 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
73 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
74 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
75 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
78 char *vsprintf2(char *format, va_list al)
83 ret = vsnprintf(NULL, 0, format, al);
84 if((buf = malloc(ret + 1)) == NULL)
89 vsnprintf(buf, ret + 1, format, al);
93 char *sprintf2(char *format, ...)
98 va_start(args, format);
99 buf = vsprintf2(format, args);
104 wchar_t *vswprintf2(wchar_t *format, va_list al)
110 buf = smalloc(sizeof(wchar_t) * (bufsize = 1024));
111 while((ret = vswprintf(buf, bufsize, format, al)) < 0)
112 buf = srealloc(buf, sizeof(wchar_t) * (bufsize *= 2));
113 if(bufsize > ret + 1)
114 buf = srealloc(buf, sizeof(wchar_t) * (ret + 1));
118 wchar_t *swprintf2(wchar_t *format, ...)
123 va_start(args, format);
124 buf = vswprintf2(format, args);
129 int havecharset(char *charset)
133 if((cd = iconv_open("wchar_t", charset)) == (iconv_t)-1)
136 if((cd = iconv_open(charset, "wchar_t")) == (iconv_t)-1)
142 wchar_t *icmbstowcs(char *mbs, char *charset)
147 size_t len1, len2, bufsize, data;
150 len1 = strlen(mbs) + 1;
151 bufsize = len2 = len1 * sizeof(wchar_t);
152 if((buf = malloc(bufsize)) == NULL)
158 charset = nl_langinfo(CODESET);
159 if((cd = iconv_open("wchar_t", charset)) == (iconv_t)-1)
162 flog(LOG_ERR, "icmbstowcs: could not open iconv structure for %s: %s", charset, strerror(errno));
170 ret = iconv(cd, &mbs, &len1, &p, &len2);
178 if((p2 = realloc(buf, bufsize)) == NULL)
195 buf = realloc(buf, p - buf);
197 return((wchar_t *)buf);
200 wchar_t *icsmbstowcs(char *mbs, char *charset, wchar_t *def)
202 static wchar_t *buf = NULL;
206 if((buf = icmbstowcs(mbs, charset)) == NULL)
208 if((def != NULL) && (*def == L'~'))
211 flog(LOG_WARNING, "icsmbstowcs: could not convert wcs string into charset %s: %s", charset, strerror(errno));
220 char *icwcstombs(wchar_t *wcs, char *charset)
225 size_t len1, len2, bufsize, data;
228 len1 = sizeof(wchar_t) * (wcslen(wcs) + 1);
229 bufsize = len2 = len1;
230 if((buf = malloc(bufsize)) == NULL)
238 charset = nl_langinfo(CODESET);
239 if((cd = iconv_open(charset, "wchar_t")) == (iconv_t)-1)
242 flog(LOG_ERR, "icwcstombs: could not open iconv structure for %s: %s", charset, strerror(errno));
250 ret = iconv(cd, (char **)&wcs, &len1, &p, &len2);
258 if((p2 = realloc(buf, bufsize)) == NULL)
275 buf = realloc(buf, p - buf);
280 char *icswcstombs(wchar_t *wcs, char *charset, char *def)
282 static char *buf = NULL;
286 if((buf = icwcstombs(wcs, charset)) == NULL)
288 if((def != NULL) && (*def == '~'))
291 flog(LOG_WARNING, "icswcstombs: could not convert mbs string from charset %s: %s", charset, strerror(errno));
300 wchar_t *wcstolower(wchar_t *wcs)
304 for(p = wcs; *p != L'\0'; p++)
309 wchar_t ucptowc(int ucp)
312 unsigned long ucpbuf;
316 size_t len1, len2, bufsize, data;
320 mbsp = (char *)&ucpbuf;
322 bufsize = len2 = len1 * sizeof(wchar_t);
323 if((buf = malloc(bufsize)) == NULL)
328 if((cd = iconv_open("wchar_t", "UCS-4BE")) == (iconv_t)-1)
331 flog(LOG_ERR, "ucptowc: could not open iconv structure for UCS-4BE: %s", strerror(errno));
339 ret = iconv(cd, &mbsp, &len1, &p, &len2);
347 if((p2 = realloc(buf, bufsize)) == NULL)
364 buf = realloc(buf, p - buf);
366 res = *(wchar_t *)buf;
371 void _sizebuf(void **buf, size_t *bufsize, size_t reqsize, size_t elsize, int algo)
373 if(*bufsize >= reqsize)
378 *buf = srealloc(*buf, elsize * ((*bufsize) = reqsize));
383 while(*bufsize < reqsize)
385 *buf = srealloc(*buf, elsize * (*bufsize));
394 gettimeofday(&tv, NULL);
395 return((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
398 int wcsexists(wchar_t *h, wchar_t *n)
403 ln = alloca(sizeof(*ln) * (nl = wcslen(n)));
404 for(i = 0; i < nl; i++)
405 ln[i] = towlower(n[i]);
406 lh = alloca(sizeof(*lh) * (hl = wcslen(h)));
409 for(i = 0; i < nl; i++)
410 lh[i] = towlower(h[i]);
414 for(o = 0; o < nl; o++)
416 if(lh[i + o] != ln[o])
423 lh[i + nl] = towlower(h[i + nl]);
428 #ifndef HAVE_WCSCASECMP
429 int wcscasecmp(const wchar_t *s1, const wchar_t *s2)
431 while(towlower(*s1) == towlower(*s2))
436 return(towlower(*s1) - towlower(*s2));
440 char *hexencode(char *data, size_t datalen)
443 size_t bufsize, bufdata;
447 bufsize = bufdata = 0;
448 for(; datalen > 0; datalen--, data++)
450 dig = (*data & 0xF0) >> 4;
452 this = 'A' + dig - 10;
458 this = 'A' + dig - 10;
467 char *hexdecode(char *data, size_t *len)
470 size_t bufsize, bufdata;
473 bufsize = bufdata = 0;
476 if((*data >= 'A') && (*data <= 'F'))
478 this = (this & 0x0F) | ((*data - 'A' + 10) << 4);
479 } else if((*data >= '0') && (*data <= '9')) {
480 this = (this & 0x0F) | ((*data - '0') << 4);
493 if((*data >= 'A') && (*data <= 'F'))
495 this = (this & 0xF0) | (*data - 'A' + 10);
496 } else if((*data >= '0') && (*data <= '9')) {
497 this = (this & 0xF0) | (*data - '0');
511 char *base64encode(char *data, size_t datalen)
514 size_t bufsize, bufdata;
519 bufsize = bufdata = 0;
522 addtobuf(buf, base64set[(data[0] & 0xfc) >> 2]);
523 addtobuf(buf, base64set[((data[0] & 0x03) << 4) | ((data[1] & 0xf0) >> 4)]);
524 addtobuf(buf, base64set[((data[1] & 0x0f) << 2) | ((data[2] & 0xc0) >> 6)]);
525 addtobuf(buf, base64set[data[2] & 0x3f]);
531 addtobuf(buf, base64set[(data[0] & 0xfc) >> 2]);
532 addtobuf(buf, base64set[(data[0] & 0x03) << 4]);
533 bufcat(buf, "==", 2);
537 addtobuf(buf, base64set[(data[0] & 0xfc) >> 2]);
538 addtobuf(buf, base64set[((data[0] & 0x03) << 4) | ((data[1] & 0xf0) >> 4)]);
539 addtobuf(buf, base64set[(data[1] & 0x0f) << 2]);
546 char *base64decode(char *data, size_t *datalen)
550 size_t bufsize, bufdata;
553 bufsize = bufdata = 0;
556 for(; *data > 0; data++)
558 c = (int)(unsigned char)*data;
563 if(base64rev[c] == -1)
572 cur |= base64rev[c] >> -b;
577 cur |= base64rev[c] << b;
585 char *base32encode(char *data, size_t datalen)
588 size_t bufsize, bufdata;
593 bufsize = bufdata = 0;
596 addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
597 addtobuf(buf, base32set[((data[0] & 0x07) << 2) | ((data[1] & 0xc0) >> 6)]);
598 addtobuf(buf, base32set[((data[1] & 0x3e) >> 1)]);
599 addtobuf(buf, base32set[((data[1] & 0x01) << 4) | ((data[2] & 0xf0) >> 4)]);
600 addtobuf(buf, base32set[((data[2] & 0x0f) << 1) | ((data[3] & 0x80) >> 7)]);
601 addtobuf(buf, base32set[((data[3] & 0x7c) >> 2)]);
602 addtobuf(buf, base32set[((data[3] & 0x03) << 3) | ((data[4] & 0xe0) >> 5)]);
603 addtobuf(buf, base32set[data[4] & 0x1f]);
609 addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
610 addtobuf(buf, base32set[((data[0] & 0x07) << 2)]);
611 bufcat(buf, "======", 6);
615 addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
616 addtobuf(buf, base32set[((data[0] & 0x07) << 2) | ((data[1] & 0xc0) >> 6)]);
617 addtobuf(buf, base32set[((data[1] & 0x3e) >> 1)]);
618 addtobuf(buf, base32set[((data[1] & 0x01) << 4)]);
619 bufcat(buf, "====", 4);
623 addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
624 addtobuf(buf, base32set[((data[0] & 0x07) << 2) | ((data[1] & 0xc0) >> 6)]);
625 addtobuf(buf, base32set[((data[1] & 0x3e) >> 1)]);
626 addtobuf(buf, base32set[((data[1] & 0x01) << 4) | ((data[2] & 0xf0) >> 4)]);
627 addtobuf(buf, base32set[((data[2] & 0x0f) << 1)]);
628 bufcat(buf, "===", 3);
632 addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
633 addtobuf(buf, base32set[((data[0] & 0x07) << 2) | ((data[1] & 0xc0) >> 6)]);
634 addtobuf(buf, base32set[((data[1] & 0x3e) >> 1)]);
635 addtobuf(buf, base32set[((data[1] & 0x01) << 4) | ((data[2] & 0xf0) >> 4)]);
636 addtobuf(buf, base32set[((data[2] & 0x0f) << 1) | ((data[3] & 0x80) >> 7)]);
637 addtobuf(buf, base32set[((data[3] & 0x7c) >> 2)]);
638 addtobuf(buf, base32set[((data[3] & 0x03) << 3)]);
645 char *base32decode(char *data, size_t *datalen)
649 size_t bufsize, bufdata;
652 bufsize = bufdata = 0;
655 for(; *data > 0; data++)
657 c = (int)(unsigned char)*data;
662 if(base32rev[c] == -1)
671 cur |= base32rev[c] >> -b;
676 cur |= base32rev[c] << b;
684 void _freeparr(void **arr)
690 for(buf = arr; *buf != NULL; buf++)
695 int _parrlen(void **arr)
701 for(i = 0; *arr != NULL; arr++)
706 char *getetcpath(char *binpath)
710 size_t etcpathsize, etcpathdata;
713 etcpathsize = etcpathdata = 0;
721 for(p = binpath; *p && (*p != ':'); p++);
722 for(; (p >= binpath) && (*p != '/'); p--);
726 addtobuf(etcpath, ':');
727 bufcat(etcpath, binpath, p - binpath + 1);
728 bufcat(etcpath, "etc", 3);
730 } while((binpath = strchr(binpath, ':')) != NULL);
731 addtobuf(etcpath, 0);
735 char *findfile(char *gname, char *uname, char *homedir, int filldef)
737 char *path, *binpath, *etcpath, *p;
741 mode = R_OK | (filldef ? W_OK : 0);
744 homedir = getenv("HOME");
745 if((homedir == NULL) && ((pw = getpwuid(getuid())) != NULL))
746 homedir = pw->pw_dir;
747 if((homedir != NULL) && ((path = sprintf2("%s/.%s", homedir, uname)) != NULL))
749 if(!access(path, mode))
756 if(strchr(gname, '/') != NULL)
758 if(!access(gname, mode))
759 return(sstrdup(gname));
761 if((binpath = getenv("PATH")) == NULL)
762 etcpath = sstrdup("/usr/local/etc:/etc:/usr/etc");
764 etcpath = getetcpath(binpath);
765 for(p = strtok(etcpath, ":"); p != NULL; p = strtok(NULL, ":"))
767 if((path = sprintf2("%s/%s", p, gname)) != NULL)
769 if(!access(path, mode))
782 return(sprintf2("%s/.%s", homedir, uname));
783 return(sprintf2("/etc/%s", gname));
789 struct wcspair *newwcspair(wchar_t *key, wchar_t *val, struct wcspair **list)
791 struct wcspair *pair;
793 pair = smalloc(sizeof(*pair));
794 memset(pair, 0, sizeof(*pair));
796 pair->key = swcsdup(key);
798 pair->val = swcsdup(val);
809 void freewcspair(struct wcspair *pair, struct wcspair **list)
813 for(cur = *list; cur != NULL; list = &(cur->next), cur = cur->next)
826 wchar_t *wpfind(struct wcspair *list, wchar_t *key)
828 for(; list != NULL; list = list->next)
830 if(!wcscmp(list->key, key))