Skip to content

Commit 4e903c3

Browse files
authored
libnetconf2 CHANGE QNX compatibility (#220)
fixes #217
1 parent 31dd4c5 commit 4e903c3

4 files changed

Lines changed: 83 additions & 19 deletions

File tree

CMakeLists.txt

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,13 @@ if(ENABLE_SSH)
251251
include_directories(${LIBSSH_INCLUDE_DIRS})
252252

253253
# crypt
254-
target_link_libraries(netconf2 -lcrypt)
255-
list(APPEND CMAKE_REQUIRED_LIBRARIES crypt)
254+
if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "QNX")
255+
target_link_libraries(netconf2 -lcrypt)
256+
list(APPEND CMAKE_REQUIRED_LIBRARIES crypt)
257+
else()
258+
target_link_libraries(netconf2 -llogin)
259+
list(APPEND CMAKE_REQUIRED_LIBRARIES login)
260+
endif()
256261
endif()
257262

258263
# dependencies - libval
@@ -268,6 +273,20 @@ find_package(LibYANG REQUIRED)
268273
target_link_libraries(netconf2 ${LIBYANG_LIBRARIES})
269274
include_directories(${LIBYANG_INCLUDE_DIRS})
270275

276+
# header file compatibility - shadow.h and crypt.h
277+
check_include_file("shadow.h" HAVE_SHADOW)
278+
check_include_file("crypt.h" HAVE_CRYPT)
279+
280+
# function compatibility - getpeereid on QNX
281+
if(${CMAKE_SYSTEM_NAME} MATCHES "QNX")
282+
target_link_libraries(netconf2 -lsocket)
283+
list(APPEND CMAKE_REQUIRED_LIBRARIES socket)
284+
list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES pthread)
285+
list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_QNX_SOURCE)
286+
check_symbol_exists(getpeereid "sys/types.h;unistd.h" HAVE_GETPEEREID)
287+
list(REMOVE_ITEM CMAKE_REQUIRED_DEFINITIONS -D_QNX_SOURCE)
288+
endif()
289+
271290
# generate doxygen documentation for libnetconf2 API
272291
find_package(Doxygen)
273292
if(DOXYGEN_FOUND)

src/config.h.in

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@
4747
*/
4848
#cmakedefine HAVE_PTHREAD_MUTEX_TIMEDLOCK
4949

50+
/*
51+
* Support for getpeereid
52+
*/
53+
#cmakedefine HAVE_GETPEEREID
54+
55+
/*
56+
* Support for shadow file manipulation
57+
*/
58+
#cmakedefine HAVE_SHADOW
59+
60+
/*
61+
* Support for crypt.h
62+
*/
63+
#cmakedefine HAVE_CRYPT
64+
5065
/*
5166
* Location of installed basic YIN/YANG schemas
5267
*/

src/session_server.c

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*
1212
* https://opensource.org/licenses/BSD-3-Clause
1313
*/
14+
#define _QNX_SOURCE /* getpeereid */
1415
#define _GNU_SOURCE /* signals, threads, SO_PEERCRED */
1516

1617
#include <stdint.h>
@@ -1717,27 +1718,48 @@ nc_ps_clear(struct nc_pollsession *ps, int all, void (*data_free)(void *))
17171718

17181719
#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
17191720

1721+
static int
1722+
nc_get_uid(int sock, uid_t *uid)
1723+
{
1724+
#ifdef SO_PEERCRED
1725+
struct ucred ucred;
1726+
socklen_t len;
1727+
len = sizeof(ucred);
1728+
if (getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len) < 0) {
1729+
ERR("Failed to get credentials from unix socket (%s).",
1730+
strerror(errno));
1731+
close(sock);
1732+
return -1;
1733+
}
1734+
*uid = ucred.uid;
1735+
#else
1736+
if (getpeereid(sock, uid, NULL) < 0) {
1737+
ERR("Failed to get credentials from unix socket (%s).",
1738+
strerror(errno));
1739+
close(sock);
1740+
return -1;
1741+
}
1742+
#endif
1743+
1744+
return 0;
1745+
}
1746+
17201747
static int
17211748
nc_accept_unix(struct nc_session *session, int sock)
17221749
{
1723-
#ifdef SO_PEERCRED
1750+
#if defined(SO_PEERCRED) || defined(HAVE_GETPEEREID)
17241751
const struct passwd *pw;
1725-
struct ucred ucred;
17261752
char *username;
1727-
socklen_t len;
17281753
session->ti_type = NC_TI_UNIX;
1754+
uid_t uid;
17291755

1730-
len = sizeof(ucred);
1731-
if (getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len) < 0) {
1732-
ERR("Failed to get credentials from unix socket (%s).",
1733-
strerror(errno));
1734-
close(sock);
1756+
if (nc_get_uid(sock, &uid) < 0) {
17351757
return -1;
17361758
}
17371759

1738-
pw = getpwuid(ucred.uid);
1760+
pw = getpwuid(uid);
17391761
if (pw == NULL) {
1740-
ERR("Failed to find username for uid=%u (%s).\n", ucred.uid,
1762+
ERR("Failed to find username for uid=%u (%s).\n", uid,
17411763
strerror(errno));
17421764
close(sock);
17431765
return -1;

src/session_server_ssh.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,25 @@
1414

1515
#define _GNU_SOURCE
1616

17+
#include "config.h" /* Expose HAVE_SHADOW and HAVE_CRYPT */
18+
19+
#ifdef HAVE_SHADOW
20+
#include <shadow.h>
21+
#endif
22+
#ifdef HAVE_CRYPT
23+
#include <crypt.h>
24+
#endif
25+
1726
#include <stdlib.h>
1827
#include <string.h>
1928
#include <sys/types.h>
2029
#include <sys/stat.h>
2130
#include <pwd.h>
22-
#ifndef __APPLE__
23-
#include <shadow.h>
24-
#include <crypt.h>
25-
#endif
2631
#include <errno.h>
2732
#include <time.h>
2833
#include <fcntl.h>
2934
#include <unistd.h>
3035

31-
#include "config.h"
3236
#include "session_server.h"
3337
#include "session_server_ch.h"
3438
#include "libnetconf.h"
@@ -664,7 +668,7 @@ nc_server_ssh_clear_opts(struct nc_server_ssh_opts *opts)
664668
static char *
665669
auth_password_get_pwd_hash(const char *username)
666670
{
667-
#ifndef __APPLE__
671+
#ifdef HAVE_SHADOW
668672
struct passwd *pwd, pwd_buf;
669673
struct spwd *spwd, spwd_buf;
670674
char *pass_hash = NULL, buf[256];
@@ -676,7 +680,11 @@ auth_password_get_pwd_hash(const char *username)
676680
}
677681

678682
if (!strcmp(pwd->pw_passwd, "x")) {
679-
getspnam_r(username, &spwd_buf, buf, 256, &spwd);
683+
#ifndef __QNXNTO__
684+
getspnam_r(username, &spwd_buf, buf, 256, &spwd);
685+
#else
686+
spwd = getspnam_r(username, &spwd_buf, buf, 256);
687+
#endif
680688
if (!spwd) {
681689
VRB("Failed to retrieve the shadow entry for \"%s\".", username);
682690
return NULL;

0 commit comments

Comments
 (0)