Skip to content
This repository
tree: 2dea6d8c91
Fetching contributors…

Cannot retrieve contributors at this time

file 681 lines (558 sloc) 15.554 kb
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 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
/*
* Copyright (c) 2011, Edd Barrett <vext01@gmail.com>
* Copyright (c) 2011, Martin Ellis <ellism88@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#define _GNU_SOURCE /* linux */

#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <err.h>
#include <libgen.h>

#ifdef __linux__
#include <bsd/readpassphrase.h>
#else
#include <readpassphrase.h>
#endif

#include "client.h"
#include "config.h"
#include "hgd.h"
#include "net.h"
#include "user.h"
#ifdef HAVE_LIBCONFIG
#include "cfg.h"
#endif

char *user = NULL, *host = NULL, *password = NULL;
int port = HGD_DFL_PORT;
int sock_fd = -1;
uint8_t max_playlist_items = 0;

SSL *ssl = NULL;
SSL_METHOD *method;
SSL_CTX *ctx;
uint8_t crypto_pref = HGD_CRYPTO_PREF_IF_POSS;
uint8_t server_ssl_capable = 0;
uint8_t authenticated = 0;
uint8_t hud_refresh_speed = 5;
uint8_t colours_on = 1;

struct hgd_resp_err hgd_resp_errs[] = {
{ "E_INT", "Internal error" },
{ "E_DENY", "Access denied" },
{ "E_FLSIZE", "File size invalid" },
{ "E_FLOOD", "Flood protect triggered" },
{ "E_NOPLAY", "No track is playing" },
{ "E_WRTRK", "Wrong track" },
{ "E_DUPVOTE", "Duplicate vote" },
{ "E_SSLAGN", "Duplicate SSL negotiation" },
{ "E_SSLNOAVAIL", "SSL not available" },
{ "E_INVCMD", "Invalid command" },
{ "E_SSLREQ", "SSL required" },
{ "E_SHTDWN", "Server is going down" },
{ "E_KICK", "Client misbehaving" },
{ "E_PERMNOCHG", "Perms did not change" },
{ "E_USREXIST", "User already exists" },
{ "E_USRNOEXIST", "User does not exist" },
{ 0, 0 }
};

int
hgd_client_edit_config()
{
char *path = NULL, *edit_cmd = NULL;
char *editor = getenv("EDITOR");
int ret = HGD_FAIL;

if (editor == NULL)
editor = HGD_DFL_EDITOR;

xasprintf(&path, "%s/%s/%s", getenv("HOME"),
HGD_USR_CFG_DIR, HGD_CLI_CFG);

xasprintf(&edit_cmd, "%s %s", editor, path);

if (system(edit_cmd) != 0)
goto clean;

ret = HGD_OK;
clean:
free(edit_cmd);
free(path);

return (ret);
}

int
hgd_client_login(int fd, SSL *ssl, char *username)
{
char *resp, *user_cmd, pass[HGD_MAX_PASS_SZ];
int login_ok = -1;
char *prompt;

/* XXX can't use getpassphrase in curses */
if (password == NULL) {
xasprintf(&prompt, "Password for %s@%s: ", user, host);
if (readpassphrase(prompt, pass, HGD_MAX_PASS_SZ,
RPP_ECHO_OFF | RPP_REQUIRE_TTY) == NULL) {
DPRINTF(HGD_D_ERROR, "Problem reading password from user");
memset(pass, 0, HGD_MAX_PASS_SZ);
free(prompt);
return (HGD_FAIL);
}
free(prompt);
} else {
strncpy(pass, password, HGD_MAX_PASS_SZ);
if (HGD_MAX_PASS_SZ > 0)
pass[HGD_MAX_PASS_SZ-1] = '\0';
}

/* send password */
xasprintf(&user_cmd, "user|%s|%s", username, pass);
hgd_sock_send_line(fd, ssl, user_cmd);
memset(pass, 0, HGD_MAX_PASS_SZ);
free(user_cmd);

resp = hgd_sock_recv_line(fd, ssl);
login_ok = hgd_check_svr_response(resp, 0);

free(resp);

if (login_ok == HGD_OK) {
authenticated = 1;
DPRINTF(HGD_D_DEBUG, "Identified as %s", user);
} else
DPRINTF(HGD_D_WARN, "Login as %s failed", user);

return (login_ok);
}

int
hgd_setup_socket()
{
struct sockaddr_in addr;
char* resp;
struct hostent *he;
int sockopt = 1, ret = HGD_OK;

DPRINTF(HGD_D_DEBUG, "Connecting to %s", host);

/* if they gave a hostname, we look up the IP */
if (!hgd_is_ip_addr(host)) {
DPRINTF(HGD_D_DEBUG, "Looking up host '%s'", host);
he = gethostbyname(host);
if (he == NULL) {
DPRINTF(HGD_D_ERROR,
"Failure in hostname resolution: '%s'", host);
ret = HGD_FAIL;
goto clean;
}

free(host);
host = xstrdup(
inet_ntoa( *(struct in_addr*)(he->h_addr_list[0])));
DPRINTF(HGD_D_DEBUG, "Found IP %s", host);
}

DPRINTF(HGD_D_DEBUG, "Connecting to IP %s:%d", host, port);

/* set up socket address */
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(host);
addr.sin_port = htons(port);

sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (sock_fd < 0) {
DPRINTF(HGD_D_ERROR, "can't make socket: %s", SERROR);
ret = HGD_FAIL;
goto clean;
}

if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR,
&sockopt, sizeof(sockopt)) < 0) {
DPRINTF(HGD_D_WARN, "Can't set SO_REUSEADDR");
}

if (connect(sock_fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
close(sock_fd);
DPRINTF(HGD_D_ERROR, "Can't connect to %s", host);
ret = HGD_FAIL;
goto clean;
}

/* expect a hello message */
resp = hgd_sock_recv_line(sock_fd, ssl);
hgd_check_svr_response(resp, 1);
free(resp);

DPRINTF(HGD_D_DEBUG, "Connected to %s", host);

/* identify ourselves */
if (user == NULL) {
/* If the user did not set their name use thier system login */
user = getenv("USER");
}
if (user == NULL) {
DPRINTF(HGD_D_ERROR, "can't get username");
ret = HGD_FAIL;
goto clean;
}

hgd_negotiate_crypto();
if ((server_ssl_capable) && (crypto_pref != HGD_CRYPTO_PREF_NEVER)) {
if (hgd_encrypt(sock_fd) != HGD_OK) {
ret = HGD_FAIL;
goto clean;
}
}

/* annoying error message for those too lazy to set up crypto */
if (ssl == NULL)
DPRINTF(HGD_D_WARN, "Connection is not encrypted");

clean:
return (ret);
}

/*
* if x == 1 you do not need to check the return value of this method as
* hgd will have exited before this returns.
*/
int
hgd_check_svr_response(char *resp, uint8_t x)
{
int err = HGD_OK;
char *trunc = NULL;

if (resp == NULL) {
DPRINTF(HGD_D_ERROR, "failed to read server response");
err = HGD_FAIL;
goto clean;
}

if (hgd_debug) {
trunc = xstrdup(resp);
DPRINTF(HGD_D_DEBUG, "Check reponse '%s'", trunc);
free(trunc);
}

if (strncmp(resp, "ok", 2) == 0) {
/* great */
} else if (strncmp(resp, "err", 3)) {
DPRINTF(HGD_D_ERROR, "Malformed server response");
} else {
/* we got an 'err' */
hgd_print_pretty_server_response(resp);
err = HGD_FAIL;
}

clean:
/* frees reposonse on error and exit */
if ((err == HGD_FAIL) && (x)) {
free(resp);
hgd_exit_nicely();
}

return (err);
}

/*
* see if the server supports encryption
* return: 1 = yes, 0 = no, -1 = error
*/
int
hgd_negotiate_crypto()
{
int n_toks = 0, ret = HGD_OK;
char *first, *next;
char *ok_tokens[2] = {"", ""};

if (crypto_pref == HGD_CRYPTO_PREF_NEVER)
return (0); /* fine, no crypto then */

hgd_sock_send_line(sock_fd, NULL, "encrypt?");
first = next = hgd_sock_recv_line(sock_fd, NULL);

hgd_check_svr_response(next, 1);

do {
ok_tokens[n_toks] = strsep(&next, "|");
n_toks++;
} while ((n_toks < 2) && (next != NULL));

if (strcmp(ok_tokens[1], "tlsv1") == 0) {
server_ssl_capable = 1;
DPRINTF(HGD_D_INFO, "Server supports %s crypto", ok_tokens[1]);
}

if ((!server_ssl_capable) && (crypto_pref == HGD_CRYPTO_PREF_ALWAYS)) {
DPRINTF(HGD_D_ERROR,
"User forced crypto, but server is incapable");
ret = HGD_FAIL;
}

free(first);

return (ret);
}

int
hgd_encrypt(int fd)
{
int ssl_res = 0;
char *ok_str = NULL;
X509 *cert;

/* XXX For semi-implemented certificate verification - FAO mex */
#if 0
X509_NAME *cert_name;
EVP_PKEY *public_key;
BIO *bio;
#endif
hgd_sock_send_line(fd, NULL, "encrypt");

if (hgd_setup_ssl_ctx(&method, &ctx, 0, 0, 0) != 0) {
return (HGD_FAIL);
}

DPRINTF(HGD_D_DEBUG, "Setting up SSL_new");
ssl = SSL_new(ctx);
if (ssl == NULL) {
PRINT_SSL_ERR (HGD_D_ERROR, "SSL_new");
return (HGD_FAIL);
}

ssl_res = SSL_set_fd(ssl, fd);
if (ssl_res == 0) {
PRINT_SSL_ERR (HGD_D_ERROR, "SSL_set_fd");
return (HGD_FAIL);
}

ssl_res = SSL_connect(ssl);
if (ssl_res != 1) {
PRINT_SSL_ERR (HGD_D_ERROR, "SSL_connect");
return (HGD_FAIL);
}

cert = SSL_get_peer_certificate(ssl);
if (!cert) {
DPRINTF(HGD_D_ERROR, "could not get remote cert");
exit (HGD_FAIL);
}

/*
* unfinished work on checking SSL certs. Need to work out how to get the
* hash from the cert to know where to write the cert to. XXX
*/
#if 0
if(SSL_get_verify_result(ssl) != X509_V_OK)
{
PRINT_SSL_ERR ("SSL_connect");

cert = SSL_get_peer_certificate(ssl);

cert->
/* PEM_write_x509(fp!,cert);- */

return (-1);
}
#endif
ok_str = hgd_sock_recv_line(fd, ssl);
hgd_check_svr_response(ok_str, 1);
free(ok_str);

DPRINTF(HGD_D_INFO, "SSL connection established");

return (HGD_OK);
}

int
hgd_print_pretty_server_response(char *resp_line)
{
char *p;
struct hgd_resp_err *resp, *chosen = NULL;

p = strchr(resp_line, '|');
if (p == NULL) {
DPRINTF(HGD_D_ERROR, "Unspecified server error reponse");
return (HGD_FAIL);
}

p++;
for (resp = hgd_resp_errs; resp->code != 0; resp++) {
if (strcmp(p, resp->code) == 0) {
chosen = resp;
break;
}
}

if (chosen == NULL) {
DPRINTF(HGD_D_ERROR, "Unknown server error reponse");
return (HGD_FAIL);
}

DPRINTF(HGD_D_ERROR,
"Server reponded with error '%s': %s", p, chosen->meaning);

return (HGD_OK);
}

/*
* check protocol version is correct
*/
int
hgd_check_svr_proto()
{
char *v, *resp = NULL;
int major = -1, minor = -1, ret = HGD_OK;
char *split = "|";
char *saveptr1;

hgd_sock_send_line(sock_fd, ssl, "proto");
resp = hgd_sock_recv_line(sock_fd, ssl);

if (hgd_check_svr_response(resp, 0) != HGD_OK) {
DPRINTF(HGD_D_ERROR, "Could not check server proto version");
ret = HGD_FAIL;
goto clean;
}

v = strtok_r(resp, split, &saveptr1);
(void) v;

/* major */
v = strtok_r(NULL, split, &saveptr1);
if (v == NULL) {
DPRINTF(HGD_D_ERROR, "Could not find protocol MAJOR version");
ret = HGD_FAIL;
goto clean;
}

major = atoi(v);

/* minor */
v = strtok_r(NULL, split, &saveptr1);
if (v == NULL) {
DPRINTF(HGD_D_ERROR, "Could not find protocol MINOR version");
ret = HGD_FAIL;
goto clean;
}

minor = atoi(v);

if (major == HGD_PROTO_VERSION_MAJOR && minor >= HGD_PROTO_VERSION_MINOR) {
if (minor > HGD_PROTO_VERSION_MINOR) {
DPRINTF(HGD_D_INFO, "Server is running a newer minor version"
"of the server.Server=%d,%d, Client=%d,%d", major, minor,
HGD_PROTO_VERSION_MAJOR, HGD_PROTO_VERSION_MINOR);
}
} else {
DPRINTF(HGD_D_ERROR, "Protocol mismatch: "
"Server=%d,%d, Client=%d,%d", major, minor,
HGD_PROTO_VERSION_MAJOR, HGD_PROTO_VERSION_MINOR);
ret = HGD_FAIL;
goto clean;
}


DPRINTF(HGD_D_DEBUG, "Protocol version matches server");

clean:
if (resp)
free(resp);

return (ret);
}

int
hgd_cli_populate_track(struct hgd_playlist_item **it, char *resp)
{
char *tokens[HGD_NUM_TRACK_FIELDS];
int n_toks = 0;

do {
tokens[n_toks] = xstrdup(strsep(&resp, "|"));
} while ((n_toks++ < HGD_NUM_TRACK_FIELDS) && (resp != NULL));

if (n_toks != HGD_NUM_TRACK_FIELDS) {
DPRINTF(HGD_D_ERROR, "Wrong number of tokens from server");
return (HGD_OK);
}

/* OK, response looked good, now populate struct */
*it = xmalloc(sizeof(struct hgd_playlist_item));

(*it)->id = atoi(tokens[0]);
(*it)->filename = tokens[1];
(*it)->user = tokens[4];
(*it)->playing = 0; /* unused here */
(*it)->finished = 0; /* unused here */
(*it)->tags.artist = tokens[2];
(*it)->tags.title = tokens[3];
(*it)->tags.album = tokens[5];
(*it)->tags.genre = tokens[6];
(*it)->tags.duration = atoi(tokens[7]);
(*it)->tags.bitrate = atoi(tokens[8]);
(*it)->tags.samplerate = atoi(tokens[9]);
(*it)->tags.channels = atoi(tokens[10]);
(*it)->tags.year = atoi(tokens[11]);
(*it)->votes_needed = atoi(tokens[12]);
(*it)->has_voted = atoi(tokens[13]);

/* NOTE: votesneeded and voted? are not stored in this struct */

return (HGD_OK);
}

int
hgd_cli_get_playlist(struct hgd_playlist **list)
{
char *p, *resp, *track_resp;
int n_items = 0, i;

hgd_sock_send_line(sock_fd, ssl, "ls");
resp = hgd_sock_recv_line(sock_fd, ssl);
if (hgd_check_svr_response(resp, 0) == HGD_FAIL) {
free(resp);
return (HGD_FAIL);
}


/* find the first pipe */
for (p = resp; (*p != 0 && *p != '|'); p ++);
if (*p != '|') {
DPRINTF(HGD_D_ERROR, "didn't find a argument separator");
free(resp);
return (HGD_FAIL);
}

/* allocate playlist*/
/* XXX dont allocate here */
*list = xcalloc(1, sizeof(struct hgd_playlist));
(*list)->n_items = atoi(++p);
(*list)->items = xcalloc(
(*list)->n_items, sizeof(struct hgd_playlist_item *));

free(resp);

/* populate items */
DPRINTF(HGD_D_DEBUG, "expecting %d items in playlist", n_items);
for (i = 0; i < (*list)->n_items; i++) {
track_resp = hgd_sock_recv_line(sock_fd, ssl);

if (hgd_cli_populate_track(
&(*list)->items[i], track_resp) != HGD_OK) {
free(track_resp);
return (HGD_FAIL);
}

free(track_resp);
}

return (HGD_OK);
}


/*
* queue a track, after each HGD_Q_CALLBACK_INTVL chunks are sent,
* the callback is fired and client has the ability to update it's ui.
*/
#define HGD_Q_CALLBACK_INTVL 500
int
hgd_cli_queue_track(char *filename,
    void *arg, int(*cb)(void *arg, float progress))
{
FILE *f;
struct stat st;
ssize_t written = 0, fsize, chunk_sz;
char chunk[HGD_BINARY_CHUNK];
char *q_req = 0, *resp1 = 0, *resp2 = 0;
int iters = 0, ret = HGD_FAIL;


DPRINTF(HGD_D_INFO, "Uploading file '%s'", filename);

if (stat(filename, &st) < 0) {
DPRINTF(HGD_D_ERROR, "Can't stat '%s'", filename);
ret = HGD_FAIL;
goto clean;
}

if (st.st_mode & S_IFDIR) {
DPRINTF(HGD_D_ERROR, "Can't upload directories");
ret = HGD_FAIL;
goto clean;
}

fsize = st.st_size;

/* send request to upload */
xasprintf(&q_req, "q|%s|%d", filename, fsize);
hgd_sock_send_line(sock_fd, ssl, q_req);

/* check we are allowed */
resp1 = hgd_sock_recv_line(sock_fd, ssl);
if (hgd_check_svr_response(resp1, 0) == HGD_FAIL)
goto clean;

DPRINTF(HGD_D_DEBUG, "opening '%s' for reading", filename);
f = fopen(filename, "r");
if (f == NULL) {
DPRINTF(HGD_D_ERROR, "fopen %s: %s", filename, SERROR);
ret = HGD_FAIL;
goto clean;
}

/*
* start sending the file
*/
written = 0;
while (written != fsize) {

if (iters % HGD_Q_CALLBACK_INTVL == 0)
cb(arg, ((float) written/fsize));
iters++;

if (fsize - written < HGD_BINARY_CHUNK)
chunk_sz = fsize - written;
else
chunk_sz = HGD_BINARY_CHUNK;

if (fread(chunk, chunk_sz, 1, f) != 1) {
DPRINTF(HGD_D_WARN, "Retrying fread");
continue;
}

hgd_sock_send_bin(sock_fd, ssl, chunk, chunk_sz);

written += chunk_sz;
DPRINTF(HGD_D_DEBUG, "Progress %d/%d bytes",
(int) written, (int) fsize);
}

fclose(f);

resp2 = hgd_sock_recv_line(sock_fd, ssl);
if (hgd_check_svr_response(resp2, 0) == HGD_FAIL) {
ret = HGD_FAIL;
goto clean;
}

DPRINTF(HGD_D_INFO, "Transfer complete");

ret = HGD_OK;
clean:
if (resp1)
free(resp1);
if (resp2)
free(resp2);
if (q_req)
free(q_req);

return (ret);
}
Something went wrong with that request. Please try again.