Skip to content

Commit 7ce95f6

Browse files
arekinathAlex Wilson
authored andcommitted
pivy-agent: fixes for macos connection confirm
1 parent 38f7ccf commit 7ce95f6

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ ifeq ($(SYSTEM), Darwin)
9090
ZLIB_CFLAGS =
9191
ZLIB_LIBS = -lz
9292
SYSTEM_CFLAGS =
93-
SYSTEM_LIBS =
93+
SYSTEM_LIBS = -lproc
9494
RDLINE_CFLAGS =
9595
RDLINE_LIBS = -ledit
9696
HAVE_ZFS := no

pivy-agent.c

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
#include <time.h>
8787
#include <string.h>
8888
#include <unistd.h>
89+
#include <libgen.h>
8990

9091
#include "libssh/ssh2.h"
9192
#include "libssh/sshbuf.h"
@@ -113,6 +114,12 @@
113114
#include <procfs.h>
114115
#endif
115116

117+
#if defined(__APPLE__)
118+
#include <sys/proc_info.h>
119+
#include <sys/ucred.h>
120+
#include <libproc.h>
121+
#endif
122+
116123
#include "libssh/digest.h"
117124
#include "libssh/cipher.h"
118125
#include "libssh/ssherr.h"
@@ -645,9 +652,10 @@ try_confirm_client(socket_entry_t *e, enum piv_slotid slotid)
645652
}
646653

647654
if (confirm_mode == C_FORWARDED) {
648-
char *ssh = strstr(e->se_exepath, "ssh");
649-
if (ssh != NULL && ssh > e->se_exepath)
650-
--ssh;
655+
const char *ssh = NULL;
656+
const size_t len = strlen(e->se_exepath);
657+
if (len >= 4)
658+
ssh = &e->se_exepath[len - 4];
651659
if (e->se_pid_idx == 0 || ssh == NULL ||
652660
strcmp(ssh, "/ssh") != 0) {
653661
e->se_authz = AUTHZ_ALLOWED;
@@ -739,8 +747,10 @@ static uint64_t
739747
get_pid_start_time(pid_t pid)
740748
{
741749
uint64_t val = 0;
750+
#if defined(__sun) || defined(__linux__)
742751
FILE *f;
743752
char fn[128];
753+
#endif
744754

745755
#if defined(__sun)
746756
struct psinfo *psinfo;
@@ -752,12 +762,23 @@ get_pid_start_time(pid_t pid)
752762
if (fread(psinfo, sizeof (struct psinfo), 1, f) == 1) {
753763
val = psinfo->pr_start.tv_sec;
754764
val *= 1000;
755-
val += (psinfo->pr_start.tv_nsec / 1000000);
765+
val += psinfo->pr_start.tv_nsec / 1000000;
756766
}
757767
fclose(f);
758768
}
759769
free(psinfo);
760770
#endif
771+
#if defined(__APPLE__)
772+
struct proc_bsdinfo pinfo;
773+
int rc;
774+
775+
rc = proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &pinfo, sizeof (pinfo));
776+
if (rc >= sizeof (pinfo)) {
777+
val = pinfo.pbi_start_tvsec;
778+
val *= 1000;
779+
val += pinfo.pbi_start_tvusec / 1000;
780+
}
781+
#endif
761782
#if defined(__linux__)
762783
char ln[1024];
763784
size_t len;
@@ -1906,22 +1927,28 @@ handle_socket_read(u_int socknum)
19061927
gid_t egid;
19071928
int fd;
19081929
pid_t pid = 0;
1930+
#if defined(__sun) || defined(SO_PEERCRED)
19091931
uint i;
1932+
FILE *f;
1933+
#endif
19101934
char *exepath = NULL;
19111935
char *exeargs = NULL;
19121936
socket_entry_t *ent;
1913-
FILE *f;
19141937
uint64_t start_time;
19151938
#if defined(__sun)
19161939
ucred_t *peer = NULL;
19171940
struct psinfo *psinfo;
19181941
zoneid_t zid;
19191942
char fn[128];
19201943
FILE *f;
1921-
#endif
1922-
#if defined(__OpenBSD__)
1944+
#elif defined(__OpenBSD__)
19231945
struct sockpeercred *peer;
19241946
socklen_t len;
1947+
#elif defined(__APPLE__)
1948+
struct xucred *peer;
1949+
socklen_t len;
1950+
char pathBuf[PROC_PIDPATHINFO_MAXSIZE];
1951+
int rc;
19251952
#elif defined(SO_PEERCRED)
19261953
struct ucred *peer;
19271954
socklen_t len;
@@ -1976,6 +2003,26 @@ handle_socket_read(u_int socknum)
19762003
egid = peer->gid;
19772004
pid = peer->pid;
19782005
free(peer);
2006+
#elif defined(__APPLE__)
2007+
peer = calloc(1, sizeof (struct xucred));
2008+
len = sizeof (struct xucred);
2009+
if (getsockopt(fd, SOL_LOCAL, LOCAL_PEERCRED, peer, &len)) {
2010+
error("getsockopts(LOCAL_PEERCRED) %d failed: %s", fd, strerror(errno));
2011+
close(fd);
2012+
free(peer);
2013+
return -1;
2014+
}
2015+
euid = peer->cr_uid;
2016+
if (peer->cr_ngroups > 0)
2017+
egid = peer->cr_groups[0];
2018+
free(peer);
2019+
len = sizeof (pid);
2020+
if (getsockopt(fd, SOL_LOCAL, LOCAL_PEERPID, &pid, &len) == 0) {
2021+
rc = proc_pidpath(pid, pathBuf, sizeof (pathBuf));
2022+
if (rc > 0) {
2023+
exepath = strdup(pathBuf);
2024+
}
2025+
}
19792026
#elif defined(SO_PEERCRED)
19802027
peer = calloc(1, sizeof (struct ucred));
19812028
len = sizeof (struct ucred);

utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ typedef uint64_t uintmax_t;
1919
#endif
2020

2121
#if !defined(USING_SPL) && !defined(__sun)
22+
#define _MACH_MACHINE_BOOLEAN_H_
2223
typedef enum { B_FALSE = 0, B_TRUE = 1 } boolean_t;
2324
typedef unsigned int uint;
2425
typedef unsigned int u_int;

0 commit comments

Comments
 (0)