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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
#ifndef __CONNECT_H__
#define __CONNECT_H__
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <sys/wait.h>
#include "config.h"
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
#ifndef SUN_LEN
#define SUN_LEN(ptr) ((socklen_t) (((struct sockaddr_un *) 0)->sun_path) \
+ strlen ((ptr)->sun_path))
#endif
#include "common.h"
#include "config.h"
#ifdef __cplusplus
typedef bool kgtk_bool;
#define KGTK_TRUE true
#define KGTK_FALSE false
#else
typedef gboolean kgtk_bool;
#define KGTK_TRUE TRUE
#define KGTK_FALSE FALSE
#endif
static int kdialogdSocket=-1;
/* From kdelibs/tdesu */
#ifdef KDIALOGD_APP
static int createSocketConnectionReal()
#else
static int createSocketConnection()
#endif
{
#ifdef KGTK_DEBUG
printf("KGTK::createSocketConnection A\n");
#endif
int sockfd=-1;
const char *sock=getSockName();
struct sockaddr_un addr;
if (access(sock, R_OK|W_OK))
{
#ifdef KGTK_DEBUG
printf("KGTK::createSocketConnection - Could not access socket, %s\n", sock);
#endif
return -1;
}
sockfd = socket(PF_UNIX, SOCK_STREAM, 0);
if (sockfd < 0)
{
#ifdef KGTK_DEBUG
printf("KGTK::createSocketConnection - Could not create socket, %d\n", errno);
#endif
return -1;
}
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, sock);
if (connect(sockfd, (struct sockaddr *) &addr, SUN_LEN(&addr)) < 0)
{
#ifdef KGTK_DEBUG
printf("KGTK::createSocketConnection - Could not connect socket, %d\n", errno);
#endif
close(sockfd);
return -1;
}
#if !defined(SO_PEERCRED) || !defined(HAVE_STRUCT_UCRED)
# if defined(HAVE_GETPEEREID)
{
uid_t euid;
gid_t egid;
/* Security: if socket exists, we must own it */
if (getpeereid(sockfd, &euid, &egid) == 0)
{
if (euid != getuid())
{
#ifdef KGTK_DEBUG
printf("KGTK::createSocketConnection - socket not owned by me! socket uid %d\n", euid);
#endif
close(sockfd);
return -1;
}
}
}
# else
# ifdef __GNUC__
# warning "Using sloppy security checks"
# endif
/* We check the owner of the socket after we have connected.
If the socket was somehow not ours an attacker will be able
to delete it after we connect but shouldn't be able to
create a socket that is owned by us. */
{
struct stat s;
if (lstat(sock, &s)!=0)
{
#ifdef KGTK_DEBUG
printf("KGTK::createSocketConnection - stat failed %s\n", sock);
#endif
close(sockfd);
return -1;
}
if (s.st_uid != getuid())
{
#ifdef KGTK_DEBUG
printf("KGTK::createSocketConnection - socket not owned by me! socket uid %d\n", s.st_uid);
#endif
close(sockfd);
return -1;
}
if (!S_ISSOCK(s.st_mode))
{
#ifdef KGTK_DEBUG
printf("KGTK::createSocketConnection - socket is not a socket %s\n", sock);
#endif
close(sockfd);
return -1;
}
}
# endif
#else
{
struct ucred cred;
socklen_t siz = sizeof(cred);
/* Security: if socket exists, we must own it */
if (getsockopt(sockfd, SOL_SOCKET, SO_PEERCRED, &cred, &siz) == 0)
{
if (cred.uid != getuid())
{
#ifdef KGTK_DEBUG
printf("KGTK::createSocketConnection - socket not owned by me! socket uid %d\n", cred.uid);
#endif
close(sockfd);
return -1;
}
}
}
#endif
#ifdef KGTK_DEBUG
printf("KGTK::createSocketConnection - sockfd:%d\n", sockfd);
#endif
return sockfd;
}
#ifdef KDIALOGD_APP
static int createSocketConnection()
{
int rv=-1,
tries=0;
do
{
if(-1==(rv=createSocketConnectionReal()))
usleep(10000);
}
while(-1==rv && ++tries<50);
if(-1==rv)
fprintf(stderr, "ERROR: Could not talk to KDialogD!!!\n");
return rv;
}
#endif
static int kdialogdPid=-1;
static kgtk_bool processIsRunning()
{
#ifdef KGTK_DEBUG
printf("KGTK::processIsRunning\n");
#endif
if(-1!=kdialogdPid && 0==kill(kdialogdPid, 0))
{
#ifdef KGTK_DEBUG
printf("KGTK::processIsRunning (%d) YES\n", kdialogdPid);
#endif
return KGTK_TRUE;
}
else
{
FILE *f=fopen(getPidFileName(), "r");
if(f)
{
int pid=0;
if(1==fscanf(f, "%d", &pid))
{
fclose(f);
if(-1!=kdialogdPid && kdialogdPid!=pid)
{
#ifdef KGTK_DEBUG
printf("KGTK::processIsRunning pid has changed from:%d to %d - need to reconnect\n", kdialogdPid, pid);
#endif
kdialogdPid=pid;
return KGTK_FALSE;
}
#ifdef KGTK_DEBUG
printf("KGTK::processIsRunning file has pid:%d\n", pid);
#endif
if(0==kill(pid, 0))
{
#ifdef KGTK_DEBUG
printf("KGTK::processIsRunning (file:%d) YES\n", pid);
#endif
kdialogdPid=pid;
return KGTK_TRUE;
}
kdialogdPid=-1; /* Process is not running! */
}
}
}
#ifdef KGTK_DEBUG
printf("KGTK::processIsRunning NO\n");
#endif
return KGTK_FALSE;
}
static void closeConnection()
{
#ifdef KGTK_DEBUG
printf("KGTK::closeConnection\n");
#endif
close(kdialogdSocket);
kdialogdSocket=-1;
}
/* Note: Calling 'fork' seems to mess things up with eclipse! */
#define KGTK_USE_SYSTEM_CALL
static kgtk_bool connectToKDialogD(const char *appName)
{
#ifdef KGTK_DEBUG
printf("KGTK::connectToKDialogD %s\n", appName ? appName : "<null>");
#endif
if(!processIsRunning())
closeConnection();
if(-1!=kdialogdSocket)
return KGTK_TRUE;
else
{
unsigned int slen=strlen(appName);
kgtk_bool rv=KGTK_TRUE;
if(slen)
slen++;
#ifdef KGTK_DEBUG
printf("KGTK::connectToKDialogD - start wrapper\n");
#endif
#ifdef KDIALOGD_APP
grabLock(5);
#ifdef KGTK_USE_SYSTEM_CALL
system("kdialogd-wrapper &");
#else
switch(fork())
{
case -1:
rv=KGTK_FALSE;
printf("ERROR: Could not start fork :-(\n");
break;
case 0:
execl(KDIALOGD_LOCATION"/kdialogd-wrapper", "kdialogd-wrapper", (char *)NULL);
break;
default:
{
int status=0;
wait(&status);
}
}
#endif
releaseLock();
#endif
if(!rv)
return rv;
rv=
#ifdef KDIALOGD_APP
grabLock(3)>0 &&
#else
0==system("dcop kded kded loadModule kdialogd") &&
#endif
-1!=(kdialogdSocket=createSocketConnection()) &&
writeBlock(kdialogdSocket, (char *)&slen, 4) &&
(0==slen || writeBlock(kdialogdSocket, appName, slen));
#ifdef KDIALOGD_APP
releaseLock();
#endif
return rv;
}
}
#endif
|