Skip to content

Commit

Permalink
new is_unix field to create listen unix sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
yrutschle committed Dec 22, 2024
1 parent 59d89e3 commit bf08229
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 10 deletions.
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
vNEXT:
Added a boolean setting "is_unix" for listen and
protocol entries. This will use the 'host' setting
as a path name to a socket file, and connections
(listening or connecting) will be performed on Unix
socket instead of Internet sockets.

v2.1.3:
Fix Landlock access to /etc/hosts.deny and
/etc/hosts.allow.
Expand Down
37 changes: 35 additions & 2 deletions common.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ static int start_listen_inet(struct listen_endpoint *sockfd[], int num_addr, str
*sockfd = realloc(*sockfd, num_addr * sizeof(*sockfd[0]));
(*sockfd)[num_addr-1].socketfd = listen_single_addr(addr, cfg->keepalive, cfg->is_udp);
(*sockfd)[num_addr-1].type = cfg->is_udp ? SOCK_DGRAM : SOCK_STREAM;
(*sockfd)[num_addr-1].family = AF_INET;
print_message(msg_config, "%d:\t%s\t[%s] [%s]\n", (*sockfd)[num_addr-1].socketfd, sprintaddr(buf, sizeof(buf), addr),
cfg->keepalive ? "keepalive" : "",
cfg->is_udp ? "udp" : "");
Expand All @@ -186,6 +187,31 @@ static int start_listen_inet(struct listen_endpoint *sockfd[], int num_addr, str
return num_addr;
}

/* Same, but for UNIX sockets */
static int start_listen_unix(struct listen_endpoint *sockfd[], int num_addr, struct sslhcfg_listen_item* cfg)
{
int fd = socket(AF_UNIX, cfg->is_udp ? SOCK_DGRAM : SOCK_STREAM, 0);
CHECK_RES_DIE(fd, "socket(AF_UNIX)");

struct sockaddr_un sun;
sun.sun_family = AF_UNIX;
strncpy(sun.sun_path, cfg->host, sizeof(sun.sun_path)-1);
printf("binding [%s]\n", sun.sun_path);
int res = bind(fd, (struct sockaddr*)&sun, sizeof(sun));
CHECK_RES_DIE(res, "bind(AF_UNIX)");

res = listen(fd, 50);

num_addr++;
*sockfd = realloc(*sockfd, num_addr * sizeof(*sockfd[0]));
(*sockfd)[num_addr-1].socketfd = fd;
(*sockfd)[num_addr-1].type = cfg->is_udp ? SOCK_DGRAM : SOCK_STREAM;
(*sockfd)[num_addr-1].family = AF_INET;

return num_addr;
}


/* Starts listening sockets on specified addresses.
* OUT: *sockfd[] pointer to newly-allocated array of listen_endpoint objects
* Returns number of addresses bound
Expand All @@ -206,7 +232,11 @@ int start_listen_sockets(struct listen_endpoint *sockfd[])
print_message(msg_config, "Listening to:\n");

for (i = 0; i < cfg.listen_len; i++) {
num_addr = start_listen_inet(sockfd, num_addr, &cfg.listen[i]);
if (cfg.listen[i].is_unix) {
num_addr = start_listen_unix(sockfd, num_addr, &cfg.listen[i]);
} else {
num_addr = start_listen_inet(sockfd, num_addr, &cfg.listen[i]);
}
}

return num_addr;
Expand Down Expand Up @@ -425,7 +455,7 @@ static int connect_unix(struct connection *cnx, int fd_from, connect_blocking bl

int fd = socket(AF_UNIX, SOCK_STREAM, 0);
sun->sun_family = AF_UNIX;
strcpy(sun->sun_path, cnx->proto->host);
strncpy(sun->sun_path, cnx->proto->host, sizeof(sun->sun_path)-1);

int res = connect(fd, (struct sockaddr*)sun, sizeof(*sun));
CHECK_RES_RETURN(res, "connect", res);
Expand Down Expand Up @@ -588,6 +618,9 @@ char* sprintaddr(char* buf, size_t size, struct addrinfo *a)
char host[NI_MAXHOST], serv[NI_MAXSERV];
int res;

memset(host, 0, sizeof(host));
memset(serv, 0, sizeof(serv));

res = getnameinfo(a->ai_addr, a->ai_addrlen,
host, sizeof(host),
serv, sizeof(serv),
Expand Down
1 change: 1 addition & 0 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ struct connection {
struct listen_endpoint {
int socketfd; /* file descriptor of listening socket */
int type; /* SOCK_DGRAM | SOCK_STREAM */
int family; /* AF_INET | AF_UNIX */
};

#define FD_CNXCLOSED 0
Expand Down
2 changes: 1 addition & 1 deletion echosrv-conf.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Generated by conf2struct (https://www.rutschle.net/tech/conf2struct/README)
* on Sun Dec 22 00:05:31 2024.
* on Sun Dec 22 22:40:51 2024.
# conf2struct: generate libconf parsers that read to structs
# Copyright (C) 2018-2024 Yves Rutschle
Expand Down
2 changes: 1 addition & 1 deletion echosrv-conf.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Generated by conf2struct (https://www.rutschle.net/tech/conf2struct/README)
* on Sun Dec 22 00:05:31 2024.
* on Sun Dec 22 22:40:51 2024.
# conf2struct: generate libconf parsers that read to structs
# Copyright (C) 2018-2024 Yves Rutschle
Expand Down
3 changes: 2 additions & 1 deletion example.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ listen:
(
{ host: "thelonious"; port: "443"; },
{ host: "thelonious"; port: "8080"; keepalive: true; },
{ host: "thelonious"; is_udp: true; port: "443" }
{ host: "thelonious"; is_udp: true; port: "443"; },
{ host: "/tmp/unix_socket"; is_unix: true; port: ""; }
);

# List of protocols
Expand Down
23 changes: 21 additions & 2 deletions sslh-conf.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Generated by conf2struct (https://www.rutschle.net/tech/conf2struct/README)
* on Sun Dec 22 16:13:50 2024.
* on Sun Dec 22 22:40:51 2024.
# conf2struct: generate libconf parsers that read to structs
# Copyright (C) 2018-2024 Yves Rutschle
Expand Down Expand Up @@ -777,7 +777,7 @@ static struct config_desc table_sslhcfg_protocols[] = {
},
{ 0 }
};

static struct config_desc table_sslhcfg_listen[] = {


Expand Down Expand Up @@ -829,6 +829,22 @@ static struct config_desc table_sslhcfg_listen[] = {
/* default_val*/ .default_val.def_bool = 0
},

{
/* name */ "is_unix",
/* type */ CFG_BOOL,
/* sub_group*/ NULL,
/* arg_cl */ NULL,
/* base_addr */ NULL,
/* offset */ offsetof(struct sslhcfg_listen_item, is_unix),
/* offset_len */ 0,
/* offset_present */ 0,
/* size */ sizeof(int),
/* array_type */ -1,
/* mandatory */ 0,
/* optional */ 0,
/* default_val*/ .default_val.def_bool = 0
},

{
/* name */ "keepalive",
/* type */ CFG_BOOL,
Expand Down Expand Up @@ -2429,6 +2445,9 @@ static void sslhcfg_listen_fprint(
fprintf(out, "is_udp: %d", sslhcfg_listen->is_udp);
fprintf(out, "\n");
indent(out, depth);
fprintf(out, "is_unix: %d", sslhcfg_listen->is_unix);
fprintf(out, "\n");
indent(out, depth);
fprintf(out, "keepalive: %d", sslhcfg_listen->keepalive);
fprintf(out, "\n");
}
Expand Down
3 changes: 2 additions & 1 deletion sslh-conf.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Generated by conf2struct (https://www.rutschle.net/tech/conf2struct/README)
* on Sun Dec 22 16:13:50 2024.
* on Sun Dec 22 22:40:51 2024.
# conf2struct: generate libconf parsers that read to structs
# Copyright (C) 2018-2024 Yves Rutschle
Expand Down Expand Up @@ -44,6 +44,7 @@ struct sslhcfg_listen_item {
char* host;
char* port;
int is_udp;
int is_unix;
int keepalive;
};

Expand Down
1 change: 1 addition & 0 deletions sslhconf.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ config: {
{ name: "host"; type: "string"; var: true; },
{ name: "port"; type: "string"; var: true; },
{ name: "is_udp"; type: "bool"; default: false },
{ name: "is_unix"; type: "bool"; default: false },
{ name: "keepalive"; type: "bool"; default: false; }
)
},
Expand Down
3 changes: 2 additions & 1 deletion test.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ listen:
(
{ host: "localhost"; port: "8080"; keepalive: true; },
{ host: "localhost"; port: "8081"; keepalive: true; },
{ host: "ip4-localhost"; is_udp: true; port: "8086"; }
{ host: "ip4-localhost"; is_udp: true; port: "8086"; },
{ host: "/tmp/sslh.sock"; is_unix: true; port: ""; }
);


Expand Down
2 changes: 1 addition & 1 deletion version.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef VERSION_H
#define VERSION_H

#define VERSION "v2.1.4-22-g9e6b4fa-dirty"
#define VERSION "v2.1.4-24-g59d89e3-dirty"
#endif

0 comments on commit bf08229

Please sign in to comment.