1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
$NetBSD: patch-ab,v 1.16 2009/01/06 19:00:50 hasso Exp $
--- dbus/dbus-sysdeps-unix.c.orig 2008-10-07 00:14:50 +0300
+++ dbus/dbus-sysdeps-unix.c 2009-01-06 16:17:35 +0200
@@ -23,6 +23,7 @@
*/
#define _GNU_SOURCE
+#define _NETBSD_SOURCE
#include "dbus-internals.h"
#include "dbus-sysdeps.h"
@@ -35,6 +36,9 @@
#include "dbus-list.h"
#include "dbus-credentials.h"
+#ifdef HAVE_NETBSD_ATOMIC_OPS
+#include <sys/atomic.h>
+#endif
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
@@ -71,6 +75,10 @@
#include <ucred.h>
#endif
+#ifdef __NetBSD__
+#include <sys/un.h>
+#endif
+
#ifdef HAVE_ADT
#include <bsm/adt.h>
#endif
@@ -87,6 +95,32 @@
#define socklen_t int
#endif
+#ifdef LOCAL_PEEREID
+static dbus_bool_t
+dbus_nb_getpeereid(int fd, pid_t *pid, uid_t *uid, gid_t *gid)
+{
+ struct unpcbid cred;
+ socklen_t len = sizeof(cred);
+
+ _dbus_verbose ("dbus_nb_getpeereid: enter, fd=%d\n");
+ if (getsockopt (fd, 0, LOCAL_PEEREID, &cred, &len) < 0)
+ {
+ _dbus_verbose ("dbus_nb_getpeereid: getsockopt LOCAL_PEEREID failed: %s\n", strerror(errno));
+ return FALSE;
+ }
+ if (pid)
+ *pid = cred.unp_pid;
+ if (uid)
+ *uid = cred.unp_euid;
+ if (gid)
+ *gid = cred.unp_egid;
+
+ _dbus_verbose ("dbus_nb_getpeereid: returning TRUE, pid=%d uid=%d gid=%d\n",
+ cred.unp_pid, cred.unp_euid, cred.unp_egid);
+ return TRUE;
+}
+#endif
+
static dbus_bool_t
_dbus_open_socket (int *fd_p,
int domain,
@@ -581,7 +615,7 @@ _dbus_set_local_creds (int fd, dbus_bool
{
dbus_bool_t retval = TRUE;
-#if defined(HAVE_CMSGCRED)
+#if defined(HAVE_CMSGCRED) || defined(LOCAL_PEEREID)
/* NOOP just to make sure only one codepath is used
* and to prefer CMSGCRED
*/
@@ -1041,9 +1075,9 @@ write_credentials_byte (int
int bytes_written;
char buf[1] = { '\0' };
#if defined(HAVE_CMSGCRED)
- struct {
+ union {
struct cmsghdr hdr;
- struct cmsgcred cred;
+ char cred[CMSG_SPACE (sizeof (struct cmsgcred))];
} cmsg;
struct iovec iov;
struct msghdr msg;
@@ -1054,10 +1088,10 @@ write_credentials_byte (int
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
- msg.msg_control = &cmsg;
- msg.msg_controllen = sizeof (cmsg);
+ msg.msg_control = (caddr_t) &cmsg;
+ msg.msg_controllen = CMSG_SPACE (sizeof (struct cmsgcred));
memset (&cmsg, 0, sizeof (cmsg));
- cmsg.hdr.cmsg_len = sizeof (cmsg);
+ cmsg.hdr.cmsg_len = CMSG_LEN (sizeof (struct cmsgcred));
cmsg.hdr.cmsg_level = SOL_SOCKET;
cmsg.hdr.cmsg_type = SCM_CREDS;
#endif
@@ -1129,15 +1163,15 @@ _dbus_read_credentials_socket (int
dbus_pid_t pid_read;
int bytes_read;
- uid_read = DBUS_UID_UNSET;
- pid_read = DBUS_PID_UNSET;
-
#ifdef HAVE_CMSGCRED
- struct {
+ union {
struct cmsghdr hdr;
- struct cmsgcred cred;
+ char cred[CMSG_SPACE (sizeof (struct cmsgcred))];
} cmsg;
+#elif defined(LOCAL_PEEREID)
+ pid_t sockpid, sockuid, sockgid;
+
#elif defined(LOCAL_CREDS)
struct {
struct cmsghdr hdr;
@@ -1145,6 +1179,9 @@ _dbus_read_credentials_socket (int
} cmsg;
#endif
+ uid_read = DBUS_UID_UNSET;
+ pid_read = DBUS_PID_UNSET;
+
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
/* The POSIX spec certainly doesn't promise this, but
@@ -1170,10 +1207,10 @@ _dbus_read_credentials_socket (int
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
-#if defined(HAVE_CMSGCRED) || defined(LOCAL_CREDS)
+#if (defined(HAVE_CMSGCRED) || defined(LOCAL_CREDS)) && !defined(LOCAL_PEEREID)
memset (&cmsg, 0, sizeof (cmsg));
- msg.msg_control = &cmsg;
- msg.msg_controllen = sizeof (cmsg);
+ msg.msg_control = (caddr_t) &cmsg;
+ msg.msg_controllen = CMSG_SPACE (sizeof (struct cmsgcred));
#endif
again:
@@ -1210,8 +1247,8 @@ _dbus_read_credentials_socket (int
return FALSE;
}
-#if defined(HAVE_CMSGCRED) || defined(LOCAL_CREDS)
- if (cmsg.hdr.cmsg_len < sizeof (cmsg) || cmsg.hdr.cmsg_type != SCM_CREDS)
+#if (defined(HAVE_CMSGCRED) || defined(LOCAL_CREDS)) && !defined(LOCAL_PEEREID)
+ if (cmsg.hdr.cmsg_len < CMSG_LEN (sizeof (struct cmsgcred)) || cmsg.hdr.cmsg_type != SCM_CREDS)
{
dbus_set_error (error, DBUS_ERROR_FAILED,
"Message from recvmsg() was not SCM_CREDS");
@@ -1238,8 +1275,21 @@ _dbus_read_credentials_socket (int
cr_len, (int) sizeof (cr), _dbus_strerror (errno));
}
#elif defined(HAVE_CMSGCRED)
- pid_read = cmsg.cred.cmcred_pid;
- uid_read = cmsg.cred.cmcred_euid;
+ struct cmsgcred *cred;
+
+ cred = (struct cmsgcred *) CMSG_DATA (&cmsg);
+ pid_read = cred->cmcred_pid;
+ uid_read = cred->cmcred_euid;
+#elif defined(LOCAL_PEEREID)
+ if (dbus_nb_getpeereid(client_fd, &sockpid, &sockuid, &sockgid) == TRUE)
+ {
+ pid_read = sockpid;
+ uid_read = sockuid;
+ }
+ else
+ {
+ _dbus_verbose ("Failed to dbus_nb_getpeereid() credentials: %s\n", _dbus_strerror (errno));
+ }
#elif defined(LOCAL_CREDS)
pid_read = DBUS_PID_UNSET;
uid_read = cmsg.cred.sc_uid;
@@ -1301,7 +1351,7 @@ _dbus_read_credentials_socket (int
}
if (ucred != NULL)
ucred_free (ucred);
-#else /* !SO_PEERCRED && !HAVE_CMSGCRED && !HAVE_GETPEEREID && !HAVE_GETPEERUCRED */
+#else /* !SO_PEERCRED && !HAVE_CMSGCRED && !HAVE_GETPEEREID && !HAVE_GETPEERUCRED && !LOCAL_PEEREID*/
_dbus_verbose ("Socket credentials not supported on this OS\n");
#endif
}
@@ -1826,7 +1876,26 @@ _dbus_parse_uid (const DBusString *
return TRUE;
}
+#ifdef HAVE_NETBSD_ATOMIC_OPS
+dbus_int32_t
+_dbus_atomic_inc (DBusAtomic *atomic)
+{
+ register dbus_int32_t old = atomic->value;
+
+ atomic_inc_uint((unsigned *)&atomic->value);
+ return old;
+}
+
+dbus_int32_t
+_dbus_atomic_dec (DBusAtomic *atomic)
+{
+ register dbus_int32_t old = atomic->value;
+ atomic_dec_uint((unsigned *)&atomic->value);
+ return old;
+}
+_DBUS_DEFINE_GLOBAL_LOCK (atomic);
+#else
_DBUS_DEFINE_GLOBAL_LOCK (atomic);
#if DBUS_USE_ATOMIC_INT_486_COND
@@ -1891,6 +1960,7 @@ _dbus_atomic_dec (DBusAtomic *atomic)
return res;
#endif
}
+#endif /* HAVE_NETBSD_ATOMIC_OPS */
#ifdef DBUS_BUILD_TESTS
/** Gets our GID
@@ -2793,8 +2863,18 @@ int
_dbus_printf_string_upper_bound (const char *format,
va_list args)
{
+#if defined(__hpux) || defined(__sgi)
+/* this code from libnbcompat's vsnprintf */
+ static FILE *devnull = 0;
+ if (!devnull) {
+ devnull = fopen("/dev/null", "w");
+ if (!devnull) return -1;
+ }
+ return vfprintf(devnull, format, args);
+#else
char c;
return vsnprintf (&c, 1, format, args);
+#endif
}
/**
|