Skip to content

Commit fb3f7cf

Browse files
romanmichalvasko
authored andcommitted
session server UPDATE add UNIX socket api
Removed unix socket from configuration and moved it back to API.
1 parent cab602e commit fb3f7cf

File tree

9 files changed

+247
-309
lines changed

9 files changed

+247
-309
lines changed

doc/libnetconf.doc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,9 @@
307307
*
308308
* - ::nc_server_set_capab_withdefaults()
309309
* - ::nc_server_set_capability()
310+
* - ::nc_server_endpt_count()
311+
* - ::nc_server_add_endpt_unix_socket_listen()
312+
* - ::nc_server_del_endpt_unix_socket()
310313
*
311314
* Server Configuration
312315
* ===

modules/[email protected]

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -375,38 +375,6 @@ module libnetconf2-netconf-server {
375375
uses keyboard-interactive-grouping;
376376
}
377377

378-
augment "/ncs:netconf-server/ncs:listen/ncs:endpoint/ncs:transport" {
379-
case unix-socket {
380-
container unix-socket {
381-
description
382-
"UNIX socket listening configuration for inbound connections.";
383-
leaf path {
384-
type string;
385-
mandatory true;
386-
description
387-
"Path to the socket on which the communication will occur.";
388-
}
389-
leaf mode {
390-
type string {
391-
pattern '[0124567]{3}';
392-
}
393-
description
394-
"Mode of the socket.";
395-
}
396-
leaf uid {
397-
type uint16;
398-
description
399-
"User ID of the socket.";
400-
}
401-
leaf gid {
402-
type uint16;
403-
description
404-
"Group ID of the socket.";
405-
}
406-
}
407-
}
408-
}
409-
410378
augment "/ncs:netconf-server/ncs:listen/ncs:endpoint/ncs:transport/ncs:ssh" +
411379
"/ncs:ssh/ncs:ssh-server-parameters/ncs:client-authentication" {
412380
uses endpoint-reference-grouping;

src/server_config.c

Lines changed: 4 additions & 192 deletions
Original file line numberDiff line numberDiff line change
@@ -770,42 +770,6 @@ nc_server_config_del_endpt_ssh(struct nc_endpt *endpt, struct nc_bind *bind)
770770
}
771771
}
772772

773-
#endif /* NC_ENABLED_SSH_TLS */
774-
775-
void
776-
nc_server_config_del_unix_socket_opts(struct nc_bind *bind, struct nc_server_unix_opts *opts)
777-
{
778-
if (bind->sock > -1) {
779-
close(bind->sock);
780-
}
781-
782-
unlink(bind->address);
783-
free(bind->address);
784-
free(opts->address);
785-
786-
free(opts);
787-
}
788-
789-
void
790-
nc_server_config_del_endpt_unix_socket(struct nc_endpt *endpt, struct nc_bind *bind)
791-
{
792-
free(endpt->name);
793-
nc_server_config_del_unix_socket_opts(bind, endpt->opts.unixsock);
794-
795-
server_opts.endpt_count--;
796-
if (!server_opts.endpt_count) {
797-
free(server_opts.endpts);
798-
free(server_opts.binds);
799-
server_opts.endpts = NULL;
800-
server_opts.binds = NULL;
801-
} else if (endpt != &server_opts.endpts[server_opts.endpt_count]) {
802-
memcpy(endpt, &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts);
803-
memcpy(bind, &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds);
804-
}
805-
}
806-
807-
#ifdef NC_ENABLED_SSH_TLS
808-
809773
static void
810774
nc_server_config_del_cert(struct nc_cert_grouping *certs, struct nc_certificate *cert)
811775
{
@@ -1060,7 +1024,7 @@ nc_server_config_listen(const struct lyd_node *node, NC_OPERATION op)
10601024
break;
10611025
#endif /* NC_ENABLED_SSH_TLS */
10621026
case NC_TI_UNIX:
1063-
nc_server_config_del_endpt_unix_socket(&server_opts.endpts[i], &server_opts.binds[i]);
1027+
_nc_server_del_endpt_unix_socket(&server_opts.endpts[i], &server_opts.binds[i]);
10641028
break;
10651029
case NC_TI_NONE:
10661030
case NC_TI_FD:
@@ -1234,7 +1198,7 @@ nc_server_config_endpoint(const struct lyd_node *node, NC_OPERATION op)
12341198
break;
12351199
#endif /* NC_ENABLED_SSH_TLS */
12361200
case NC_TI_UNIX:
1237-
nc_server_config_del_endpt_unix_socket(endpt, bind);
1201+
_nc_server_del_endpt_unix_socket(endpt, bind);
12381202
break;
12391203
case NC_TI_NONE:
12401204
case NC_TI_FD:
@@ -1429,73 +1393,6 @@ nc_server_config_tls(const struct lyd_node *node, NC_OPERATION op)
14291393
return ret;
14301394
}
14311395

1432-
#endif /* NC_ENABLED_SSH_TLS */
1433-
1434-
static int
1435-
nc_server_config_set_address_port(struct nc_endpt *endpt, struct nc_bind *bind, const char *address, uint16_t port)
1436-
{
1437-
int sock = -1, set_addr, ret = 0;
1438-
1439-
assert((address && !port) || (!address && port) || (endpt->ti == NC_TI_UNIX));
1440-
1441-
if (address) {
1442-
set_addr = 1;
1443-
} else {
1444-
set_addr = 0;
1445-
}
1446-
1447-
if (set_addr) {
1448-
port = bind->port;
1449-
} else {
1450-
address = bind->address;
1451-
}
1452-
1453-
/* we have all the information we need to create a listening socket */
1454-
if ((address && port) || (endpt->ti == NC_TI_UNIX)) {
1455-
/* create new socket, close the old one */
1456-
if (endpt->ti == NC_TI_UNIX) {
1457-
sock = nc_sock_listen_unix(endpt->opts.unixsock);
1458-
} else {
1459-
sock = nc_sock_listen_inet(address, port, &endpt->ka);
1460-
}
1461-
1462-
if (sock == -1) {
1463-
ret = 1;
1464-
goto cleanup;
1465-
}
1466-
1467-
if (bind->sock > -1) {
1468-
close(bind->sock);
1469-
}
1470-
bind->sock = sock;
1471-
}
1472-
1473-
if (sock > -1) {
1474-
switch (endpt->ti) {
1475-
case NC_TI_UNIX:
1476-
VRB(NULL, "Listening on %s for UNIX connections.", endpt->opts.unixsock->address);
1477-
break;
1478-
#ifdef NC_ENABLED_SSH_TLS
1479-
case NC_TI_LIBSSH:
1480-
VRB(NULL, "Listening on %s:%u for SSH connections.", address, port);
1481-
break;
1482-
case NC_TI_OPENSSL:
1483-
VRB(NULL, "Listening on %s:%u for TLS connections.", address, port);
1484-
break;
1485-
#endif /* NC_ENABLED_SSH_TLS */
1486-
default:
1487-
ERRINT;
1488-
ret = 1;
1489-
break;
1490-
}
1491-
}
1492-
1493-
cleanup:
1494-
return ret;
1495-
}
1496-
1497-
#ifdef NC_ENABLED_SSH_TLS
1498-
14991396
/* mandatory leaf */
15001397
static int
15011398
nc_server_config_local_address(const struct lyd_node *node, NC_OPERATION op)
@@ -1518,7 +1415,7 @@ nc_server_config_local_address(const struct lyd_node *node, NC_OPERATION op)
15181415
bind->address = strdup(lyd_get_value(node));
15191416
NC_CHECK_ERRMEM_GOTO(!bind->address, ret = 1, cleanup);
15201417

1521-
ret = nc_server_config_set_address_port(endpt, bind, lyd_get_value(node), 0);
1418+
ret = nc_server_set_address_port(endpt, bind, lyd_get_value(node), 0);
15221419
if (ret) {
15231420
goto cleanup;
15241421
}
@@ -1551,7 +1448,7 @@ nc_server_config_local_port(const struct lyd_node *node, NC_OPERATION op)
15511448
bind->port = 0;
15521449
}
15531450

1554-
ret = nc_server_config_set_address_port(endpt, bind, NULL, bind->port);
1451+
ret = nc_server_set_address_port(endpt, bind, NULL, bind->port);
15551452
if (ret) {
15561453
goto cleanup;
15571454
}
@@ -2790,89 +2687,6 @@ nc_server_config_mac_alg(const struct lyd_node *node, NC_OPERATION op)
27902687
return ret;
27912688
}
27922689

2793-
#endif /* NC_ENABLED_SSH_TLS */
2794-
2795-
static int
2796-
nc_server_config_create_unix_socket(struct nc_endpt *endpt)
2797-
{
2798-
endpt->ti = NC_TI_UNIX;
2799-
endpt->opts.unixsock = calloc(1, sizeof *endpt->opts.unixsock);
2800-
NC_CHECK_ERRMEM_RET(!endpt->opts.unixsock, 1);
2801-
2802-
/* set default values */
2803-
endpt->opts.unixsock->mode = -1;
2804-
endpt->opts.unixsock->uid = -1;
2805-
endpt->opts.unixsock->gid = -1;
2806-
2807-
return 0;
2808-
}
2809-
2810-
static int
2811-
nc_server_config_unix_socket(const struct lyd_node *node, NC_OPERATION op)
2812-
{
2813-
int ret = 0;
2814-
uint32_t log_options = 0;
2815-
struct nc_endpt *endpt;
2816-
struct nc_bind *bind;
2817-
struct nc_server_unix_opts *opts;
2818-
struct lyd_node *data = NULL;
2819-
2820-
assert(!strcmp(LYD_NAME(node), "unix-socket"));
2821-
2822-
if (nc_server_config_get_endpt(node, &endpt, &bind)) {
2823-
ret = 1;
2824-
goto cleanup;
2825-
}
2826-
2827-
if (op == NC_OP_CREATE) {
2828-
if (nc_server_config_create_unix_socket(endpt)) {
2829-
ret = 1;
2830-
goto cleanup;
2831-
}
2832-
2833-
opts = endpt->opts.unixsock;
2834-
2835-
lyd_find_path(node, "path", 0, &data);
2836-
assert(data);
2837-
2838-
opts->address = strdup(lyd_get_value(data));
2839-
bind->address = strdup(lyd_get_value(data));
2840-
NC_CHECK_ERRMEM_GOTO(!opts->address || !bind->address, ret = 1, cleanup);
2841-
2842-
/* silently search for non-mandatory parameters */
2843-
ly_temp_log_options(&log_options);
2844-
ret = lyd_find_path(node, "mode", 0, &data);
2845-
if (!ret) {
2846-
opts->mode = strtol(lyd_get_value(data), NULL, 8);
2847-
}
2848-
2849-
ret = lyd_find_path(node, "uid", 0, &data);
2850-
if (!ret) {
2851-
opts->uid = strtol(lyd_get_value(data), NULL, 10);
2852-
}
2853-
2854-
ret = lyd_find_path(node, "gid", 0, &data);
2855-
if (!ret) {
2856-
opts->gid = strtol(lyd_get_value(data), NULL, 10);
2857-
}
2858-
2859-
/* reset the logging options */
2860-
ly_temp_log_options(NULL);
2861-
2862-
ret = nc_server_config_set_address_port(endpt, bind, NULL, 0);
2863-
if (ret) {
2864-
goto cleanup;
2865-
}
2866-
} else if (op == NC_OP_DELETE) {
2867-
nc_server_config_del_unix_socket_opts(bind, endpt->opts.unixsock);
2868-
}
2869-
2870-
cleanup:
2871-
return ret;
2872-
}
2873-
2874-
#ifdef NC_ENABLED_SSH_TLS
2875-
28762690
static int
28772691
nc_server_config_check_endpt_reference_cycle(struct nc_endpt *original, struct nc_endpt *next)
28782692
{
@@ -4063,8 +3877,6 @@ nc_server_config_parse_netconf_server(const struct lyd_node *node, NC_OPERATION
40633877
ret = nc_server_config_hello_timeout(node, op);
40643878
} else if (!strcmp(name, "endpoint")) {
40653879
ret = nc_server_config_endpoint(node, op);
4066-
} else if (!strcmp(name, "unix-socket")) {
4067-
ret = nc_server_config_unix_socket(node, op);
40683880
}
40693881
#ifdef NC_ENABLED_SSH_TLS
40703882
else if (!strcmp(name, "ssh")) {

src/server_config.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -122,23 +122,6 @@ int nc_server_config_add_address_port(const struct ly_ctx *ctx, const char *endp
122122

123123
#endif /* NC_ENABLED_SSH_TLS */
124124

125-
/**
126-
* @brief Creates new YANG data nodes for a UNIX socket.
127-
*
128-
* @param[in] ctx libyang context.
129-
* @param[in] endpt_name Arbitrary identifier of the endpoint.
130-
* If an endpoint with this identifier already exists, its contents might be changed.
131-
* @param[in] path Path to the socket.
132-
* @param[in] mode New mode, use -1 for default.
133-
* @param[in] uid New uid, use -1 for default
134-
* @param[in] gid New gid, use -1 for default
135-
* @param[in,out] config Configuration YANG data tree. If *config is NULL, it will be created.
136-
* Otherwise the new YANG data will be added to the previous data and may override it.
137-
* @return 0 on success, non-zero otherwise.
138-
*/
139-
int nc_server_config_add_unix_socket(const struct ly_ctx *ctx, const char *endpt_name, const char *path,
140-
mode_t mode, uid_t uid, gid_t gid, struct lyd_node **config);
141-
142125
/**
143126
* @brief Deletes an endpoint from the YANG data.
144127
*

src/server_config_util.c

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,65 +1325,6 @@ nc_server_config_del_truststore_cert(const char *cert_bag_name,
13251325

13261326
#endif /* NC_ENABLED_SSH_TLS */
13271327

1328-
API int
1329-
nc_server_config_add_unix_socket(const struct ly_ctx *ctx, const char *endpt_name, const char *path,
1330-
mode_t mode, uid_t uid, gid_t gid, struct lyd_node **config)
1331-
{
1332-
int ret = 0;
1333-
char *tree_path = NULL;
1334-
char buf[12] = {0};
1335-
1336-
NC_CHECK_ARG_RET(NULL, ctx, endpt_name, path, config, 1);
1337-
1338-
ret = asprintf(&tree_path, "/ietf-netconf-server:netconf-server/listen/endpoint[name='%s']/libnetconf2-netconf-server:unix-socket", endpt_name);
1339-
NC_CHECK_ERRMEM_GOTO(ret == -1, tree_path = NULL; ret = 1, cleanup);
1340-
1341-
/* path to unix socket */
1342-
ret = nc_server_config_append(ctx, tree_path, "path", path, config);
1343-
if (ret) {
1344-
goto cleanup;
1345-
}
1346-
1347-
/* mode */
1348-
if (mode != (mode_t)-1) {
1349-
if (mode > 0777) {
1350-
ERR(NULL, "Invalid mode value (%o).", mode);
1351-
ret = 1;
1352-
goto cleanup;
1353-
}
1354-
1355-
sprintf(buf, "%o", mode);
1356-
ret = nc_server_config_append(ctx, tree_path, "mode", buf, config);
1357-
if (ret) {
1358-
goto cleanup;
1359-
}
1360-
}
1361-
1362-
/* uid */
1363-
if (uid != (uid_t)-1) {
1364-
memset(buf, 0, 12);
1365-
sprintf(buf, "%u", uid);
1366-
ret = nc_server_config_append(ctx, tree_path, "uid", buf, config);
1367-
if (ret) {
1368-
goto cleanup;
1369-
}
1370-
}
1371-
1372-
/* gid */
1373-
if (gid != (gid_t)-1) {
1374-
memset(buf, 0, 12);
1375-
sprintf(buf, "%u", gid);
1376-
ret = nc_server_config_append(ctx, tree_path, "gid", buf, config);
1377-
if (ret) {
1378-
goto cleanup;
1379-
}
1380-
}
1381-
1382-
cleanup:
1383-
free(tree_path);
1384-
return ret;
1385-
}
1386-
13871328
API int
13881329
nc_server_config_add_ch_persistent(const struct ly_ctx *ctx, const char *ch_client_name, struct lyd_node **config)
13891330
{

0 commit comments

Comments
 (0)