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 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
|
/*
* $Id: telephone_isp.c,v 1.26 2009-06-03 11:34:11 vrsieh Exp $
*
* Copyright (C) 2007-2009 FAUmachine Team <info@faumachine.org>.
* This program is free software. You can redistribute it and/or modify it
* under the terms of the GNU General Public License, either version 2 of
* the License, or (at your option) any later version. See COPYING.
*/
#include "config.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <signal.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <unistd.h>
#include "glue-io.h"
#include "glue-log.h"
#include "glue-main.h"
#include "telephone_isp.h"
/* ********************* DEFINITIONS ************************** */
#define COMP "telephone_isp"
#define SCRIPT_HEADER "Faum-ISP at your service\r\n"
#define SCRIPT_PROMPT_LOGIN "Login: "
#define SCRIPT_PROMPT_PASS "Password: "
enum isp_states {
STATE_OFFLINE,
STATE_CONNECTED,
STATE_AUTH
};
enum auth_states {
AUTH_UNTRIGGERED,
AUTH_WAIT_LOGIN,
AUTH_WAIT_PASS,
AUTH_SUCCESS,
AUTH_FAILURE
};
/* ****************** GLOBAL VARIABLES ************************ */
/* FIXME */
struct cpssp {
struct sig_telephone *port_phone;
int state_switch;
enum isp_states isp_state;
enum auth_states auth_state;
int auth_buf_pos;
pid_t child_pid;
int child_fd;
};
/* ******************** IMPLEMENTAION ************************* */
static int
isp_get_power_state(struct cpssp *cpssp)
{
return cpssp->state_switch;
}
static void
isp_send_tele(struct cpssp *cpssp, const uint8_t *data, int len) {
int i;
if (! isp_get_power_state(cpssp)) {
return;
}
if (cpssp->isp_state != STATE_AUTH) {
return;
}
for (i = 0; i < len; i++) {
sig_telephone_send_data(cpssp->port_phone, cpssp, data[i]);
}
}
static void
auth_handle_login(struct cpssp *cpssp, char *user, int len)
{
/* FIXME check user */
faum_log(FAUM_LOG_DEBUG, "node-isp", "", "sent user %s\n", user);
cpssp->auth_state=AUTH_WAIT_PASS;
isp_send_tele(cpssp, SCRIPT_PROMPT_PASS, strlen(SCRIPT_PROMPT_PASS));
}
static void
auth_handle_password(struct cpssp *cpssp, char *pass, int len)
{
/* FIXME check password */
faum_log(FAUM_LOG_DEBUG, "node-isp", "", "sent pass %s\n", pass);
cpssp->auth_state=AUTH_SUCCESS;
}
static void
auth_reset(struct cpssp *cpssp)
{
cpssp->auth_state = AUTH_UNTRIGGERED;
cpssp->auth_buf_pos = 0;
}
static bool
auth_create(struct cpssp *cpssp)
{
auth_reset(cpssp);
return true;
}
static bool
auth_push_char(struct cpssp *cpssp, unsigned char c)
{
static char buf[1024];
switch (cpssp->auth_state) {
case AUTH_UNTRIGGERED:
case AUTH_FAILURE:
case AUTH_SUCCESS:
faum_log(FAUM_LOG_DEBUG, "node-isp", "",
"invalid state of auth\n");
return false;
case AUTH_WAIT_PASS:
if ((c == '\n') || (c == '\r')) {
/* buffer empty? */
if (! cpssp->auth_buf_pos) {
return true;
}
auth_handle_password(cpssp, buf, cpssp->auth_buf_pos);
cpssp->auth_buf_pos=0;
/* done with it! */
return false;
} else {
buf[cpssp->auth_buf_pos++] = c;
assert(cpssp->auth_buf_pos < 1024);
isp_send_tele(cpssp, &c, 1);
}
return true;
break;
case AUTH_WAIT_LOGIN:
if ((c == '\n') || (c == '\r')) {
/* buffer empty? */
if (! cpssp->auth_buf_pos) {
return true;
}
isp_send_tele(cpssp, "\r\n", 2);
auth_handle_login(cpssp, buf, cpssp->auth_buf_pos);
cpssp->auth_buf_pos=0;
return true;
} else {
buf[cpssp->auth_buf_pos++] = c;
assert(cpssp->auth_buf_pos < 1024);
isp_send_tele(cpssp, &c, 1);
}
return true;
break;
}
return true;
}
static bool
auth_get_state(struct cpssp *cpssp)
{
switch (cpssp->auth_state) {
case AUTH_SUCCESS:
return true;
case AUTH_FAILURE:
return false;
case AUTH_UNTRIGGERED:
case AUTH_WAIT_LOGIN:
case AUTH_WAIT_PASS:
faum_log(FAUM_LOG_DEBUG, "node-isp", "",
"external state UNDEFINED!\n");
}
return false;
}
static void
auth_trigger(struct cpssp *cpssp)
{
unsigned char backbuf[1024];
sprintf(backbuf, "%s%s", SCRIPT_HEADER, SCRIPT_PROMPT_LOGIN);
isp_send_tele(cpssp, backbuf, strlen(backbuf));
cpssp->auth_state=AUTH_WAIT_LOGIN;
}
#if 0 /* sig_child handled by glue-main already. */
static void
sig_sigchild(int signum)
{
int ret;
int status;
ret = waitpid(child_pid, &status, WNOHANG);
if (ret && (ret == child_pid)) {
if (isp_state != STATE_OFFLINE) {
/* child died unexpected... send hangup to peer */
isp_state=STATE_OFFLINE;
sig_telephone_send_ctrl(cpssp_port_phone, NULL,
SIG_TELE_HANGUP);
}
if (child_fd) {
io_unregister(child_fd);
close(child_fd);
child_fd = 0;
}
}
}
#endif
static void
slirp_recv(int fd, void *_cpssp)
{
static uint8_t buf[2000];
ssize_t ret, i;
struct cpssp *cpssp = (struct cpssp*)_cpssp;
if (! isp_get_power_state(cpssp)) {
return;
}
if (cpssp->isp_state != STATE_CONNECTED) {
return;
}
for (;;) {
errno = 0;
ret = read(fd, buf, sizeof(buf));
if (ret < 0 && errno == EAGAIN) {
break;
}
if (ret == 0) {
faum_log(FAUM_LOG_WARNING, "node-isp", "slirp",
"broken pipe?\n");
break;
}
for (i = 0; i < ret; i++) {
sig_telephone_send_data(cpssp->port_phone, cpssp,
buf[i]);
}
}
}
static void
sanitize_locale(void)
{
unsetenv("LANG");
unsetenv("LC_CTYPE");
unsetenv("LC_NUMERIC");
unsetenv("LC_TIME");
unsetenv("LC_COLLATE");
unsetenv("LC_MONETARY");
unsetenv("LC_MESSAGES");
unsetenv("LC_PAPER");
unsetenv("LC_NAME");
unsetenv("LC_ADDRESS");
unsetenv("LC_TELEPHONE");
unsetenv("LC_MEASUREMENT");
unsetenv("LC_IDENTIFICATION");
unsetenv("LC_ALL");
}
static void
launch_slirp(struct cpssp *cpssp)
{
int ret;
int iopipe[2];
ret = socketpair(PF_UNIX, SOCK_STREAM, 0, iopipe);
if (ret < 0) {
perror("pipe");
/* send hangup to peer */
sig_telephone_send_ctrl(cpssp->port_phone, cpssp,
SIG_TELE_HANGUP);
cpssp->isp_state=STATE_OFFLINE;
auth_reset(cpssp);
return;
}
cpssp->child_pid = fork();
switch (cpssp->child_pid) {
case 0: /* child */
sanitize_locale(); /* FIXME needed? */
ret = dup2(iopipe[0], 0);
assert(0 <= ret);
ret = close(1);
assert(0 <= ret);
ret = open("/dev/null", O_WRONLY);
assert(ret == 1);
ret = close(iopipe[0]);
assert(0 <= ret);
ret = close(iopipe[1]);
assert(0 <= ret);
/* launch slirp */
ret = execlp("slirp", "slirp", "ppp" ,"mtu 1400", NULL);
/* we shouldn't get here */
if (ret == -1) {
perror("could not execute\n");
faum_log(FAUM_LOG_CRITICAL, "node-isp", "slirp",
"Couldn't find slirp.\n");
}
exit(1);
break;
case -1: /* fork not possible */
faum_log(FAUM_LOG_DEBUG, "node-isp", "",
"slirp fork failed\n");
close(iopipe[0]);
close(iopipe[1]);
/* send hangup to peer */
sig_telephone_send_ctrl(cpssp->port_phone, cpssp,
SIG_TELE_HANGUP);
cpssp->isp_state=STATE_OFFLINE;
auth_reset(cpssp);
return;
default: /* parent */
ret = close(iopipe[0]);
assert(0 <= ret);
cpssp->child_fd = iopipe[1];
io_register(cpssp->child_fd, cpssp, slirp_recv);
cpssp->isp_state = STATE_CONNECTED;
break;
}
/* now we reach ONLINE state */
}
static void
kill_slirp(struct cpssp *cpssp)
{
int ret;
int status;
if (cpssp->child_pid) {
ret = kill(cpssp->child_pid, SIGKILL);
if (ret == -1) {
perror("could not kill slirp:");
}
ret = waitpid(cpssp->child_pid, &status, WNOHANG);
cpssp->child_pid=0;
if (cpssp->child_fd != -1) {
io_unregister(cpssp->child_fd);
ret = close(cpssp->child_fd);
assert(ret >= 0);
cpssp->child_fd = -1;
}
}
}
static void
isp_reset(struct cpssp *cpssp)
{
if (cpssp->isp_state != STATE_OFFLINE) {
sig_telephone_send_ctrl(cpssp->port_phone, cpssp,
SIG_TELE_HANGUP);
}
cpssp->isp_state = STATE_OFFLINE;
kill_slirp(cpssp);
auth_reset(cpssp);
}
static void
isp_handle_conn_event(struct cpssp *cpssp, enum sig_telephone_protocol event)
{
switch(event) {
case SIG_TELE_BUSY:
/* peer hung up */
cpssp->isp_state = STATE_OFFLINE;
isp_reset(cpssp);
return;
default:
faum_log(FAUM_LOG_DEBUG, "node-isp", __func__,
"invalid protocol!\n");
}
}
static void
isp_handle_off_event(struct cpssp *cpssp, enum sig_telephone_protocol event)
{
if (event != SIG_TELE_RING) {
faum_log(FAUM_LOG_DEBUG, "node-isp", __func__,
"invalid protocol %d!\n", event);
return;
}
cpssp->isp_state=STATE_AUTH;
sig_telephone_send_ctrl(cpssp->port_phone, cpssp, SIG_TELE_LIFT);
auth_trigger(cpssp);
}
static void
isp_handle_auth_event(struct cpssp *cpssp, enum sig_telephone_protocol event)
{
if (event == SIG_TELE_BUSY) {
/* peer hung up */
isp_reset(cpssp);
return;
}
/* invalid */
faum_log(FAUM_LOG_WARNING, "node-isp", __func__,
"invalid protocol=%d\n", event);
}
static void
slirp_send(struct cpssp *cpssp, uint8_t data)
{
int ret;
if (cpssp->child_fd == -1) {
faum_log(FAUM_LOG_WARNING, "node-isp", "slirp",
"slirp fd is closed, dropping data.\n");
return;
}
ret = io_write(cpssp->child_fd, &data, sizeof(data));
if (ret != sizeof(data)) {
faum_log(FAUM_LOG_WARNING, COMP, __func__,
"dropped one byte to slirp\n");
}
}
static void
isp_recv_data(void *_cpssp, uint8_t data)
{
bool more;
struct cpssp *cpssp = (struct cpssp*)_cpssp;
if (! isp_get_power_state(cpssp)) {
return;
}
switch (cpssp->isp_state) {
case STATE_AUTH:
more = auth_push_char(cpssp, data);
if (! more) {
/* authentication complete */
if (auth_get_state(cpssp)) {
cpssp->isp_state = STATE_CONNECTED;
launch_slirp(cpssp);
} else {
/* authentication failed. */
auth_reset(cpssp);
cpssp->isp_state = STATE_OFFLINE;
sig_telephone_send_ctrl(cpssp->port_phone,
cpssp,
SIG_TELE_HANGUP);
}
}
break;
case STATE_CONNECTED:
/* forward data to slirp */
slirp_send(cpssp, data);
break;
default:
faum_log(FAUM_LOG_WARNING, "node-isp", "",
"received data in wrong state.\n");
break;
}
}
static void
isp_recv_ctrl(void *_cpssp, enum sig_telephone_protocol event)
{
struct cpssp *cpssp = (struct cpssp*)_cpssp;
if (! isp_get_power_state(cpssp)) {
return;
}
switch (cpssp->isp_state) {
case STATE_CONNECTED:
isp_handle_conn_event(cpssp, event);
break;
case STATE_OFFLINE:
isp_handle_off_event(cpssp, event);
break;
case STATE_AUTH:
isp_handle_auth_event(cpssp, event);
break;
}
}
static void
isp_recv_dial(void *s, uint32_t number)
{
faum_log(FAUM_LOG_WARNING, "node-isp", "",
"received invalid dial event.\n");
}
static void
isp_power_set(void *_cpssp, unsigned int val)
{
struct cpssp *cpssp = (struct cpssp *)_cpssp;
cpssp->state_switch = val;
if (! val) {
/* on -> off */
isp_reset(cpssp);
}
}
void *
telephone_isp_create(
const char *name,
struct sig_manage *port_manage,
struct sig_telephone *port_phone,
struct sig_boolean *port_switch
)
{
static const struct sig_telephone_funcs tf = {
.recv_data = isp_recv_data,
.recv_ctrl = isp_recv_ctrl,
.recv_dial = isp_recv_dial
};
static struct sig_boolean_funcs tpf = {
.set = isp_power_set
};
struct cpssp *cpssp;
cpssp = malloc(sizeof(*cpssp));
assert(cpssp);
auth_create(cpssp);
cpssp->port_phone = port_phone;
sig_telephone_connect(port_phone, cpssp, &tf);
sig_boolean_connect_in(port_switch, cpssp, &tpf);
cpssp->isp_state = STATE_OFFLINE;
cpssp->child_pid = 0;
cpssp->child_fd = -1;
return cpssp;
}
void
telephone_isp_destroy(void *_cpssp)
{
struct cpssp *cpssp = _cpssp;
kill_slirp(cpssp);
free(cpssp);
}
|