|
5859
|
1 /*
|
|
|
2 * gaim-remote
|
|
|
3 *
|
|
|
4 * Copyright (C) 2002, Sean Egan <bj91704@binghamton.edu>
|
|
|
5 *
|
|
|
6 * This program is free software; you can redistribute it and/or modify
|
|
|
7 * it under the terms of the GNU General Public License as published by
|
|
|
8 * the Free Software Foundation; either version 2 of the License, or
|
|
|
9 * (at your option) any later version.
|
|
|
10 *
|
|
|
11 * This program is distributed in the hope that it will be useful,
|
|
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
14 * GNU General Public License for more details.
|
|
|
15 *
|
|
|
16 * You should have received a copy of the GNU General Public License
|
|
|
17 * along with this program; if not, write to the Free Software
|
|
|
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
19 *
|
|
|
20 */
|
|
|
21
|
|
|
22 /* Somewhat inspired by XMMS:
|
|
|
23 * Copyright (C) 1998-2002 Peter Alm, Mikael Alm, Olle Hallnas,
|
|
|
24 * Thomas Nilsson and 4Front Technologies
|
|
|
25 * Copyright (C) 1999-2002 Haavard Kvaalen
|
|
|
26 */
|
|
|
27
|
|
|
28 /* This provides code for connecting to a Gaim socket and communicating with
|
|
|
29 * it. It will eventually be made a library once the core and ui are split. */
|
|
|
30
|
|
|
31 #include <sys/types.h>
|
|
|
32 #include <sys/socket.h>
|
|
|
33 #include <sys/un.h>
|
|
|
34 #include <unistd.h>
|
|
|
35 #include "gaim.h"
|
|
|
36 #include "remote-socket.h"
|
|
|
37
|
|
|
38 void
|
|
|
39 gaim_remote_session_send_packet(int fd, GaimRemotePacket *p)
|
|
|
40 {
|
|
|
41 int len = sizeof(p->type) + sizeof(p->subtype) +
|
|
|
42 sizeof(p->length) + p->length;
|
|
|
43 char *pack = g_malloc(len);
|
|
|
44 char *a = pack;
|
|
|
45
|
|
|
46 memcpy (a, &(p->type), sizeof(p->type));
|
|
|
47 a = a + sizeof(p->type);
|
|
|
48 memcpy (a, &(p->subtype), sizeof(p->subtype));
|
|
|
49 a = a + sizeof(p->subtype);
|
|
|
50 memcpy (a, &(p->length), sizeof(p->length));
|
|
|
51 a = a + sizeof(p->length);
|
|
|
52 memcpy (a, p->data, p->length);
|
|
|
53 write(fd, pack, len);
|
|
|
54 g_free(pack);
|
|
|
55 }
|
|
|
56
|
|
|
57 void
|
|
|
58 gaim_remote_packet_append_string(GaimRemotePacket *p, char *str)
|
|
|
59 {
|
|
|
60 int len = p->length + strlen(str);
|
|
|
61 char *k = g_malloc(len);
|
|
|
62
|
|
|
63 memcpy(k, p->data, p->length);
|
|
|
64 memcpy(k + p->length, str, strlen(str));
|
|
|
65
|
|
|
66 if (p->data)
|
|
|
67 g_free(p->data);
|
|
|
68
|
|
|
69 p->data = k;
|
|
|
70 p->length = len;
|
|
|
71 }
|
|
|
72
|
|
|
73 void
|
|
|
74 gaim_remote_packet_append_char(GaimRemotePacket *p, char c)
|
|
|
75 {
|
|
|
76 int len = p->length + sizeof(char);
|
|
|
77 char *k = g_malloc(len);
|
|
|
78
|
|
|
79 memcpy(k, p->data, p->length);
|
|
|
80 k[p->length] = c;
|
|
|
81
|
|
|
82 if (p->data)
|
|
|
83 g_free(p->data);
|
|
|
84
|
|
|
85 p->data = k;
|
|
|
86 p->length = len;
|
|
|
87 }
|
|
|
88
|
|
|
89 void
|
|
|
90 gaim_remote_packet_append_raw(GaimRemotePacket *p, char *str, int len)
|
|
|
91 {
|
|
|
92 int lent = p->length + len;
|
|
|
93 char *k = g_malloc(lent);
|
|
|
94
|
|
|
95 memcpy(k, p->data, p->length);
|
|
|
96 memcpy(k + p->length, str, len);
|
|
|
97
|
|
|
98 if (p->data)
|
|
|
99 g_free(p->data);
|
|
|
100
|
|
|
101 p->data = k;
|
|
|
102 p->length = lent;
|
|
|
103 }
|
|
|
104
|
|
|
105 GaimRemotePacket *
|
|
|
106 gaim_remote_packet_new(guchar type, guchar subtype)
|
|
|
107 {
|
|
|
108 GaimRemotePacket *p = g_new0(GaimRemotePacket, 1);
|
|
|
109 p->type = type;
|
|
|
110 p->subtype = subtype;
|
|
|
111 p->length = 0;
|
|
|
112 p->data = NULL;
|
|
|
113 return p;
|
|
|
114 }
|
|
|
115
|
|
|
116 void
|
|
|
117 gaim_remote_packet_free(GaimRemotePacket *p)
|
|
|
118 {
|
|
|
119 if (p->data)
|
|
|
120 g_free(p->data);
|
|
|
121 g_free(p);
|
|
|
122 }
|
|
|
123
|
|
|
124 GaimRemotePacket *
|
|
|
125 gaim_remote_session_read_packet(int fd)
|
|
|
126 {
|
|
|
127 GaimRemotePacket *p = g_new0(GaimRemotePacket, 1);
|
|
|
128 char *data = NULL;
|
|
|
129
|
|
|
130 if (!(read(fd, &p->type, sizeof(p->type)))) {
|
|
|
131 g_free(p);
|
|
|
132 return NULL;
|
|
|
133 }
|
|
|
134
|
|
|
135 if (!(read(fd, &p->subtype, sizeof(p->subtype)))) {
|
|
|
136 g_free(p);
|
|
|
137 return NULL;
|
|
|
138 }
|
|
|
139
|
|
|
140 if (!(read(fd, &p->length, sizeof(p->length)))) {
|
|
|
141 g_free(p);
|
|
|
142 return NULL;
|
|
|
143 }
|
|
|
144
|
|
|
145 if (p->length) {
|
|
|
146 data = g_malloc(p->length);
|
|
|
147
|
|
|
148 if (!(read(fd, data, p->length))) {
|
|
|
149 g_free(p);
|
|
|
150 return NULL;
|
|
|
151 }
|
|
|
152 }
|
|
|
153
|
|
|
154 p->data = data;
|
|
|
155
|
|
|
156 return p;
|
|
|
157 }
|
|
|
158
|
|
|
159 /* copied directly from xmms_connect_to_session */
|
|
|
160 int
|
|
|
161 gaim_remote_session_connect(int session)
|
|
|
162 {
|
|
|
163 gint fd;
|
|
|
164 uid_t stored_uid, euid;
|
|
|
165 struct sockaddr_un saddr;
|
|
|
166
|
|
|
167 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) != -1)
|
|
|
168 {
|
|
|
169 saddr.sun_family = AF_UNIX;
|
|
|
170 stored_uid = getuid();
|
|
|
171 euid = geteuid();
|
|
|
172 setuid(euid);
|
|
|
173 sprintf(saddr.sun_path, "%s/gaim_%s.%d",
|
|
|
174 g_get_tmp_dir(), g_get_user_name(), session);
|
|
|
175 setreuid(stored_uid, euid);
|
|
|
176
|
|
|
177 if (connect(fd, (struct sockaddr *) &saddr, sizeof (saddr)) != -1)
|
|
|
178 return fd;
|
|
|
179 }
|
|
|
180
|
|
|
181 close(fd);
|
|
|
182
|
|
|
183 return -1;
|
|
|
184 }
|
|
|
185
|
|
|
186 gboolean
|
|
|
187 gaim_remote_session_exists(int sess)
|
|
|
188 {
|
|
|
189 GaimRemotePacket *pack = NULL;
|
|
|
190 int fd = gaim_remote_session_connect(sess);
|
|
|
191
|
|
|
192 if (fd > 0) {
|
|
|
193 pack = gaim_remote_packet_new(CUI_TYPE_META, CUI_META_PING);
|
|
|
194 gaim_remote_session_send_packet(fd, pack);
|
|
|
195 gaim_remote_packet_free(pack);
|
|
|
196 close(fd);
|
|
|
197
|
|
|
198 return TRUE;
|
|
|
199 }
|
|
|
200
|
|
|
201 return FALSE;
|
|
|
202 }
|
|
|
203
|