2
* Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003
3
* Inferno Nettverk A/S, Norway. All rights reserved.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
8
* 1. The above copyright notice, this list of conditions and the following
9
* disclaimer must appear in all copies of the software, derivative works
10
* or modified versions, and any portions thereof, aswell as in all
11
* supporting documentation.
12
* 2. All advertising materials mentioning features or use of this software
13
* must display the following acknowledgement:
14
* This product includes software developed by
15
* Inferno Nettverk A/S, Norway.
16
* 3. The name of the author may not be used to endorse or promote products
17
* derived from this software without specific prior written permission.
19
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
* Inferno Nettverk A/S requests users of this software to return to
32
* Software Distribution Coordinator or sdc@inet.no
33
* Inferno Nettverk A/S
39
* any improvements or extensions that they make and grant Inferno Nettverk A/S
40
* the rights to redistribute these changes.
45
#include "config_parse.h"
47
static const char rcsid[] =
48
"$Id: tostring.c,v 1.9 2003/07/01 13:21:32 michaels Exp $";
51
proxyprotocols2string(proxyprotocols, str, strsize)
52
const struct proxyprotocol_t *proxyprotocols;
59
*str = NUL; /* make sure we return a NUL terminated string. */
65
if (proxyprotocols->socks_v4)
66
strused += snprintfn(&str[strused], strsize - strused, "%s, ",
69
if (proxyprotocols->socks_v5)
70
strused += snprintfn(&str[strused], strsize - strused, "%s, ",
73
if (proxyprotocols->msproxy_v2)
74
strused += snprintfn(&str[strused], strsize - strused, "%s, ",
77
if (proxyprotocols->http_v1_0)
78
strused += snprintfn(&str[strused], strsize - strused, "%s, ",
85
protocols2string(protocols, str, strsize)
86
const struct protocol_t *protocols;
93
*str = NUL; /* make sure we return a NUL terminated string. */
100
strused += snprintfn(&str[strused], strsize - strused, "%s, ",
101
QUOTE(PROTOCOL_TCPs));
104
strused += snprintfn(&str[strused], strsize - strused, "%s, ",
105
QUOTE(PROTOCOL_UDPs));
112
socks_packet2string(packet, type)
116
static char buf[1024];
117
char hstring[MAXSOCKSHOSTSTRING];
118
unsigned char version;
119
const struct request_t *request = NULL;
120
const struct response_t *response = NULL;
124
request = (const struct request_t *)packet;
125
version = request->version;
129
response = (const struct response_t *)packet;
130
version = response->version;
139
case SOCKS_V4REPLY_VERSION:
142
snprintfn(buf, sizeof(buf),
143
"(V4) VN: %d CD: %d address: %s",
144
request->version, request->command,
145
sockshost2string(&request->host, hstring, sizeof(hstring)));
149
snprintfn(buf, sizeof(buf), "(V4) VN: %d CD: %d address: %s",
150
response->version, response->reply,
151
sockshost2string(&response->host, hstring, sizeof(hstring)));
159
snprintfn(buf, sizeof(buf),
160
"VER: %d CMD: %d FLAG: %d ATYP: %d address: %s",
161
request->version, request->command, request->flag,
163
sockshost2string(&request->host, hstring, sizeof(hstring)));
167
snprintfn(buf, sizeof(buf),
168
"VER: %d REP: %d FLAG: %d ATYP: %d address: %s",
169
response->version, response->reply, response->flag,
170
response->host.atype,
171
sockshost2string(&response->host, hstring, sizeof(hstring)));
185
string2operator(string)
189
if (strcmp(string, "eq") == 0 || strcmp(string, "=") == 0)
192
if (strcmp(string, "neq") == 0 || strcmp(string, "!=") == 0)
195
if (strcmp(string, "ge") == 0 || strcmp(string, ">=") == 0)
198
if (strcmp(string, "le") == 0 || strcmp(string, "<=") == 0)
201
if (strcmp(string, "gt") == 0 || strcmp(string, ">") == 0)
204
if (strcmp(string, "lt") == 0 || strcmp(string, "<") == 0)
207
/* parser should make sure this never happens. */
216
operator2string(operator)
217
enum operator_t operator;
222
return QUOTE("none");
243
return QUOTE("range");
254
ruleaddress2string(address, string, len)
255
const struct ruleaddress_t *address;
261
if (string == NULL) {
262
static char addrstring[MAXRULEADDRSTRING];
265
len = sizeof(addrstring);
268
switch (address->atype) {
269
case SOCKS_ADDR_IPV4: {
272
snprintfn(string, len,
273
"%s: %s/%d, %s: %d, %s : %d, %s: %s, %s: %d",
275
QUOTE(strcheck(a = strdup(inet_ntoa(address->addr.ipv4.ip)))),
276
QUOTE(bitcount((unsigned long)address->addr.ipv4.mask.s_addr)),
278
QUOTE(ntohs(address->port.tcp)),
280
QUOTE(ntohs(address->port.udp)),
282
QUOTE(operator2string(address->operator)),
284
QUOTE(ntohs(address->portend)));
289
case SOCKS_ADDR_DOMAIN:
290
snprintfn(string, len,
291
"%s: %s, %s: %d, %s : %d, %s: %s, %s: %d",
293
QUOTE(address->addr.domain),
295
QUOTE(ntohs(address->port.tcp)),
297
QUOTE(ntohs(address->port.udp)),
299
QUOTE(operator2string(address->operator)),
301
QUOTE(ntohs(address->portend)));
304
case SOCKS_ADDR_IFNAME:
305
snprintfn(string, len,
306
"%s: %s, %s: %d, %s : %d, %s: %s, %s: %d",
308
QUOTE(address->addr.ifname),
310
QUOTE(ntohs(address->port.tcp)),
312
QUOTE(ntohs(address->port.udp)),
314
QUOTE(operator2string(address->operator)),
316
QUOTE(ntohs(address->portend)));
320
SERRX(address->atype);
328
protocol2string(protocol)
334
return QUOTE(PROTOCOL_TCPs);
337
return QUOTE(PROTOCOL_UDPs);
347
resolveprotocol2string(resolveprotocol)
350
switch (resolveprotocol) {
351
case RESOLVEPROTOCOL_TCP:
352
return QUOTE(PROTOCOL_TCPs);
354
case RESOLVEPROTOCOL_UDP:
355
return QUOTE(PROTOCOL_UDPs);
357
case RESOLVEPROTOCOL_FAKE:
358
return QUOTE("fake");
361
SERRX(resolveprotocol);
369
command2string(command)
375
return QUOTE(SOCKS_BINDs);
378
return QUOTE(SOCKS_CONNECTs);
380
case SOCKS_UDPASSOCIATE:
381
return QUOTE(SOCKS_UDPASSOCIATEs);
383
/* pseudo commands. */
385
return QUOTE(SOCKS_ACCEPTs);
387
case SOCKS_BINDREPLY:
388
return QUOTE(SOCKS_BINDREPLYs);
391
return QUOTE(SOCKS_UDPREPLYs);
393
case SOCKS_DISCONNECT:
394
return QUOTE(SOCKS_DISCONNECTs);
404
commands2string(command, str, strsize)
405
const struct command_t *command;
412
*str = NUL; /* make sure we return a NUL terminated string. */
419
strused += snprintfn(&str[strused], strsize - strused, "%s, ",
420
command2string(SOCKS_BIND));
422
if (command->bindreply)
423
strused += snprintfn(&str[strused], strsize - strused, "%s, ",
424
command2string(SOCKS_BINDREPLY));
426
if (command->connect)
427
strused += snprintfn(&str[strused], strsize - strused, "%s, ",
428
command2string(SOCKS_CONNECT));
430
if (command->udpassociate)
431
strused += snprintfn(&str[strused], strsize - strused, "%s, ",
432
command2string(SOCKS_UDPASSOCIATE));
434
if (command->udpreply)
435
strused += snprintfn(&str[strused], strsize - strused, "%s, ",
436
command2string(SOCKS_UDPREPLY));
442
method2string(method)
447
case AUTHMETHOD_NONE:
448
return QUOTE(AUTHMETHOD_NONEs);
450
case AUTHMETHOD_GSSAPI:
451
return QUOTE(AUTHMETHOD_GSSAPIs);
453
case AUTHMETHOD_UNAME:
454
return QUOTE(AUTHMETHOD_UNAMEs);
456
case AUTHMETHOD_NOACCEPT:
457
return QUOTE(AUTHMETHOD_NOACCEPTs);
459
case AUTHMETHOD_RFC931:
460
return QUOTE(AUTHMETHOD_RFC931s);
463
return QUOTE(AUTHMETHOD_PAMs);
473
methods2string(methodc, methodv, str, strsize)
483
*str = NUL; /* make sure we return a NUL terminated string. */
488
for (i = 0; i < methodc; ++i)
489
strused += snprintfn(&str[strused], strsize - strused, "%s, ",
490
method2string(methodv[i]));
496
string2method(methodname)
497
const char *methodname;
503
{ AUTHMETHOD_NONEs, AUTHMETHOD_NONE },
504
{ AUTHMETHOD_UNAMEs, AUTHMETHOD_UNAME },
505
{ AUTHMETHOD_RFC931s, AUTHMETHOD_RFC931 },
506
{ AUTHMETHOD_PAMs, AUTHMETHOD_PAM }
510
for (i = 0; i < ELEMENTS(method); ++i)
511
if (strcmp(method[i].methodname, methodname) == 0)
512
return method[i].method;
519
sockshost2string(host, string, len)
520
const struct sockshost_t *host;
525
if (string == NULL) { /* to ease debugging. */
526
static char hstring[MAXSOCKSHOSTSTRING];
529
len = sizeof(hstring);
532
switch (host->atype) {
533
case SOCKS_ADDR_IPV4:
534
snprintfn(string, len, "%s.%d",
535
inet_ntoa(host->addr.ipv4), ntohs(host->port));
538
case SOCKS_ADDR_IPV6:
539
snprintfn(string, len, "%s.%d",
540
"<IPv6 address not supported>", ntohs(host->port));
543
case SOCKS_ADDR_DOMAIN: {
546
snprintfn(string, len, "%s.%d",
547
strcheck(name = str2vis(host->addr.domain, strlen(host->addr.domain))),
562
sockaddr2string(address, string, len)
563
const struct sockaddr *address;
569
if (string == NULL) {
570
static char addrstring[MAXSOCKADDRSTRING];
573
len = sizeof(addrstring);
576
switch (address->sa_family) {
578
/* LINTED pointer casts may be troublesome */
579
const struct sockaddr_un *addr = (const struct sockaddr_un *)address;
581
strncpy(string, addr->sun_path, len - 1);
582
string[len - 1] = NUL;
587
/* LINTED pointer casts may be troublesome */
588
const struct sockaddr_in *addr = TOCIN(address);
590
snprintfn(string, len, "%s.%d",
591
inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
596
SERRX(address->sa_family);
603
string2udpheader(data, len, header)
606
struct udpheader_t *header;
609
bzero(header, sizeof(*header));
611
if (len < sizeof(header->flag))
613
memcpy(&header->flag, data, sizeof(header->flag));
614
data += sizeof(header->flag);
615
len -= sizeof(header->flag);
617
if (len < sizeof(header->frag))
619
memcpy(&header->frag, data, sizeof(header->frag));
620
data += sizeof(header->frag);
621
len -= sizeof(header->frag);
623
if ((data = (const char *)mem2sockshost(&header->host,
624
(const unsigned char *)data, len, SOCKS_V5)) == NULL)
631
extensions2string(extensions, str, strsize)
632
const struct extension_t *extensions;
639
*str = NUL; /* make sure we return a NUL terminated string. */
645
if (extensions->bind)
646
strused += snprintfn(&str[strused], strsize - strused, "%s, ",
656
logtypes2string(logtypes, str, strsize)
657
const struct logtype_t *logtypes;
665
*str = NUL; /* make sure we return a NUL terminated string. */
671
if (logtypes->type & LOGTYPE_SYSLOG)
672
strused += snprintfn(&str[strused], strsize - strused, "\"syslog.%s\", ",
673
logtypes->facilityname);
675
if (logtypes->type & LOGTYPE_FILE)
676
for (i = 0; i < logtypes->fpc; ++i)
677
strused += snprintfn(&str[strused], strsize - strused, "\"%s\", ",
678
logtypes->fnamev[i]);
684
options2string(options, prefix, str, strsize)
685
const struct option_t *options;
693
*str = NUL; /* make sure we return a NUL terminated string. */
699
strused += snprintfn(&str[strused], strsize - strused,
700
"\"%sconfigfile\": \"%s\",\n", prefix, options->configfile == NULL ?
701
SOCKD_CONFIGFILE : options->configfile);
703
strused += snprintfn(&str[strused], strsize - strused,
704
"\"%sdaemon\": \"%d\",\n", prefix, options->daemon);
706
strused += snprintfn(&str[strused], strsize - strused,
707
"\"%sdebug\": \"%d\",\n", prefix, options->debug);
709
strused += snprintfn(&str[strused], strsize - strused,
710
"\"%skeepalive\": \"%d\",\n", prefix, options->keepalive);
712
strused += snprintfn(&str[strused], strsize - strused,
713
"\"%slinebuffer\": \"%d\",\n", prefix, options->debug);
715
strused += snprintfn(&str[strused], strsize - strused,
716
"\"%sservercount\": \"%d\",\n", prefix, options->serverc);
723
logs2string(logs, str, strsize)
724
const struct log_t *logs;
731
*str = NUL; /* make sure we return a NUL terminated string. */
738
strused += snprintfn(&str[strused], strsize - strused, "%s, ",
739
QUOTE(LOG_CONNECTs));
741
if (logs->disconnect)
742
strused += snprintfn(&str[strused], strsize - strused, "%s, ",
743
QUOTE(LOG_DISCONNECTs));
746
strused += snprintfn(&str[strused], strsize - strused, "%s, ",
750
strused += snprintfn(&str[strused], strsize - strused, "%s, ",
753
if (logs->iooperation)
754
strused += snprintfn(&str[strused], strsize - strused, "%s, ",
755
QUOTE(LOG_IOOPERATIONs));
761
childtype2string(type)
772
case CHILD_NEGOTIATE:
786
verdict2string(verdict)
790
return verdict == VERDICT_PASS ?
791
QUOTE(VERDICT_PASSs) : QUOTE(VERDICT_BLOCKs);
795
users2string(user, str, strsize)
796
const struct linkedname_t *user;
803
*str = NUL; /* make sure we return a NUL terminated string. */
809
for (; user != NULL; user = user->next)
810
strused += snprintfn(&str[strused], strsize - strused, "\"%s\", ",
817
compats2string(compats, str, strsize)
818
const struct compat_t *compats;
825
*str = NUL; /* make sure we return a NUL terminated string. */
831
if (compats->reuseaddr)
832
strused += snprintfn(&str[strused], strsize - strused, "%s, ",
835
if (compats->sameport)
836
strused += snprintfn(&str[strused], strsize - strused, "%s, ",
843
srchosts2string(srchost, prefix, str, strsize)
844
const struct srchost_t *srchost;
852
*str = NUL; /* make sure we return a NUL terminated string. */
858
strused += snprintfn(&str[strused], strsize - strused,
859
"\"%snomismatch\": \"%d\",\n", prefix, srchost->nomismatch);
861
strused += snprintfn(&str[strused], strsize - strused,
862
"\"%snounknown\": \"%d\",\n", prefix, srchost->nounknown);
873
if ((pw = getpwuid(uid)) == NULL)
880
userids2string(userids, prefix, str, strsize)
881
const struct userid_t *userids;
889
*str = NUL; /* make sure we return a NUL terminated string. */
895
strused += snprintfn(&str[strused], strsize - strused,
896
"\"%sprivileged\": \"%s\",\n", prefix, uid2name(userids->privileged));
898
strused += snprintfn(&str[strused], strsize - strused,
899
"\"%sunprivileged\": \"%s\",\n", prefix, uid2name(userids->unprivileged));
901
strused += snprintfn(&str[strused], strsize - strused,
902
"\"%slibwrap\": \"%s\",\n", prefix, uid2name(userids->libwrap));
908
timeouts2string(timeouts, prefix, str, strsize)
909
const struct timeout_t *timeouts;
917
*str = NUL; /* make sure we return a NUL terminated string. */
923
strused += snprintfn(&str[strused], strsize - strused,
924
"\"%snegotiate\": \"%ld\",\n", prefix, (long)timeouts->negotiate);
926
strused += snprintfn(&str[strused], strsize - strused,
927
"\"%sio\": \"%ld\",\n", prefix, (long)timeouts->io);
933
rotation2string(rotation)
951
#endif /* SOCKS_SERVER */