Skip to content

Commit b4bb18e

Browse files
committed
Merge branch 'devel' of https://github.com/cesnet/libnetconf2 into devel
2 parents 7d30573 + f62f0e2 commit b4bb18e

File tree

3 files changed

+60
-27
lines changed

3 files changed

+60
-27
lines changed

src/session_p.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,12 +424,14 @@ enum nc_ps_session_state {
424424
NC_PS_STATE_INVALID /**< session is invalid and was already returned by another poll */
425425
};
426426

427+
struct nc_ps_session {
428+
struct nc_session *session;
429+
enum nc_ps_session_state state;
430+
};
431+
427432
/* ACCESS locked */
428433
struct nc_pollsession {
429-
struct {
430-
struct nc_session *session;
431-
enum nc_ps_session_state state;
432-
} *sessions;
434+
struct nc_ps_session **sessions;
433435
uint16_t session_count;
434436
uint16_t last_event_session;
435437

src/session_server.c

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,8 @@ nc_ps_new(void)
816816
API void
817817
nc_ps_free(struct nc_pollsession *ps)
818818
{
819+
uint16_t i;
820+
819821
if (!ps) {
820822
return;
821823
}
@@ -824,6 +826,10 @@ nc_ps_free(struct nc_pollsession *ps)
824826
ERR("FATAL: Freeing a pollsession structure that is currently being worked with!");
825827
}
826828

829+
for (i = 0; i < ps->session_count; i++) {
830+
free(ps->sessions[i]);
831+
}
832+
827833
free(ps->sessions);
828834
pthread_mutex_destroy(&ps->lock);
829835
pthread_cond_destroy(&ps->cond);
@@ -857,8 +863,16 @@ nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session)
857863
nc_ps_unlock(ps, q_id, __func__);
858864
return -1;
859865
}
860-
ps->sessions[ps->session_count - 1].session = session;
861-
ps->sessions[ps->session_count - 1].state = NC_PS_STATE_NONE;
866+
ps->sessions[ps->session_count - 1] = calloc(1, sizeof **ps->sessions);
867+
if (!ps->sessions[ps->session_count - 1]) {
868+
ERRMEM;
869+
--ps->session_count;
870+
/* UNLOCK */
871+
nc_ps_unlock(ps, q_id, __func__);
872+
return -1;
873+
}
874+
ps->sessions[ps->session_count - 1]->session = session;
875+
ps->sessions[ps->session_count - 1]->state = NC_PS_STATE_NONE;
862876

863877
/* UNLOCK */
864878
return nc_ps_unlock(ps, q_id, __func__);
@@ -874,12 +888,14 @@ _nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session, int in
874888
goto remove;
875889
}
876890
for (i = 0; i < ps->session_count; ++i) {
877-
if (ps->sessions[i].session == session) {
891+
if (ps->sessions[i]->session == session) {
878892
remove:
879893
--ps->session_count;
880-
if (i < ps->session_count) {
894+
if (i <= ps->session_count) {
895+
free(ps->sessions[i]);
881896
ps->sessions[i] = ps->sessions[ps->session_count];
882-
} else if (!ps->session_count) {
897+
}
898+
if (!ps->session_count) {
883899
free(ps->sessions);
884900
ps->sessions = NULL;
885901
}
@@ -936,8 +952,8 @@ nc_ps_get_session_by_sid(const struct nc_pollsession *ps, uint32_t sid)
936952
}
937953

938954
for (i = 0; i < ps->session_count; ++i) {
939-
if (ps->sessions[i].session->id == sid) {
940-
ret = ps->sessions[i].session;
955+
if (ps->sessions[i]->session->id == sid) {
956+
ret = ps->sessions[i]->session;
941957
break;
942958
}
943959
}
@@ -1314,6 +1330,7 @@ nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session)
13141330
char msg[256];
13151331
struct timespec ts_timeout, ts_cur;
13161332
struct nc_session *cur_session;
1333+
struct nc_ps_session *cur_ps_session;
13171334
struct nc_server_rpc *rpc = NULL;
13181335

13191336
if (!ps) {
@@ -1347,35 +1364,36 @@ nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session)
13471364
i = j = ps->last_event_session + 1;
13481365
}
13491366
do {
1350-
cur_session = ps->sessions[i].session;
1367+
cur_ps_session = ps->sessions[i];
1368+
cur_session = cur_ps_session->session;
13511369

13521370
/* SESSION LOCK */
13531371
r = nc_session_lock(cur_session, 0, __func__);
13541372
if (r == -1) {
13551373
ret = NC_PSPOLL_ERROR;
13561374
} else if (r == 1) {
13571375
/* no one else is currently working with the session, so we can, otherwise skip it */
1358-
if (ps->sessions[i].state == NC_PS_STATE_NONE) {
1376+
if (cur_ps_session->state == NC_PS_STATE_NONE) {
13591377
if (cur_session->status == NC_STATUS_RUNNING) {
13601378
/* session is fine, work with it */
1361-
ps->sessions[i].state = NC_PS_STATE_BUSY;
1379+
cur_ps_session->state = NC_PS_STATE_BUSY;
13621380

13631381
ret = nc_ps_poll_session(cur_session, ts_cur.tv_sec, msg);
13641382
switch (ret) {
13651383
case NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR:
13661384
ERR("Session %u: %s.", cur_session->id, msg);
1367-
ps->sessions[i].state = NC_PS_STATE_INVALID;
1385+
cur_ps_session->state = NC_PS_STATE_INVALID;
13681386
break;
13691387
case NC_PSPOLL_ERROR:
13701388
ERR("Session %u: %s.", cur_session->id, msg);
1371-
ps->sessions[i].state = NC_PS_STATE_NONE;
1389+
cur_ps_session->state = NC_PS_STATE_NONE;
13721390
break;
13731391
case NC_PSPOLL_TIMEOUT:
13741392
#ifdef NC_ENABLED_SSH
13751393
case NC_PSPOLL_SSH_CHANNEL:
13761394
case NC_PSPOLL_SSH_MSG:
13771395
#endif
1378-
ps->sessions[i].state = NC_PS_STATE_NONE;
1396+
cur_ps_session->state = NC_PS_STATE_NONE;
13791397
break;
13801398
case NC_PSPOLL_RPC:
13811399
/* let's keep the state busy, we are not done with this session */
@@ -1387,9 +1405,9 @@ nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session)
13871405
if (cur_session->term_reason != NC_SESSION_TERM_CLOSED) {
13881406
ret |= NC_PSPOLL_SESSION_ERROR;
13891407
}
1390-
ps->sessions[i].state = NC_PS_STATE_INVALID;
1408+
cur_ps_session->state = NC_PS_STATE_INVALID;
13911409
}
1392-
} else if (ps->sessions[i].state == NC_PS_STATE_BUSY) {
1410+
} else if (cur_ps_session->state == NC_PS_STATE_BUSY) {
13931411
/* it definitely should not be busy because we have the lock */
13941412
ERRINT;
13951413
ret = NC_PSPOLL_ERROR;
@@ -1457,9 +1475,9 @@ nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session)
14571475
if (ret & (NC_PSPOLL_ERROR | NC_PSPOLL_BAD_RPC)) {
14581476
if (cur_session->status != NC_STATUS_RUNNING) {
14591477
ret |= NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1460-
ps->sessions[i].state = NC_PS_STATE_INVALID;
1478+
cur_ps_session->state = NC_PS_STATE_INVALID;
14611479
} else {
1462-
ps->sessions[i].state = NC_PS_STATE_NONE;
1480+
cur_ps_session->state = NC_PS_STATE_NONE;
14631481
}
14641482
} else {
14651483
cur_session->opts.server.last_rpc = time(NULL);
@@ -1473,9 +1491,9 @@ nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session)
14731491
if (!(cur_session->term_reason & (NC_SESSION_TERM_CLOSED | NC_SESSION_TERM_KILLED))) {
14741492
ret |= NC_PSPOLL_SESSION_ERROR;
14751493
}
1476-
ps->sessions[i].state = NC_PS_STATE_INVALID;
1494+
cur_ps_session->state = NC_PS_STATE_INVALID;
14771495
} else {
1478-
ps->sessions[i].state = NC_PS_STATE_NONE;
1496+
cur_ps_session->state = NC_PS_STATE_NONE;
14791497
}
14801498
}
14811499

@@ -1505,16 +1523,17 @@ nc_ps_clear(struct nc_pollsession *ps, int all, void (*data_free)(void *))
15051523

15061524
if (all) {
15071525
for (i = 0; i < ps->session_count; i++) {
1508-
nc_session_free(ps->sessions[i].session, data_free);
1526+
nc_session_free(ps->sessions[i]->session, data_free);
1527+
free(ps->sessions[i]);
15091528
}
15101529
free(ps->sessions);
15111530
ps->sessions = NULL;
15121531
ps->session_count = 0;
15131532
ps->last_event_session = 0;
15141533
} else {
15151534
for (i = 0; i < ps->session_count; ) {
1516-
if (ps->sessions[i].session->status != NC_STATUS_RUNNING) {
1517-
session = ps->sessions[i].session;
1535+
if (ps->sessions[i]->session->status != NC_STATUS_RUNNING) {
1536+
session = ps->sessions[i]->session;
15181537
_nc_ps_del_session(ps, NULL, i);
15191538
nc_session_free(session, data_free);
15201539
continue;

src/session_server_ssh.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
#include "session_server_ch.h"
2929
#include "libnetconf.h"
3030

31+
#if !defined(HAVE_CRYPT_R)
32+
pthread_mutex_t crypt_lock = PTHREAD_MUTEX_INITIALIZER;
33+
#endif
34+
3135
extern struct nc_server_opts server_opts;
3236

3337
static char *
@@ -751,7 +755,9 @@ static int
751755
auth_password_compare_pwd(const char *pass_hash, const char *pass_clear)
752756
{
753757
char *new_pass_hash;
758+
#if defined(HAVE_CRYPT_R)
754759
struct crypt_data cdata;
760+
#endif
755761

756762
if (!pass_hash[0]) {
757763
if (!pass_clear[0]) {
@@ -764,8 +770,14 @@ auth_password_compare_pwd(const char *pass_hash, const char *pass_clear)
764770
}
765771
}
766772

773+
#if defined(HAVE_CRYPT_R)
767774
cdata.initialized = 0;
768775
new_pass_hash = crypt_r(pass_clear, pass_hash, &cdata);
776+
#else
777+
pthread_mutex_lock(&crypt_lock);
778+
new_pass_hash = crypt(pass_clear, pass_hash);
779+
pthread_mutex_unlock(&crypt_lock);
780+
#endif
769781
return strcmp(new_pass_hash, pass_hash);
770782
}
771783

@@ -1540,7 +1552,7 @@ nc_ps_accept_ssh_channel(struct nc_pollsession *ps, struct nc_session **session)
15401552
}
15411553

15421554
for (i = 0; i < ps->session_count; ++i) {
1543-
cur_session = ps->sessions[i].session;
1555+
cur_session = ps->sessions[i]->session;
15441556
if ((cur_session->status == NC_STATUS_RUNNING) && (cur_session->ti_type == NC_TI_LIBSSH)
15451557
&& cur_session->ti.libssh.next) {
15461558
/* an SSH session with more channels */

0 commit comments

Comments
 (0)