Skip to content

Commit 6017f59

Browse files
committed
server config UPDATE cmake configurable unixsock basepath
1 parent c90b82d commit 6017f59

File tree

9 files changed

+83
-33
lines changed

9 files changed

+83
-33
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ set(MAX_PSPOLL_THREAD_COUNT 6 CACHE STRING "Maximum number of threads that could
100100
set(TIMEOUT_STEP 100 CACHE STRING "Number of microseconds tasks are repeated until timeout elapses")
101101
set(YANG_MODULE_DIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATADIR}/yang/modules/libnetconf2" CACHE STRING "Directory where to copy the YANG modules to")
102102
set(CLIENT_SEARCH_DIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATADIR}/yang/modules" CACHE STRING "Default NC client YANG module search directory")
103+
set(UNIX_SOCKET_DIR "/tmp" CACHE PATH "Base directory for UNIX sockets")
103104

104105
#
105106
# sources

src/config.h.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@
8080
*/
8181
#define NC_TIMEOUT_STEP @TIMEOUT_STEP@
8282

83+
/*
84+
* The default base directory for UNIX sockets.
85+
*/
86+
#define NC_UNIX_SOCKET_DIR "@UNIX_SOCKET_DIR@"
87+
8388
/* Portability feature-check macros. */
8489
#cmakedefine HAVE_PTHREAD_RWLOCKATTR_SETKIND_NP
8590

src/proxy_unix.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,15 @@ nc_proxy_unix_connect(const char *address, const char *username)
3636
struct sockaddr_un sun;
3737
struct passwd *pw, pw_buf;
3838
int sock = -1;
39-
char *buf = NULL;
39+
char *buf = NULL, *sock_path = NULL;
4040
size_t buf_size = 0;
4141

42+
/* construct the path to the UNIX socket */
43+
if (asprintf(&sock_path, "%s/%s", NC_UNIX_SOCKET_DIR, address) == -1) {
44+
ERRMEM;
45+
goto error;
46+
}
47+
4248
/* connect to the UNIX socket */
4349
sock = socket(AF_UNIX, SOCK_STREAM, 0);
4450
if (sock < 0) {
@@ -48,10 +54,10 @@ nc_proxy_unix_connect(const char *address, const char *username)
4854

4955
memset(&sun, 0, sizeof(sun));
5056
sun.sun_family = AF_UNIX;
51-
snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", address);
57+
snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", sock_path);
5258

5359
if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
54-
ERR(NULL, "Cannot connect to sock server %s (%s)", address, strerror(errno));
60+
ERR(NULL, "Cannot connect to sock server %s (%s)", sock_path, strerror(errno));
5561
goto error;
5662
}
5763

@@ -76,13 +82,15 @@ nc_proxy_unix_connect(const char *address, const char *username)
7682
}
7783

7884
free(buf);
85+
free(sock_path);
7986
return sock;
8087

8188
error:
8289
if (sock > -1) {
8390
close(sock);
8491
}
8592
free(buf);
93+
free(sock_path);
8694
return -1;
8795
}
8896

src/server_config.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ nc_server_config_free(struct nc_server_config *config)
360360
struct nc_ch_client *ch_client;
361361
struct nc_ch_endpt *ch_endpt;
362362
LY_ARRAY_COUNT_TYPE i, j;
363-
const char *socket_path = NULL;
363+
char *socket_path = NULL;
364364

365365
if (!config) {
366366
return;
@@ -397,6 +397,11 @@ nc_server_config_free(struct nc_server_config *config)
397397
}
398398
LY_ARRAY_FREE(endpt->binds);
399399

400+
if (endpt->ti == NC_TI_UNIX) {
401+
free(socket_path);
402+
socket_path = NULL;
403+
}
404+
400405
/* free transport specific options */
401406
switch (endpt->ti) {
402407
#ifdef NC_ENABLED_SSH_TLS
@@ -5035,7 +5040,8 @@ static int
50355040
nc_server_config_bindings_match(const struct nc_endpt *e1, const struct nc_bind *b1,
50365041
const struct nc_endpt *e2, const struct nc_bind *b2)
50375042
{
5038-
const char *addr1, *addr2;
5043+
int rc = 1;
5044+
char *addr1 = NULL, *addr2 = NULL;
50395045

50405046
if (e1->ti != e2->ti) {
50415047
/* different transport protocols */
@@ -5052,15 +5058,22 @@ nc_server_config_bindings_match(const struct nc_endpt *e1, const struct nc_bind
50525058
}
50535059
if (!addr1 || !addr2) {
50545060
/* unable to get the address */
5055-
return 0;
5061+
rc = 0;
5062+
goto cleanup;
50565063
}
50575064

50585065
if (strcmp(addr1, addr2) || (b1->port != b2->port)) {
50595066
/* different addresses or ports */
5060-
return 0;
5067+
rc = 0;
5068+
goto cleanup;
50615069
}
50625070

5063-
return 1;
5071+
cleanup:
5072+
if (e1->ti == NC_TI_UNIX) {
5073+
free(addr1);
5074+
free(addr2);
5075+
}
5076+
return rc;
50645077
}
50655078

50665079
/**

src/session_client.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,13 +1448,19 @@ nc_connect_unix(const char *address, struct ly_ctx *ctx)
14481448
struct nc_session *session = NULL;
14491449
struct sockaddr_un sun;
14501450
struct passwd *pw, pw_buf;
1451-
char *username;
1451+
char *username, *sock_path = NULL;
14521452
int sock = -1;
14531453
char *buf = NULL;
14541454
size_t buf_size = 0;
14551455

14561456
NC_CHECK_ARG_RET(NULL, address, NULL);
14571457

1458+
/* construct the path to the UNIX socket */
1459+
if (asprintf(&sock_path, "%s/%s", NC_UNIX_SOCKET_DIR, address) == -1) {
1460+
ERRMEM;
1461+
goto fail;
1462+
}
1463+
14581464
sock = socket(AF_UNIX, SOCK_STREAM, 0);
14591465
if (sock < 0) {
14601466
ERR(NULL, "Failed to create socket (%s).", strerror(errno));
@@ -1463,10 +1469,10 @@ nc_connect_unix(const char *address, struct ly_ctx *ctx)
14631469

14641470
memset(&sun, 0, sizeof(sun));
14651471
sun.sun_family = AF_UNIX;
1466-
snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", address);
1472+
snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", sock_path);
14671473

14681474
if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
1469-
ERR(NULL, "Cannot connect to sock server %s (%s)", address, strerror(errno));
1475+
ERR(NULL, "Cannot connect to sock server %s (%s)", sock_path, strerror(errno));
14701476
goto fail;
14711477
}
14721478

@@ -1485,8 +1491,9 @@ nc_connect_unix(const char *address, struct ly_ctx *ctx)
14851491
session->ti.unixsock.sock = sock;
14861492
sock = -1;
14871493

1488-
/* socket path */
1489-
session->path = strdup(address);
1494+
/* transfer socket path ownership to session */
1495+
session->path = sock_path;
1496+
sock_path = NULL;
14901497

14911498
/* NETCONF username */
14921499
if (unix_opts.username) {
@@ -1536,6 +1543,7 @@ nc_connect_unix(const char *address, struct ly_ctx *ctx)
15361543
if (sock >= 0) {
15371544
close(sock);
15381545
}
1546+
free(sock_path);
15391547
return NULL;
15401548
}
15411549

src/session_p.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,10 +977,13 @@ void *nc_realloc(void *ptr, size_t size);
977977
/**
978978
* @brief Get the UNIX socket path for the given endpoint.
979979
*
980+
* Finds the socket filename based on the @p endpt and then appends it
981+
* to ::NC_UNIX_SOCKET_PATH_DIR.
982+
*
980983
* @param[in] endpt Endpoint to get the socket path for.
981984
* @return Socket path, NULL on error.
982985
*/
983-
const char *nc_server_unix_get_socket_path(const struct nc_endpt *endpt);
986+
char *nc_server_unix_get_socket_path(const struct nc_endpt *endpt);
984987

985988
/**
986989
* @brief Bind and listen on a socket for the given endpoint and its bind.

src/session_server.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -358,31 +358,38 @@ nc_sock_listen_inet(const char *address, uint16_t port)
358358
return -1;
359359
}
360360

361-
const char *
361+
char *
362362
nc_server_unix_get_socket_path(const struct nc_endpt *endpt)
363363
{
364364
LY_ARRAY_COUNT_TYPE i;
365-
const char *path = NULL;
365+
const char *filename = NULL;
366+
char *path = NULL;
366367

367368
/* check the endpoints options for type of socket path */
368369
if (endpt->opts.unix->path_type == NC_UNIX_SOCKET_PATH_FILE) {
369370
/* UNIX socket endpoints always have only one bind, get its address */
370-
path = endpt->binds[0].address;
371+
filename = endpt->binds[0].address;
371372
} else if (endpt->opts.unix->path_type == NC_UNIX_SOCKET_PATH_HIDDEN) {
372-
/* serach the mappings */
373+
/* search the mappings */
373374
LY_ARRAY_FOR(server_opts.unix_paths, i) {
374375
if (!strcmp(server_opts.unix_paths[i].endpt_name, endpt->name)) {
375-
path = server_opts.unix_paths[i].path;
376+
filename = server_opts.unix_paths[i].path;
376377
break;
377378
}
378379
}
379-
if (!path) {
380+
if (!filename) {
380381
ERR(NULL, "UNIX socket path mapping for endpoint \"%s\" not found.", endpt->name);
381382
}
382383
} else {
383384
ERRINT;
384385
}
385386

387+
/* construct the full path */
388+
if (asprintf(&path, "%s/%s", NC_UNIX_SOCKET_DIR, filename) == -1) {
389+
ERRMEM;
390+
return NULL;
391+
}
392+
386393
return path;
387394
}
388395

@@ -543,7 +550,7 @@ nc_sock_accept_binds(struct nc_endpt *endpt, struct nc_bind *binds, uint16_t bin
543550
pthread_mutex_t *bind_lock, int timeout, char **host, uint16_t *port, uint16_t *idx, int *sock)
544551
{
545552
uint16_t i, j, pfd_count, client_port;
546-
char *client_address;
553+
char *client_address, *sockpath = NULL;
547554
struct pollfd *pfd;
548555
struct sockaddr_storage saddr;
549556
socklen_t saddr_len = sizeof(saddr);
@@ -648,7 +655,11 @@ nc_sock_accept_binds(struct nc_endpt *endpt, struct nc_bind *binds, uint16_t bin
648655
}
649656

650657
if (saddr.ss_family == AF_UNIX) {
651-
VRB(NULL, "Accepted a connection on %s.", endpt ? nc_server_unix_get_socket_path(endpt) : "UNIX socket");
658+
if (endpt) {
659+
sockpath = nc_server_unix_get_socket_path(endpt);
660+
}
661+
VRB(NULL, "Accepted a connection on %s.", sockpath ? sockpath : "UNIX socket");
662+
free(sockpath);
652663
} else {
653664
VRB(NULL, "Accepted a connection on %s:%u from %s:%u.", binds[i].address, binds[i].port, client_address, client_port);
654665
}
@@ -2335,7 +2346,7 @@ nc_ps_clear(struct nc_pollsession *ps, int all, void (*data_free)(void *))
23352346
int
23362347
nc_server_bind_and_listen(struct nc_endpt *endpt, struct nc_bind *bind)
23372348
{
2338-
const char *unix_path = NULL;
2349+
char *unix_path = NULL;
23392350
int sock = -1, rc = 0;
23402351

23412352
/* start listening on the endpoint */
@@ -2378,6 +2389,7 @@ nc_server_bind_and_listen(struct nc_endpt *endpt, struct nc_bind *bind)
23782389
}
23792390

23802391
cleanup:
2392+
free(unix_path);
23812393
return rc;
23822394
}
23832395

tests/test_config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@ setup_f(void **state)
962962
assert_int_equal(ret, 0);
963963

964964
/* set hidden path for UNIX endpoint */
965-
ret = nc_server_set_unix_socket_path("unix", "/tmp/netconf-test-server.sock");
965+
ret = nc_server_set_unix_socket_path("unix", "netconf-test-server.sock");
966966
assert_int_equal(ret, 0);
967967

968968
lyd_free_all(tree);

tests/test_unix_socket.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ setup_glob_f(void **state)
5555
test_ctx->free_test_data = ln2_glob_test_free_test_data;
5656

5757
/* set two hidden paths for UNIX sockets */
58-
ret = nc_server_set_unix_socket_path("unix", "/tmp/nc2_test_unix_sock");
58+
ret = nc_server_set_unix_socket_path("unix", "nc2_test_unix_sock");
5959
assert_int_equal(ret, 0);
60-
ret = nc_server_set_unix_socket_path("unix2", "/tmp/nc2_test_unix_sock2");
60+
ret = nc_server_set_unix_socket_path("unix2", "nc2_test_unix_sock2");
6161
assert_int_equal(ret, 0);
6262

6363
return 0;
@@ -126,7 +126,7 @@ test_connect(void **state)
126126

127127
assert_non_null(state);
128128

129-
data->socket_path = "/tmp/nc2_test_unix_sock";
129+
data->socket_path = "nc2_test_unix_sock";
130130
ret = pthread_create(&tids[0], NULL, test_unix_client_thread, *state);
131131
assert_int_equal(ret, 0);
132132
ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, *state);
@@ -149,7 +149,7 @@ test_invalid_user(void **state)
149149
assert_non_null(state);
150150

151151
/* set invalid username, the server will reject it */
152-
data->socket_path = "/tmp/nc2_test_unix_sock";
152+
data->socket_path = "nc2_test_unix_sock";
153153
data->expect_fail = 1;
154154
data->username = "INVALID";
155155

@@ -182,7 +182,7 @@ proxy_client_thread(void *arg)
182182
pthread_barrier_wait(&test_ctx->barrier);
183183

184184
/* connect the proxy */
185-
fd = nc_proxy_unix_connect("/tmp/nc2_test_unix_sock", NULL);
185+
fd = nc_proxy_unix_connect("nc2_test_unix_sock", NULL);
186186
assert_int_not_equal(fd, 0);
187187

188188
/* send the hello message */
@@ -251,14 +251,14 @@ auth_client_thread(void *arg)
251251
pthread_barrier_wait(&test_ctx->barrier);
252252

253253
/* session fails to be created with the default username */
254-
session = nc_connect_unix("/tmp/nc2_test_unix_sock", NULL);
254+
session = nc_connect_unix("nc2_test_unix_sock", NULL);
255255
assert_null(session);
256256

257257
/* set the expected username */
258258
nc_client_unix_set_username("auth_user");
259259

260260
/* session created */
261-
session = nc_connect_unix("/tmp/nc2_test_unix_sock", NULL);
261+
session = nc_connect_unix("nc2_test_unix_sock", NULL);
262262
assert_non_null(session);
263263

264264
/* free the session */
@@ -424,14 +424,14 @@ test_cleartext_path(void **state)
424424

425425
/* create the UNIX socket with a different cleartext path */
426426
ret = nc_server_config_add_unix_socket(test_ctx->ctx,
427-
"unix2", "/tmp/nc2_test_cleartext_unix_sock", "0666", NULL, NULL, &config);
427+
"unix2", "nc2_test_cleartext_unix_sock", "0666", NULL, NULL, &config);
428428
assert_int_equal(ret, 0);
429429

430430
ret = nc_server_config_setup_data(config);
431431
assert_int_equal(ret, 0);
432432

433433
/* start the client and server threads, the client should be able to connect to the cleartext path */
434-
data->socket_path = "/tmp/nc2_test_cleartext_unix_sock";
434+
data->socket_path = "nc2_test_cleartext_unix_sock";
435435
ret = pthread_create(&tid[0], NULL, test_unix_client_thread, *state);
436436
assert_int_equal(ret, 0);
437437
ret = pthread_create(&tid[1], NULL, ln2_glob_test_server_thread, *state);

0 commit comments

Comments
 (0)