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>
40 static char *base64set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
41 static int base64rev[] = {
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, 62, -1, -1, -1, 63,
45 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
46 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
47 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
48 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
49 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -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,
57 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
59 static char *base32set = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
60 static int base32rev[] = {
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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
64 -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1, -1, -1, -1,
65 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
66 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
67 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
68 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -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,
76 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
79 char *vsprintf2(char *format, va_list al)
86 ret = vsnprintf(NULL, 0, format, al2);
88 if((buf = malloc(ret + 1)) == NULL)
94 vsnprintf(buf, ret + 1, format, al2);
99 char *sprintf2(char *format, ...)
104 va_start(args, format);
105 buf = vsprintf2(format, args);
110 wchar_t *vswprintf2(wchar_t *format, va_list al)
117 buf = smalloc(sizeof(wchar_t) * (bufsize = 1024));
121 ret = vswprintf(buf, bufsize, format, al2);
125 buf = srealloc(buf, sizeof(wchar_t) * (bufsize *= 2));
127 if(bufsize > ret + 1)
128 buf = srealloc(buf, sizeof(wchar_t) * (ret + 1));
132 wchar_t *swprintf2(wchar_t *format, ...)
137 va_start(args, format);
138 buf = vswprintf2(format, args);
143 int havecharset(char *charset)
147 if((cd = iconv_open("wchar_t", charset)) == (iconv_t)-1)
150 if((cd = iconv_open(charset, "wchar_t")) == (iconv_t)-1)
156 wchar_t *icmbstowcs(char *mbs, char *charset)
161 size_t len1, len2, bufsize, data;
164 len1 = strlen(mbs) + 1;
165 bufsize = len2 = len1 * sizeof(wchar_t);
166 if((buf = malloc(bufsize)) == NULL)
172 charset = nl_langinfo(CODESET);
173 if((cd = iconv_open("wchar_t", charset)) == (iconv_t)-1)
176 flog(LOG_ERR, "icmbstowcs: could not open iconv structure for %s: %s", charset, strerror(errno));
184 ret = iconv(cd, &mbs, &len1, &p, &len2);
192 if((p2 = realloc(buf, bufsize)) == NULL)
209 buf = realloc(buf, p - buf);
211 return((wchar_t *)buf);
214 wchar_t *icsmbstowcs(char *mbs, char *charset, wchar_t *def)
216 static wchar_t *buf = NULL;
220 if((buf = icmbstowcs(mbs, charset)) == NULL)
222 if((def != NULL) && (*def == L'~'))
225 flog(LOG_WARNING, "icsmbstowcs: could not convert wcs string into charset %s: %s", charset, strerror(errno));
234 char *icwcstombs(wchar_t *wcs, char *charset)
239 size_t len1, len2, bufsize, data;
242 len1 = sizeof(wchar_t) * (wcslen(wcs) + 1);
243 bufsize = len2 = len1;
244 if((buf = malloc(bufsize)) == NULL)
252 charset = nl_langinfo(CODESET);
253 if((cd = iconv_open(charset, "wchar_t")) == (iconv_t)-1)
256 flog(LOG_ERR, "icwcstombs: could not open iconv structure for %s: %s", charset, strerror(errno));
264 ret = iconv(cd, (char **)&wcs, &len1, &p, &len2);
272 if((p2 = realloc(buf, bufsize)) == NULL)
289 buf = realloc(buf, p - buf);
294 char *icswcstombs(wchar_t *wcs, char *charset, char *def)
296 static char *buf = NULL;
300 if((buf = icwcstombs(wcs, charset)) == NULL)
302 if((def != NULL) && (*def == '~'))
305 flog(LOG_WARNING, "icswcstombs: could not convert mbs string from charset %s: %s", charset, strerror(errno));
314 wchar_t *wcstolower(wchar_t *wcs)
318 for(p = wcs; *p != L'\0'; p++)
323 wchar_t ucptowc(int ucp)
326 unsigned long ucpbuf;
330 size_t len1, len2, bufsize, data;
334 mbsp = (char *)&ucpbuf;
336 bufsize = len2 = len1 * sizeof(wchar_t);
337 if((buf = malloc(bufsize)) == NULL)
342 if((cd = iconv_open("wchar_t", "UCS-4BE")) == (iconv_t)-1)
345 flog(LOG_ERR, "ucptowc: could not open iconv structure for UCS-4BE: %s", strerror(errno));
353 ret = iconv(cd, &mbsp, &len1, &p, &len2);
361 if((p2 = realloc(buf, bufsize)) == NULL)
378 buf = realloc(buf, p - buf);
380 res = *(wchar_t *)buf;
385 void _sizebuf(void **buf, size_t *bufsize, size_t reqsize, size_t elsize, int algo)
387 if(*bufsize >= reqsize)
392 *buf = srealloc(*buf, elsize * ((*bufsize) = reqsize));
397 while(*bufsize < reqsize)
399 *buf = srealloc(*buf, elsize * (*bufsize));
408 gettimeofday(&tv, NULL);
409 return((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
412 int wcsexists(wchar_t *h, wchar_t *n)
417 ln = alloca(sizeof(*ln) * (nl = wcslen(n)));
418 for(i = 0; i < nl; i++)
419 ln[i] = towlower(n[i]);
420 lh = alloca(sizeof(*lh) * (hl = wcslen(h)));
423 for(i = 0; i < nl; i++)
424 lh[i] = towlower(h[i]);
428 for(o = 0; o < nl; o++)
430 if(lh[i + o] != ln[o])
437 lh[i + nl] = towlower(h[i + nl]);
442 #ifndef HAVE_WCSCASECMP
443 int wcscasecmp(const wchar_t *s1, const wchar_t *s2)
445 for(; (towlower(*s1) == towlower(*s2)) && (*s1 != L'\0'); s1++, s2++);
446 return(towlower(*s1) - towlower(*s2));
450 char *hexencode(char *data, size_t datalen)
453 size_t bufsize, bufdata;
457 bufsize = bufdata = 0;
458 for(; datalen > 0; datalen--, data++)
460 dig = (*data & 0xF0) >> 4;
462 this = 'A' + dig - 10;
468 this = 'A' + dig - 10;
477 char *hexdecode(char *data, size_t *len)
479 char *buf, this, bit;
480 size_t bufsize, bufdata;
483 bufsize = bufdata = 0;
484 for(bit = 4, this = 0; *data; data++)
486 if((*data >= 'A') && (*data <= 'F'))
488 this |= (this & 0x0F) | ((*data - 'A' + 10) << bit);
489 } else if((*data >= 'a') && (*data <= 'f')) {
490 this |= (this & 0x0F) | ((*data - 'a' + 10) << bit);
491 } else if((*data >= '0') && (*data <= '9')) {
492 this |= (this & 0x0F) | ((*data - '0') << bit);
493 } else if(*data == '\n') {
519 char *base64encode(char *data, size_t datalen)
522 size_t bufsize, bufdata;
527 bufsize = bufdata = 0;
530 addtobuf(buf, base64set[(data[0] & 0xfc) >> 2]);
531 addtobuf(buf, base64set[((data[0] & 0x03) << 4) | ((data[1] & 0xf0) >> 4)]);
532 addtobuf(buf, base64set[((data[1] & 0x0f) << 2) | ((data[2] & 0xc0) >> 6)]);
533 addtobuf(buf, base64set[data[2] & 0x3f]);
539 addtobuf(buf, base64set[(data[0] & 0xfc) >> 2]);
540 addtobuf(buf, base64set[(data[0] & 0x03) << 4]);
541 bufcat(buf, "==", 2);
545 addtobuf(buf, base64set[(data[0] & 0xfc) >> 2]);
546 addtobuf(buf, base64set[((data[0] & 0x03) << 4) | ((data[1] & 0xf0) >> 4)]);
547 addtobuf(buf, base64set[(data[1] & 0x0f) << 2]);
554 char *base64decode(char *data, size_t *datalen)
558 size_t bufsize, bufdata;
561 bufsize = bufdata = 0;
564 for(; *data > 0; data++)
566 c = (int)(unsigned char)*data;
571 if(base64rev[c] == -1)
580 cur |= base64rev[c] >> -b;
585 cur |= base64rev[c] << b;
593 char *base32encode(char *data, size_t datalen)
596 size_t bufsize, bufdata;
601 bufsize = bufdata = 0;
604 addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
605 addtobuf(buf, base32set[((data[0] & 0x07) << 2) | ((data[1] & 0xc0) >> 6)]);
606 addtobuf(buf, base32set[((data[1] & 0x3e) >> 1)]);
607 addtobuf(buf, base32set[((data[1] & 0x01) << 4) | ((data[2] & 0xf0) >> 4)]);
608 addtobuf(buf, base32set[((data[2] & 0x0f) << 1) | ((data[3] & 0x80) >> 7)]);
609 addtobuf(buf, base32set[((data[3] & 0x7c) >> 2)]);
610 addtobuf(buf, base32set[((data[3] & 0x03) << 3) | ((data[4] & 0xe0) >> 5)]);
611 addtobuf(buf, base32set[data[4] & 0x1f]);
617 addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
618 addtobuf(buf, base32set[((data[0] & 0x07) << 2)]);
619 bufcat(buf, "======", 6);
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)]);
627 bufcat(buf, "====", 4);
631 addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
632 addtobuf(buf, base32set[((data[0] & 0x07) << 2) | ((data[1] & 0xc0) >> 6)]);
633 addtobuf(buf, base32set[((data[1] & 0x3e) >> 1)]);
634 addtobuf(buf, base32set[((data[1] & 0x01) << 4) | ((data[2] & 0xf0) >> 4)]);
635 addtobuf(buf, base32set[((data[2] & 0x0f) << 1)]);
636 bufcat(buf, "===", 3);
640 addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
641 addtobuf(buf, base32set[((data[0] & 0x07) << 2) | ((data[1] & 0xc0) >> 6)]);
642 addtobuf(buf, base32set[((data[1] & 0x3e) >> 1)]);
643 addtobuf(buf, base32set[((data[1] & 0x01) << 4) | ((data[2] & 0xf0) >> 4)]);
644 addtobuf(buf, base32set[((data[2] & 0x0f) << 1) | ((data[3] & 0x80) >> 7)]);
645 addtobuf(buf, base32set[((data[3] & 0x7c) >> 2)]);
646 addtobuf(buf, base32set[((data[3] & 0x03) << 3)]);
653 char *base32decode(char *data, size_t *datalen)
657 size_t bufsize, bufdata;
660 bufsize = bufdata = 0;
663 for(; *data > 0; data++)
665 c = (int)(unsigned char)*data;
670 if(base32rev[c] == -1)
679 cur |= base32rev[c] >> -b;
684 cur |= base32rev[c] << b;
692 void _freeparr(void **arr)
698 for(buf = arr; *buf != NULL; buf++)
703 int _parrlen(void **arr)
709 for(i = 0; *arr != NULL; arr++)
714 char *getetcpath(char *binpath)
718 size_t etcpathsize, etcpathdata;
721 etcpathsize = etcpathdata = 0;
729 for(p = binpath; *p && (*p != ':'); p++);
730 for(; (p >= binpath) && (*p != '/'); p--);
734 addtobuf(etcpath, ':');
735 bufcat(etcpath, binpath, p - binpath + 1);
736 bufcat(etcpath, "etc", 3);
738 } while((binpath = strchr(binpath, ':')) != NULL);
739 addtobuf(etcpath, 0);
743 char *findfile(char *name, char *homedir, int filldef)
745 char *path, *binpath, *etcpath, *p;
752 mode = R_OK | (filldef ? W_OK : 0);
753 homeonly = homedir != NULL;
755 if(!strchr(name, '/'))
758 homedir = getenv("HOME");
759 if((homedir == NULL) && ((pw = getpwuid(getuid())) != NULL))
760 homedir = pw->pw_dir;
761 if((homedir != NULL) && ((path = sprintf2("%s/.%s", homedir, name)) != NULL))
763 if(!access(path, mode))
771 if(strchr(name, '/') != NULL)
773 if(!access(name, mode))
774 return(sstrdup(name));
776 if((binpath = getenv("PATH")) == NULL)
777 etcpath = sstrdup("/usr/local/etc:/etc:/usr/etc");
779 etcpath = getetcpath(binpath);
780 for(p = strtok(etcpath, ":"); p != NULL; p = strtok(NULL, ":"))
782 if((path = sprintf2("%s/%s", p, name)) != NULL)
784 if(!access(path, mode))
798 return(sprintf2("%s/.%s", homedir, name));
799 return(sprintf2("/etc/%s", name));
805 struct strpair *newstrpair(char *key, char *val, struct strpair **list)
807 struct strpair *pair;
809 pair = smalloc(sizeof(*pair));
810 memset(pair, 0, sizeof(*pair));
812 pair->key = sstrdup(key);
814 pair->val = sstrdup(val);
823 void freestrpair(struct strpair *pair, struct strpair **list)
827 for(cur = *list; cur != NULL; list = &(cur->next), cur = cur->next)
840 char *spfind(struct strpair *list, char *key)
842 for(; list != NULL; list = list->next)
844 if(!strcmp(list->key, key))
850 struct wcspair *newwcspair(wchar_t *key, wchar_t *val, struct wcspair **list)
852 struct wcspair *pair;
854 pair = smalloc(sizeof(*pair));
855 memset(pair, 0, sizeof(*pair));
857 pair->key = swcsdup(key);
859 pair->val = swcsdup(val);
868 void freewcspair(struct wcspair *pair, struct wcspair **list)
872 for(cur = *list; cur != NULL; list = &(cur->next), cur = cur->next)
885 wchar_t *wpfind(struct wcspair *list, wchar_t *key)
887 for(; list != NULL; list = list->next)
889 if(!wcscmp(list->key, key))