summaryrefslogtreecommitdiffstats
path: root/kppp/requester.cpp
blob: 016d72fa042103e179aa4e4f117801f537fc6de5 (plain)
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
 *              kPPP: A pppd Front End for the KDE project
 *
 * $Id$
 *
 *              Copyright (C) 1997,98 Bernd Johannes Wuebben,
 *		                      Mario Weilguni,
 *                                    Harri Porten
 *
 *
 * This file was contributed by Harri Porten <[email protected]>
 *
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#ifdef __osf__
#define _XOPEN_SOURCE_EXTENDED 1
#endif


#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <sys/uio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>
#include <string.h>

#ifdef __osf__
#undef accept
extern "C" unsigned int alarm(unsigned int);
#endif

#ifdef _XPG4_2
extern "C" {
  ssize_t sendmsg(int, const struct msghdr *, int);
  ssize_t recvmsg(int, struct msghdr *, int);
}
#endif

#include <kdebug.h>
#include <tqfile.h>

#include "auth.h"
#include "pppdata.h"
#include "opener.h"
#include "requester.h"
#include "devices.h"

Requester *Requester::rq = 0L;

Requester::Requester(int s) : socket(s) {
  assert(rq==0L);
  rq = this;
  lastStatus = -1;
}

Requester::~Requester() {
}

//
// Receive file name and file descriptors from envoy
//
int Requester::recvFD() {
  union {
    struct {
      char _cmsg[sizeof(struct cmsghdr)];
      char data[sizeof(int)];  // int fd field
    };
    struct cmsghdr cmsg;
  } control;
  struct msghdr	msg;
  struct ResponseHeader response;

  struct iovec iov;
  int flags = 0, fd, len;
  size_t cmsglen;

  msg.msg_name = 0L;
  msg.msg_namelen = 0;
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;

  iov.iov_base = IOV_BASE_CAST &response;
  iov.iov_len = sizeof(struct ResponseHeader);
#ifdef CMSG_LEN
  cmsglen = CMSG_LEN(sizeof(int));
#else
  cmsglen = sizeof(struct cmsghdr) + sizeof(int);
#endif
  control.cmsg.cmsg_len = cmsglen;
  control.cmsg.cmsg_level = SOL_SOCKET;
  control.cmsg.cmsg_type = MY_SCM_RIGHTS;

  msg.msg_control = (char *) &control;
  msg.msg_controllen = control.cmsg.cmsg_len;

  fd = -1;

  // set alarm in case recvmsg() hangs
  signal(SIGALRM, recv_timeout);
  alarm(2);

  len = recvmsg(socket, &msg, flags);

  alarm(0);
  signal(SIGALRM, SIG_DFL);

  if(len <= 0) {
    kdError(5002) << "recvmsg failed " << strerror(errno) << endl;
    return -1;
  } else if (msg.msg_controllen < cmsglen) {
    kdError(5002) << "recvmsg: truncated message " << strerror(errno) << endl;
    exit(1);
  } else {
#ifdef CMSG_DATA
    fd = *((int *)CMSG_DATA(&control.cmsg));
#else
    fd = *((int *) control.cmsg.cmsg_data);
#endif
    kdDebug(5002) << "response.status: " << response.status << endl;
    assert(response.status <= 0);
    if(response.status < 0)
      return response.status;
  }

  return fd;
}

bool Requester::recvResponse() {

  struct msghdr	msg;
  struct iovec iov;
  struct ResponseHeader response;
  int flags = 0, len;

  msg.msg_name = 0L;
  msg.msg_namelen = 0;
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;
  msg.msg_control = 0L;
  msg.msg_controllen = 0;

  iov.iov_base = IOV_BASE_CAST &response;
  iov.iov_len = sizeof(struct ResponseHeader);
  kdDebug(5002) << "recvResponse(): waiting for message" << endl;
  len = recvmsg(socket, &msg, flags);
  kdDebug(5002) << "recvResponse(): received message" << endl;
  if (len <= 0) {
    if (errno == EINTR)
      kdDebug(5002) << "Interrupted system call. Continuing." << endl;
    else
      perror("recvmsg failed");
  } else {
    kdDebug(5002) << "response.status: " << response.status << endl;
  }

  lastStatus = response.status;
  return (response.status == 0);
}

int Requester::openModem(const TQString & dev) {

  struct OpenModemRequest req;
  req.header.type = Opener::OpenDevice;
  if((req.deviceNum = indexDevice(dev)) < 0)
    return -1;

  sendRequest((struct RequestHeader *) &req, sizeof(req));
  return recvFD();
}


int Requester::openLockfile(const TQString &dev, int flags) {

  struct OpenLockRequest req;

  req.header.type = Opener::OpenLock;
  if((req.deviceNum = indexDevice(dev)) < 0)
    return -1;
  req.flags = flags;

  sendRequest((struct RequestHeader *) &req, sizeof(req));
  return recvFD();
}


bool Requester::removeLockfile() {

  struct RemoveLockRequest req;

  req.header.type = Opener::RemoveLock;

  sendRequest((struct RequestHeader *) &req, sizeof(req));
  return recvResponse();
}


int Requester::openResolv(int flags) {

  struct OpenResolvRequest req;

  req.header.type = Opener::OpenResolv;
  req.flags = flags;
  sendRequest((struct RequestHeader *) &req, sizeof(req));
  return recvFD();
}


int Requester::openSysLog() {

  struct OpenLogRequest req;

  req.header.type = Opener::OpenSysLog;
  sendRequest((struct RequestHeader *) &req, sizeof(req));
  return recvFD();
}


bool Requester::setSecret(int method, const TQString &name, const TQString &password) {
  assert(name!=0);
  assert(password!=0);

  if(method == AUTH_PAPCHAP)
    return setSecret(AUTH_PAP, name, password) &&
	setSecret(AUTH_CHAP, name, password);

  struct SetSecretRequest req;
  req.header.type = Opener::SetSecret;
  switch(method) {
  case AUTH_PAP:
    req.method = Opener::PAP;
    break;
  case AUTH_CHAP:
    req.method = Opener::CHAP;
    break;
  default:
    return false;
  }
  strncpy(req.username, TQFile::encodeName(name), Opener::MaxStrLen);
  req.username[Opener::MaxStrLen] = '\0';
  strncpy(req.password, TQFile::encodeName(password), Opener::MaxStrLen);
  req.password[Opener::MaxStrLen] = '\0';
  sendRequest((struct RequestHeader *) &req, sizeof(req));
  return recvResponse();
}

bool Requester::removeSecret(int authMethod) {
  struct RemoveSecretRequest req;
  req.header.type = Opener::RemoveSecret;
  if(authMethod == AUTH_PAP)
    req.method = Opener::PAP;
  else
    if(authMethod == AUTH_CHAP)
      req.method = Opener::CHAP;
    else
      return false;

  sendRequest((struct RequestHeader *) &req, sizeof(req));
  return recvResponse();
}

bool Requester::setHostname(const TQString &name) {
  if (name.isEmpty())
    return false;
  struct SetHostnameRequest req;
  req.header.type = Opener::SetHostname;
  strncpy(req.name, TQFile::encodeName(name), Opener::MaxStrLen);
  req.name[Opener::MaxStrLen] = '\0';
  sendRequest((struct RequestHeader *) &req, sizeof(req));
  return recvResponse();
}


bool Requester::execPPPDaemon(const TQString &arguments) {
  struct ExecDaemonRequest req;
  req.header.type = Opener::ExecPPPDaemon;
  strncpy(req.arguments, TQFile::encodeName(arguments), MAX_CMDLEN);
  req.arguments[MAX_CMDLEN] = '\0';
  sendRequest((struct RequestHeader *) &req, sizeof(req));
  if(recvResponse()==0) {
    gpppdata.setpppdRunning(true);
    return true;
  } else
    return false;
}


bool Requester::killPPPDaemon() {
  struct KillDaemonRequest req;
  gpppdata.setpppdRunning(false);
  req.header.type = Opener::KillPPPDaemon;
  sendRequest((struct RequestHeader *) &req, sizeof(req));
  return recvResponse();
}

int Requester::pppdExitStatus()
{
  struct PPPDExitStatusRequest req;
  req.header.type = Opener::PPPDExitStatus;
  sendRequest((struct RequestHeader *) &req, sizeof(req));
  return recvResponse();
}

bool Requester::stop() {

  struct StopRequest req;
  req.header.type = Opener::Stop;
  sendRequest((struct RequestHeader *) &req, sizeof(req));

  //  return recvResponse();
  return true;
}


bool Requester::sendRequest(struct RequestHeader *request, int len) {

  request->len = len - sizeof(struct RequestHeader);

  struct msghdr	msg;
  struct iovec iov;

  iov.iov_base = IOV_BASE_CAST request;
  iov.iov_len = len;

  msg.msg_name = 0L;
  msg.msg_namelen = 0;
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;
  msg.msg_control = 0L;
  msg.msg_controllen = 0;
  kdDebug(5002) << "sendRequest: trying to send msg type " << request->type << endl;
  sendmsg(socket, &msg, 0);
  kdDebug(5002) << "sendRequest: sent message" << endl;

  return true;
}


int Requester::indexDevice(const TQString &dev) {

  int index = -1;

  for(int i = 0; devices[i]; i++)
    if (dev == devices[i])
      index = i;
  return index;
}


void recv_timeout(int) {
  kdDebug(5002) << "timeout()" << endl;
}