Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 512ed48

Browse files
nielsbbvanassche
authored andcommittedOct 9, 2024
Port sshtosnmp to FreeBSD.
Completes 3346619 (libsnmp: Port the SSH domain transport to FreeBSD)
1 parent f2b6880 commit 512ed48

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed
 

‎apps/sshtosnmp.c

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
#include <stdlib.h>
2727
#include <errno.h>
2828
#include <stdio.h>
29+
#ifdef HAVE_STRING_H
30+
#include <string.h>
31+
#endif
2932

3033
#ifndef MAXPATHLEN
3134
#warning no system max path length detected
@@ -87,6 +90,7 @@ main(int argc, char **argv) {
8790
exit(1);
8891
}
8992

93+
#if defined(SO_PASSCRED)
9094
/* set the SO_PASSCRED option so we can pass uid */
9195
/* XXX: according to the unix(1) manual this shouldn't be needed
9296
on the sending side? */
@@ -95,6 +99,15 @@ main(int argc, char **argv) {
9599
setsockopt(sock, SOL_SOCKET, SO_PASSCRED, (void *) &one,
96100
sizeof(one));
97101
}
102+
#elif defined(LOCAL_CREDS)
103+
{
104+
int one = 1;
105+
setsockopt(sock, SOL_SOCKET, LOCAL_CREDS, (void *) &one,
106+
sizeof(one));
107+
}
108+
#else
109+
#error Cannot pass credentials
110+
#endif
98111

99112
if (connect(sock, (struct sockaddr *) &addr,
100113
sizeof(struct sockaddr_un)) != 0) {
@@ -124,21 +137,36 @@ main(int argc, char **argv) {
124137
{
125138
struct msghdr msg = { 0 };
126139
struct iovec iov = { buf, buf_len };
127-
struct ucred ouruser = { 0 };
128140
struct cmsghdr *cmsg;
129-
char cmsgbuf[CMSG_SPACE(sizeof(ouruser))];
141+
#if defined(SCM_CREDENTIALS)
142+
struct ucred ouruser = { 0 };
143+
char cmsgbuf[sizeof(ouruser)];
130144

131145
ouruser.uid = getuid();
132146
ouruser.gid = getgid();
133147
ouruser.pid = getpid();
148+
#elif defined(SCM_CREDS)
149+
struct cmsgcred ouruser = { 0 };
150+
char cmsgbuf[2*4096];
151+
152+
ouruser.cmcred_uid = getuid();
153+
ouruser.cmcred_gid = getgid();
154+
ouruser.cmcred_pid = getpid();
155+
#else
156+
#error cannot encode credentials
157+
#endif
134158

135159
msg.msg_control = cmsgbuf;
136160
msg.msg_controllen = sizeof(cmsgbuf);
137161

138162
cmsg = CMSG_FIRSTHDR(&msg);
139163
cmsg->cmsg_len = CMSG_LEN(sizeof(ouruser));
140164
cmsg->cmsg_level = SOL_SOCKET;
141-
cmsg->cmsg_type = SCM_CREDENTIALS;
165+
#if defined(SCM_CREDENTIALS)
166+
cmsg->cmsg_type = SCM_CREDENTIALS;
167+
#elif defined(SCM_CREDS)
168+
cmsg->cmsg_type = SCM_CREDS;
169+
#endif
142170
memcpy(CMSG_DATA(cmsg), &ouruser, sizeof(ouruser));
143171

144172
msg.msg_name = NULL;

0 commit comments

Comments
 (0)
Please sign in to comment.