summaryrefslogtreecommitdiff
path: root/tools/database/SOURCES (follow)
Commit message (Expand)AuthorAge
* Revert r26048. APE tags in mp3 is explicitely on http://www.rockbox.org/wiki/...Frank Gevaerts2010-05-15
* mp3: when ID3 tags are not found, search APE tagsYoshihisa Uchida2010-05-15
* ID3 tags parser separates from metadata/mp3.cYoshihisa Uchida2010-05-15
* Revert r25854 which was bad for the database tool (I forgot it still needs th...Thomas Martitz2010-05-06
* tools/database: build process updates.Yoshihisa Uchida2010-02-25
* sorry, I forget tools/database/SOURCES.Yoshihisa Uchida2010-02-24
* Fix red caused by r24615Michael Chicoine2010-02-12
* Make the database tool buildable from configure.Frank Gevaerts2009-10-07
ref='#n98'>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 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
/*
 *   NetCosm - a MUD server
 *   Copyright (C) 2016 Franklin Wei
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 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 General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "globals.h"

#include "auth.h"
#include "client.h"
#include "hash.h"
#include "server.h"
#include "room.h"
#include "telnet.h"
#include "util.h"

static bool admin = false;

static int client_fd, to_parent, from_parent;

static room_id current_room = 0;

static volatile sig_atomic_t output_locked = 0;

char *current_user = NULL;

bool poll_requests(void);

void out_raw(const void *buf, size_t len)
{
    if(!len)
        return;

try_again:

    while(output_locked);

    /* something weird happened and the value changed between the loop and here */
    if(!output_locked)
    {
        output_locked = 1;
        write(client_fd, buf, len);
        output_locked = 0;
    }
    else
        goto try_again;
}

void __attribute__((format(printf,1,2))) out(const char *fmt, ...)
{
    char buf[1024];
    memset(buf, 0, sizeof(buf));
    va_list ap;
    va_start(ap, fmt);

    vsnprintf(buf, sizeof(buf), fmt, ap);

    va_end(ap);

    /* do some line wrapping */

    int pos = 0, last_space = 0;
    char *ptr = buf;
    uint16_t line_width = telnet_get_width() + 1;
    char *line_buf = malloc(line_width + 2);
    size_t line_idx = 0;
    while(ptr[pos])
    {
        bool is_newline = (ptr[pos] == '\n');
        if(is_newline || pos >= line_width)
        {
            if(is_newline || !last_space)
                last_space = pos;

            while(*ptr && last_space-- > 0)
            {
                line_buf[line_idx++] = *ptr++;
            }

            line_buf[line_idx++] = '\r';
            line_buf[line_idx++] = '\n';

            out_raw(line_buf, line_idx);
            line_idx = 0;

            if(is_newline)
                ++ptr; /* skip the newline */
            while(*ptr == ' ')
                ++ptr;
            last_space = 0;
            pos = 0;
        }
        else
        {
            if(ptr[pos] == ' ')
                last_space = pos;
            ++pos;
        }
    }
    out_raw(ptr, strlen(ptr));
    free(line_buf);
}

static volatile sig_atomic_t request_complete;

/* for rate-limiting */
static int reqs_since_ts;
static time_t ts = 0;

void send_master(unsigned char cmd, const void *data, size_t sz)
{
    if(!admin)
    {
        time_t t = time(NULL);
        if(ts != t)
        {
            ts = t;
            reqs_since_ts = 0;
        }
        if(reqs_since_ts++ > 10)
        {
            out("Rate limit exceeded.\n");
            return;
        }
    }

    request_complete = 0;

    pid_t our_pid = getpid();

    if(!data)
        sz = 0;

    /* format of child->parent packets:
     * | PID | CMD | DATA |
     */

    /* pack it all into one write so it's atomic */
    char *req = malloc(sizeof(pid_t) + 1 + sz);

    memcpy(req, &our_pid, sizeof(pid_t));
    memcpy(req + sizeof(pid_t), &cmd, 1);
    memcpy(req + sizeof(pid_t) + 1, data, sz);

    write(to_parent, req, 1 + sizeof(pid_t) + sz);

    /* poll till we get data */
    struct pollfd pfd;
    pfd.fd = from_parent;
    pfd.events = POLLIN;

    poll(&pfd, 1, -1);

    while(!request_complete) poll_requests();

    free(req);

    debugf("done with request\n");
}

#define BUFSZ 128

char *client_read(void)
{
    char *buf;
    size_t bufidx;
tryagain:

    buf = malloc(BUFSZ);
    bufidx = 0;

    memset(buf, 0, BUFSZ);

    /* set of the client fd and the pipe from our parent */
    struct pollfd fds[2];

    /* order matters here: we first fulfill parent requests, then
     * handle client data */

    fds[0].fd = from_parent;
    fds[0].events = POLLIN;

    fds[1].fd = client_fd;
    fds[1].events = POLLIN;

    while(1)
    {
        poll(fds, ARRAYLEN(fds), -1);
        for(int i = 0; i < 2; ++i)
        {
            if(fds[i].revents == POLLIN)
            {
                if(fds[i].fd == from_parent)
                {
                    poll_requests();
                }
                else if(fds[i].fd == client_fd)
                {
                    ssize_t len = read(client_fd, buf + bufidx, BUFSZ - bufidx - 1);
                    if(len < 0)
                        error("lost connection");

                    buf[BUFSZ - 1] = '\0';

                    enum telnet_status ret = telnet_parse_data((unsigned char*)buf + bufidx, len);

                    switch(ret)
                    {
                    case TELNET_EXIT:
                    case TELNET_FOUNDCMD:
                        free(buf);
                        if(ret == TELNET_EXIT)
                            exit(0);
                        goto tryagain;
                    case TELNET_DATA:
                        bufidx += len;
                        continue;
                    case TELNET_LINEOVER:
                        break;
                    }

                    remove_cruft(buf);

                    return buf;
                }
            }
        }
    }
}

/* still not encrypted, but a bit more secure than echoing the password! */
char *client_read_password(void)
{
    telnet_echo_off();
    char *ret = client_read();
    telnet_echo_on();
    out("\n");
    return ret;
}

enum reqdata_typespec reqdata_type = TYPE_NONE;
union reqdata_t returned_reqdata;

void read_string_max(int fd, char *buf, size_t max)
{
    size_t len;
    if(read(fd, &len, sizeof(len)) != sizeof(len))
        error("read_string_max");
    if(len > max - 1)
        error("read_string_max");
    if(read(fd, buf, len) != (int)len)
        error("unexpected EOF");
    buf[max - 1] = '\0';
}

bool poll_requests(void)
{
    if(!are_child)
        return false;

    bool got_cmd = false;

    while(1)
    {
        unsigned char packet[MSG_MAX + 1];
        memset(packet, 0, sizeof(packet));

        ssize_t packetlen = read(from_parent, packet, MSG_MAX);

        unsigned char *data = packet + 1;
        size_t datalen = packetlen - 1;
        packet[MSG_MAX] = '\0';

        if(packetlen <= 0)
            goto fail;

        got_cmd = true;

        unsigned char cmd = packet[0];

        debugf("Child gets code %d\n", cmd);

        switch(cmd)
        {
        case REQ_BCASTMSG:
        {
            out((char*)data, datalen);
            break;
        }
        case REQ_KICK:
        {
            out((char*)data, datalen);
            exit(EXIT_SUCCESS);
        }
        case REQ_MOVE:
        {
            int status = *((int*)data);

            reqdata_type = TYPE_BOOLEAN;
            returned_reqdata.boolean = status;
            if(!status)
                out("Cannot go that way.\n");
            break;
        }
        case REQ_GETUSERDATA:
        {
            if(datalen == sizeof(struct userdata_t))
                reqdata_type = TYPE_USERDATA;
            else
                break;

            struct userdata_t *user = &returned_reqdata.userdata;
            *user = *((struct userdata_t*)data);
            break;
        }
        case REQ_DELUSERDATA:
        {
            reqdata_type = TYPE_BOOLEAN;
            returned_reqdata.boolean = *((bool*)data);
            break;
        }
        case REQ_ADDUSERDATA:
        {
            reqdata_type = TYPE_BOOLEAN;
            returned_reqdata.boolean = *((bool*)data);
            break;
        }
        case REQ_NOP:
            break;
        case REQ_PRINTNEWLINE:
        {
            out("\n");
            break;
        }
        case REQ_ALLDONE:
            request_complete = 1;
            return true;
        default:
            debugf("WARNING: client process received unknown code %d\n", cmd);
            break;
        }
    }
fail:

    return got_cmd;
}

void client_change_state(int state)
{
    send_master(REQ_CHANGESTATE, &state, sizeof(state));
}

void client_change_user(const char *user)
{
    send_master(REQ_CHANGEUSER, user, strlen(user) + 1);
}

void client_change_room(room_id id)
{
    send_master(REQ_SETROOM, &id, sizeof(id));
}

void *dir_map = NULL;

void client_move(const char *dir)
{
    const struct dir_pair {
        const char *text;
        enum direction_t val;
    } dirs[] = {
        {  "N",          DIR_N     },
        {  "NORTH",      DIR_N     },
        {  "NE",         DIR_NE    },
        {  "NORTHEAST",  DIR_N     },
        {  "E",          DIR_E     },
        {  "EAST",       DIR_E     },
        {  "SE",         DIR_SE    },
        {  "SOUTHEAST",  DIR_SE    },
        {  "S",          DIR_S     },
        {  "SOUTH",      DIR_S     },
        {  "SW",         DIR_SW    },
        {  "SOUTHWEST",  DIR_SW    },
        {  "W",          DIR_W     },
        {  "WEST",       DIR_W     },
        {  "NW",         DIR_NW    },
        {  "NORTHWEST",  DIR_NW    },
        {  "U",          DIR_UP    },
        {  "UP",         DIR_UP    },
        {  "D",          DIR_DN    },
        {  "DOWN",       DIR_DN    },
        {  "IN",         DIR_IN    },
        {  "OUT",        DIR_OT    },
    };
    if(!dir_map)
    {
        dir_map = hash_init(ARRAYLEN(dirs), hash_djb, compare_strings);
        hash_insert_pairs(dir_map, (struct hash_pair*)dirs, sizeof(struct dir_pair), ARRAYLEN(dirs));
    }

    struct dir_pair *pair = hash_lookup(dir_map, dir);
    if(pair)
    {
        send_master(REQ_MOVE, &pair->val, sizeof(pair->val));
    }
    else
        out("Unknown direction.\n");
}

void client_look(void)
{
    send_master(REQ_GETROOMNAME, NULL, 0);
    out("\n");
    send_master(REQ_GETROOMDESC, NULL, 0);
}

#define WSPACE " \t\r\n"

void client_main(int fd, struct sockaddr_in *addr, int total, int to, int from)
{
    client_fd = fd;
    to_parent = to;
    from_parent = from;

    output_locked = 0;

    telnet_init();

    char *ip = inet_ntoa(addr->sin_addr);
    debugf("New client %s\n", ip);
    debugf("Total clients: %d\n", total);

    debugf("client is running with uid %d\n", getuid());

auth:

    out("NetCosm " NETCOSM_VERSION "\n");
    if(total > 1)
        out("%d clients connected.\n", total);
    else
        out("%d client connected.\n", total);

    out("\nPlease authenticate to continue.\n\n");

    int failures = 0;

    int authlevel;
    struct userdata_t *current_data = NULL;

    client_change_state(STATE_AUTH);

    /* auth loop */
    while(1)
    {
        out("login: ");

        current_user = client_read();
        remove_cruft(current_user);

        out("Password: ");

        char *pass = client_read_password();

        client_change_state(STATE_CHECKING);

        current_data = auth_check(current_user, pass);

        memset(pass, 0, strlen(pass));
        free(pass);

        if(current_data)
        {
            out("Last login: %s", ctime(&current_data->last_login));
            current_data->last_login = time(0);
            authlevel = current_data->priv;
            userdb_request_add(current_data);
            break;
        }
        else
        {
            client_change_state(STATE_FAILED);
            free(current_user);
            current_user = NULL;
            out("Login incorrect\n\n");
            if(++failures >= MAX_FAILURES)
                return;
        }
    }

    /* something has gone wrong, but we are here for some reason */
    if(authlevel == PRIV_NONE)
        return;

    admin = (authlevel == PRIV_ADMIN);
    if(admin)
        client_change_state(STATE_ADMIN);
    else
        client_change_state(STATE_LOGGEDIN);

    /* authenticated */
    debugf("client: Authenticated as %s\n", current_user);
    client_change_user(current_user);
    current_room = 0;
    client_change_room(current_room);
    client_look();
    while(1)
    {
        out(">> ");
        char *cmd = client_read();

        char *save = NULL;

        char *tok = strtok_r(cmd, WSPACE, &save);

        if(!tok)
            goto next_cmd;
        all_upper(tok);

        if(admin)
        {
            if(!strcmp(tok, "USER"))
            {
                char *what = strtok_r(NULL, WSPACE, &save);
                if(!what)
                    goto next_cmd;

                all_upper(what);

                if(!strcmp(what, "DEL"))
                {
                    char *user = strtok_r(NULL, WSPACE, &save);
                    if(user)
                    {
                    if(strcmp(user, current_user) && auth_user_del(user))
                        out("Success.\n");
                    else
                        out("Failure.\n");
                    }
                    else
                    {
                        out("Usage: USER DEL <USERNAME>\n");
                    }
                }
                else if(!strcmp(what, "ADD") || !strcmp(what, "MODIFY"))
                {
                    char *user = strtok_r(NULL, WSPACE, &save);
                    if(user)
                    {
                        if(!strcmp(user, current_user))
                        {
                            out("Do not modify your own password using USER. User CHPASS instead.\n");
                            goto next_cmd;
                        }

                        out("Editing user '%s'\n", user);

                        out("New Password (_DO_NOT_USE_A_VALUABLE_PASSWORD_): ");

                        /* BAD BAD BAD BAD BAD BAD BAD CLEARTEXT PASSWORDS!!! */
                        char *pass = client_read_password();

                        out("Verify Password: ");
                        char *pass2 = client_read_password();

                        if(strcmp(pass, pass2))
                        {
                            memset(pass, 0, strlen(pass));
                            memset(pass2, 0, strlen(pass2));
                            free(pass);
                            free(pass2);
                            out("Failure.\n");
                            goto next_cmd;
                        }

                        out("Admin privileges [y/N]? ");
                        char *allow_admin = client_read();
                        int priv = PRIV_USER;
                        if(toupper(allow_admin[0]) == 'Y')
                            priv = PRIV_ADMIN;

                        free(allow_admin);

                        if(auth_user_add(user, pass, priv))
                            out("Success.\n");
                        else
                            out("Failure.\n");
                        memset(pass, 0, strlen(pass));
                        free(pass);
                    }
                    else
                        out("Usage: USER <ADD|MODIFY> <USERNAME>\n");
                }
                else if(!strcmp(what, "LIST"))
                {
                    auth_user_list();
                }
                else
                {
                    out("Usage: USER <ADD|DEL|MODIFY|LIST> <ARGS>\n");
                }
            }
            else if(!strcmp(tok, "CLIENT"))
            {
                char *what = strtok_r(NULL, WSPACE, &save);
                if(!what)
                {
                    out("Usage: CLIENT <LIST|KICK> <PID>\n");
                    goto next_cmd;
                }

                all_upper(what);

                if(!strcmp(what, "LIST"))
                {
                    send_master(REQ_LISTCLIENTS, NULL, 0);
                }
                else if(!strcmp(what, "KICK"))
                {
                    char *pid_s = strtok_r(NULL, WSPACE, &save);
                    all_upper(pid_s);
                    if(pid_s)
                    {
                        if(!strcmp(pid_s, "ALL"))
                        {
                            const char *msg = "Kicking everyone...\n";
                            send_master(REQ_KICKALL, msg, strlen(msg));
                            goto next_cmd;
                        }
                        /* weird pointer voodoo */
                        /* TODO: simplify */
                        char pidbuf[MAX(sizeof(pid_t), MSG_MAX)];
                        char *end;
                        pid_t pid = strtol(pid_s, &end, 0);
                        if(pid == getpid())
                        {
                            out("You cannot kick yourself. Use EXIT instead.\n");
                            goto next_cmd;
                        }
                        else if(*end != '\0')
                        {
                            out("Expected a child PID after KICK.\n");
                            goto next_cmd;
                        }
                        memcpy(pidbuf, &pid, sizeof(pid));
                        int len = sizeof(pid_t) + snprintf(pidbuf + sizeof(pid_t),
                                                           sizeof(pidbuf) - sizeof(pid_t),
                                                           "You were kicked.\n");
                        send_master(REQ_KICK, pidbuf, len);
                        debugf("Success.\n");
                    }
                    else
                        out("Usage: CLIENT KICK <PID>\n");
                }
            }
            //else if(!strcmp(tok, "HANG"))
            //{
            //    send_master(REQ_HANG);
            //}
        }

        if(!strcmp(tok, "QUIT") || !strcmp(tok, "EXIT"))
        {
            free(cmd);
            goto done;
        }
        else if(!strcmp(tok, "SAY"))
        {
            char buf[MSG_MAX];
            char *what = strtok_r(NULL, "", &save);
            int len = snprintf(buf, sizeof(buf), "%s says %s\n", current_user, what);

            send_master(REQ_BCASTMSG, buf, len);
        }
        else if(!strcmp(tok, "DATE"))
        {
            time_t t = time(NULL);
            out("%s", ctime(&t));
        }
        else if(!strcmp(tok, "LOGOUT"))
        {
            out("Logged out.\n");
            goto auth;
        }
        else if(!strcmp(tok, "LOOK"))
        {
            client_look();
        }
        else if(!strcmp(tok, "WAIT"))
        {
            send_master(REQ_WAIT, NULL, 0);
        }
        else if(!strcmp(tok, "GO"))
        {
            char *dir = strtok_r(NULL, WSPACE, &save);
            if(dir)
            {
                all_upper(dir);
                client_move(dir);
                client_look();
            }
            else
                out("Expected direction after GO.\n");
        }

    next_cmd:

        free(cmd);
    }

done:
    free(current_user);
    current_user = NULL;
}