Skip to content

Commit 3e2e61b

Browse files
committed
compat UPDATE move socket UID get into compat
1 parent 8154a39 commit 3e2e61b

5 files changed

Lines changed: 60 additions & 61 deletions

File tree

CMakeLists.txt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -313,16 +313,6 @@ find_package(LibYANG ${LIBYANG_DEP_SOVERSION} REQUIRED)
313313
target_link_libraries(netconf2 ${LIBYANG_LIBRARIES})
314314
include_directories(${LIBYANG_INCLUDE_DIRS})
315315

316-
# function compatibility - getpeereid on QNX
317-
if(${CMAKE_SYSTEM_NAME} MATCHES "QNX")
318-
target_link_libraries(netconf2 -lsocket)
319-
list(APPEND CMAKE_REQUIRED_LIBRARIES socket)
320-
list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES pthread)
321-
list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_QNX_SOURCE)
322-
check_symbol_exists(getpeereid "sys/types.h;unistd.h" HAVE_GETPEEREID)
323-
list(REMOVE_ITEM CMAKE_REQUIRED_DEFINITIONS -D_QNX_SOURCE)
324-
endif()
325-
326316
# set sources
327317
target_sources(netconf2 PRIVATE ${libsrc} ${compatsrc})
328318

CMakeModules/UseCompat.cmake

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,19 @@ macro(USE_COMPAT)
8383
check_symbol_exists(crypt_r "unistd.h" HAVE_CRYPT_R)
8484
endif()
8585

86+
# unsock_get_uid
87+
check_symbol_exists(SO_PEERCRED "sys/socket.h" HAVE_SO_PEERCRED)
88+
if(NOT HAVE_SO_PEERCRED)
89+
if(${CMAKE_SYSTEM_NAME} MATCHES "QNX")
90+
target_link_libraries(netconf2 -lsocket)
91+
list(APPEND CMAKE_REQUIRED_LIBRARIES socket)
92+
list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES pthread)
93+
list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_QNX_SOURCE)
94+
endif()
95+
check_symbol_exists(getpeereid "sys/types.h;unistd.h" HAVE_GETPEEREID)
96+
list(REMOVE_ITEM CMAKE_REQUIRED_DEFINITIONS -D_QNX_SOURCE)
97+
endif()
98+
8699
test_big_endian(IS_BIG_ENDIAN)
87100

88101
check_include_file("stdatomic.h" HAVE_STDATOMIC)

compat/compat.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*/
1414
#define _POSIX_C_SOURCE 200809L /* fdopen, _POSIX_PATH_MAX, strdup */
1515
#define _ISOC99_SOURCE /* vsnprintf */
16+
#define _QNX_SOURCE /* getpeereid */
17+
#define _GNU_SOURCE /* SO_PEERCRED */
1618

1719
#include "compat.h"
1820

@@ -24,7 +26,10 @@
2426
#include <stdio.h>
2527
#include <stdlib.h>
2628
#include <string.h>
29+
#include <sys/socket.h>
2730
#include <sys/time.h>
31+
#include <sys/types.h>
32+
#include <sys/un.h>
2833
#include <time.h>
2934
#include <unistd.h>
3035

@@ -424,3 +429,30 @@ timegm(struct tm *tm)
424429
}
425430

426431
#endif
432+
433+
int
434+
unsock_get_uid(int sock, uid_t *uid)
435+
{
436+
int r;
437+
438+
#if defined(HAVE_SO_PEERCRED)
439+
struct ucred ucred;
440+
socklen_t len;
441+
442+
len = sizeof(ucred);
443+
r = getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len);
444+
if (!r) {
445+
*uid = ucred.uid;
446+
}
447+
#elif defined(GETPEEREUID)
448+
r = getpeereid(sock, uid, NULL);
449+
#else
450+
(void)sock;
451+
(void)uid;
452+
453+
r = -1;
454+
errno = ENOSYS;
455+
#endif
456+
457+
return r;
458+
}

compat/compat.h.in

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@
7979
#cmakedefine HAVE_STRDUPA
8080
#cmakedefine HAVE_STRCHRNUL
8181
#cmakedefine HAVE_GET_CURRENT_DIR_NAME
82-
#cmakedefine HAVE_CRYPT_R
8382
#cmakedefine HAVE_TIMEGM
83+
#cmakedefine HAVE_CRYPT_R
84+
#cmakedefine HAVE_SO_PEERCRED
85+
#cmakedefine HAVE_GETPEEREID
8486

8587
#ifndef bswap64
8688
#define bswap64(val) \
@@ -218,12 +220,10 @@ char *get_current_dir_name(void);
218220

219221
#ifndef HAVE_CRYPT_R
220222

221-
# if !defined (__FreeBSD__)
222223
/* unused anyway */
223224
struct crypt_data {
224225
char a;
225226
};
226-
# endif
227227

228228
char *crypt_r(const char *phrase, const char *setting, struct crypt_data *data);
229229
#endif
@@ -232,4 +232,14 @@ char *crypt_r(const char *phrase, const char *setting, struct crypt_data *data);
232232
time_t timegm(struct tm *tm);
233233
#endif
234234

235+
/**
236+
* @brief Get UID of the owner of a socket.
237+
*
238+
* @param[in] sock Socket to analyze.
239+
* @param[out] uid Socket owner UID.
240+
* @return 0 on success,
241+
* @return -1 on error.
242+
*/
243+
int unsock_get_uid(int sock, uid_t *uid);
244+
235245
#endif /* _COMPAT_H_ */

src/session_server.c

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
*
1313
* https://opensource.org/licenses/BSD-3-Clause
1414
*/
15-
#define _QNX_SOURCE /* getpeereid */
16-
#define _GNU_SOURCE /* threads, SO_PEERCRED */
15+
#define _GNU_SOURCE /* threads */
1716

1817
#include <arpa/inet.h>
1918
#include <assert.h>
@@ -2325,43 +2324,6 @@ nc_server_set_address_port(struct nc_endpt *endpt, struct nc_bind *bind, const c
23252324
return ret;
23262325
}
23272326

2328-
#if defined (SO_PEERCRED) || defined (HAVE_GETPEEREID)
2329-
2330-
/**
2331-
* @brief Get UID of the owner of a socket.
2332-
*
2333-
* @param[in] sock Socket to analyze.
2334-
* @param[out] uid Socket owner UID.
2335-
* @return 0 on success,
2336-
* @return -1 on error.
2337-
*/
2338-
static int
2339-
nc_get_uid(int sock, uid_t *uid)
2340-
{
2341-
int r;
2342-
2343-
#ifdef SO_PEERCRED
2344-
struct ucred ucred;
2345-
socklen_t len;
2346-
2347-
len = sizeof(ucred);
2348-
r = getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len);
2349-
if (!r) {
2350-
*uid = ucred.uid;
2351-
}
2352-
#else
2353-
r = getpeereid(sock, uid, NULL);
2354-
#endif
2355-
2356-
if (r < 0) {
2357-
ERR(NULL, "Failed to get owner UID of a UNIX socket (%s).", strerror(errno));
2358-
return -1;
2359-
}
2360-
return 0;
2361-
}
2362-
2363-
#endif
2364-
23652327
/**
23662328
* @brief Fully accept a session on a connected UNIX socket.
23672329
*
@@ -2373,14 +2335,13 @@ nc_get_uid(int sock, uid_t *uid)
23732335
static int
23742336
nc_accept_unix_session(struct nc_session *session, int sock)
23752337
{
2376-
#if defined (SO_PEERCRED) || defined (HAVE_GETPEEREID)
23772338
struct passwd *pw, pw_buf;
23782339
char *username;
23792340
uid_t uid = 0;
23802341
char *buf = NULL;
23812342
size_t buf_len = 0;
23822343

2383-
if (nc_get_uid(sock, &uid)) {
2344+
if (unsock_get_uid(sock, &uid)) {
23842345
close(sock);
23852346
return -1;
23862347
}
@@ -2405,13 +2366,6 @@ nc_accept_unix_session(struct nc_session *session, int sock)
24052366
session->ti.unixsock.sock = sock;
24062367

24072368
return 1;
2408-
#else
2409-
(void)session;
2410-
(void)sock;
2411-
2412-
ERR(session, "Unable to learn the identity of the client connected to the UNIX socket, terminating.");
2413-
return -1;
2414-
#endif
24152369
}
24162370

24172371
API int

0 commit comments

Comments
 (0)