X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/blobdiff_plain/b42ffad6922d87580e3edf528870f079f21dfd07..ac152d06f4cfa9abcd4a88de7dbc6a9040f99c0d:/server/dcgi.c diff --git a/server/dcgi.c b/server/dcgi.c index f14d8bf..27c77b1 100644 --- a/server/dcgi.c +++ b/server/dcgi.c @@ -39,7 +39,6 @@ #include "vector.h" #include "sink.h" #include "cgi.h" -#include "dcgi.h" #include "log.h" #include "configuration.h" #include "table.h" @@ -54,6 +53,10 @@ #include "defs.h" #include "trackname.h" #include "charset.h" +#include "dcgi.h" +#include "url.h" +#include "mime.h" +#include "sendmail.h" char *login_cookie; @@ -101,34 +104,45 @@ static const char *front_url(void) { return config->url; } +static void header_cookie(struct sink *output) { + struct dynstr d[1]; + struct url u; + + memset(&u, 0, sizeof u); + dynstr_init(d); + parse_url(config->url, &u); + if(login_cookie) { + dynstr_append_string(d, "disorder="); + dynstr_append_string(d, quote822(login_cookie, 0)); + } else { + /* Force browser to discard cookie */ + dynstr_append_string(d, "disorder=none;Max-Age=0"); + } + if(u.path) { + /* The default domain matches the request host, so we need not override + * that. But the default path only goes up to the rightmost /, which would + * cause the browser to expose the cookie to other CGI programs on the same + * web server. */ + dynstr_append_string(d, ";Path="); + dynstr_append_string(d, quote822(u.path, 0)); + } + dynstr_terminate(d); + cgi_header(output, "Set-Cookie", d->vec); +} + static void redirect(struct sink *output) { const char *back; back = cgi_get("back"); cgi_header(output, "Location", back && *back ? back : front_url()); + header_cookie(output); cgi_body(output); } -static void header_cookie(cgi_sink *output) { - struct dynstr d[1]; - char *s; - - if(login_cookie) { - dynstr_init(d); - for(s = login_cookie; *s; ++s) { - if(*s == '"') - dynstr_append(d, '\\'); - dynstr_append(d, *s); - } - dynstr_terminate(d); - byte_xasprintf(&s, "disorder=\"%s\"", d->vec); /* TODO domain, path, expiry */ - cgi_header(output->sink, "Set-Cookie", s); - } -} - static void expand_template(dcgi_state *ds, cgi_sink *output, const char *action) { cgi_header(output->sink, "Content-Type", "text/html"); + header_cookie(output->sink); cgi_body(output->sink); expand(output, action, ds); } @@ -137,6 +151,7 @@ static void lookups(dcgi_state *ds, unsigned want) { unsigned need; struct queue_entry *r, *rnext; const char *dir, *re; + char *rights; if(ds->g->client && (need = want ^ (ds->g->flags & want)) != 0) { if(need & DC_QUEUE) @@ -171,6 +186,12 @@ static void lookups(dcgi_state *ds, unsigned want) { &ds->g->files, &ds->g->nfiles)) ds->g->nfiles = 0; } + if(need & DC_RIGHTS) { + ds->g->rights = RIGHT_READ; /* fail-safe */ + if(!disorder_userinfo(ds->g->client, disorder_user(ds->g->client), + "rights", &rights)) + parse_rights(rights, &ds->g->rights, 1); + } ds->g->flags |= need; } } @@ -271,6 +292,7 @@ static void act_playing(cgi_sink *output, dcgi_state *ds) { byte_snprintf(r, sizeof r, "%ld;url=%s", refresh > 0 ? refresh : 1, front_url()); cgi_header(output->sink, "Refresh", r); + header_cookie(output->sink); cgi_body(output->sink); expand(output, "playing", ds); } @@ -337,9 +359,11 @@ static void act_volume(cgi_sink *output, dcgi_state *ds) { * URL) */ cgi_header(output->sink, "Location", (back = cgi_get("back")) ? back : volume_url()); + header_cookie(output->sink); cgi_body(output->sink); } else { cgi_header(output->sink, "Content-Type", "text/html"); + header_cookie(output->sink); cgi_body(output->sink); expand(output, "volume", ds); } @@ -408,6 +432,7 @@ static void act_prefs(cgi_sink *output, dcgi_state *ds) { for(numfile = 0; numfile < nfiles; ++numfile) process_prefs(ds, numfile); cgi_header(output->sink, "Content-Type", "text/html"); + header_cookie(output->sink); cgi_body(output->sink); expand(output, "prefs", ds); } @@ -451,8 +476,9 @@ static void act_login(cgi_sink *output, return; } /* We have a new cookie */ - header_cookie(output); - if((back = cgi_get("back")) && back) + header_cookie(output->sink); + cgi_set_option("status", "loginok"); + if((back = cgi_get("back")) && *back) /* Redirect back to somewhere or other */ redirect(output->sink); else @@ -464,14 +490,18 @@ static void act_logout(cgi_sink *output, dcgi_state *ds) { disorder_revoke(ds->g->client); login_cookie = 0; + /* Reconnect as guest */ + disorder_cgi_login(ds, output); /* Back to the login page */ + cgi_set_option("status", "logoutok"); expand_template(ds, output, "login"); } static void act_register(cgi_sink *output, dcgi_state *ds) { const char *username, *password, *email; - char *confirm; + char *confirm, *content_type; + const char *text, *encoding, *charset; username = cgi_get("username"); password = cgi_get("password"); @@ -504,8 +534,36 @@ static void act_register(cgi_sink *output, expand_template(ds, output, "login"); return; } + /* Send the user a mail */ + /* TODO templatize this */ + byte_xasprintf((char **)&text, + "Welcome to DisOrder. To active your login, please visit this URL:\n" + "\n" + "%s?c=%s\n", config->url, urlencodestring(confirm)); + if(!(text = mime_encode_text(text, &charset, &encoding))) + fatal(0, "cannot encode email"); + byte_xasprintf(&content_type, "text/plain;charset=%s", + quote822(charset, 0)); + sendmail("", config->mail_sender, email, "Welcome to DisOrder", + encoding, content_type, text); /* TODO error checking */ /* We'll go back to the login page with a suitable message */ - cgi_set_option("registered", "registeredok"); + cgi_set_option("status", "registered"); + expand_template(ds, output, "login"); +} + +static void act_confirm(cgi_sink *output, + dcgi_state *ds) { + const char *confirmation; + + if(!(confirmation = cgi_get("c"))) { + cgi_set_option("error", "noconfirm"); + expand_template(ds, output, "login"); + } + if(disorder_confirm(ds->g->client, confirmation)) { + cgi_set_option("error", "badconfirm"); + expand_template(ds, output, "login"); + } + cgi_set_option("status", "confirmed"); expand_template(ds, output, "login"); } @@ -513,6 +571,7 @@ static const struct action { const char *name; void (*handler)(cgi_sink *output, dcgi_state *ds); } actions[] = { + { "confirm", act_confirm }, { "disable", act_disable }, { "enable", act_enable }, { "login", act_login }, @@ -1222,17 +1281,12 @@ static void exp_scratchable(int attribute((unused)) nargs, cgi_sink *output, void attribute((unused)) *u) { dcgi_state *ds = u; - int result; - - if(config->restrictions & RESTRICT_SCRATCH) { - lookups(ds, DC_PLAYING); - result = (ds->g->playing - && (!ds->g->playing->submitter - || !strcmp(ds->g->playing->submitter, - disorder_user(ds->g->client)))); - } else - result = 1; - sink_printf(output->sink, "%s", bool2str(result)); + + lookups(ds, DC_PLAYING|DC_RIGHTS); + sink_printf(output->sink, "%s", + bool2str(right_scratchable(ds->g->rights, + disorder_user(ds->g->client), + ds->g->playing))); } static void exp_removable(int attribute((unused)) nargs, @@ -1240,16 +1294,25 @@ static void exp_removable(int attribute((unused)) nargs, cgi_sink *output, void attribute((unused)) *u) { dcgi_state *ds = u; - int result; - if(config->restrictions & RESTRICT_REMOVE) - result = (ds->track - && ds->track->submitter - && !strcmp(ds->track->submitter, - disorder_user(ds->g->client))); - else - result = 1; - sink_printf(output->sink, "%s", bool2str(result)); + lookups(ds, DC_RIGHTS); + sink_printf(output->sink, "%s", + bool2str(right_removable(ds->g->rights, + disorder_user(ds->g->client), + ds->track))); +} + +static void exp_movable(int attribute((unused)) nargs, + char attribute((unused)) **args, + cgi_sink *output, + void attribute((unused)) *u) { + dcgi_state *ds = u; + + lookups(ds, DC_RIGHTS); + sink_printf(output->sink, "%s", + bool2str(right_movable(ds->g->rights, + disorder_user(ds->g->client), + ds->track))); } static void exp_navigate(int attribute((unused)) nargs, @@ -1522,6 +1585,25 @@ static void exp_user(int attribute((unused)) nargs, cgi_output(output, "%s", disorder_user(ds->g->client)); } +static void exp_right(int attribute((unused)) nargs, + char **args, + cgi_sink *output, + void *u) { + dcgi_state *const ds = u; + const char *right = expandarg(args[0], ds); + rights_type r; + + lookups(ds, DC_RIGHTS); + if(parse_rights(right, &r, 1/*report*/)) + r = 0; + if(args[1] == 0) + cgi_output(output, "%s", bool2str(!!(r & ds->g->rights))); + else if(r & ds->g->rights) + expandstring(output, args[1], ds); + else if(args[2]) + expandstring(output, args[2], ds); +} + static const struct cgi_expansion expansions[] = { { "#", 0, INT_MAX, EXP_MAGIC, exp_comment }, { "action", 0, 0, 0, exp_action }, @@ -1549,6 +1631,7 @@ static const struct cgi_expansion expansions[] = { { "isrecent", 0, 0, 0, exp_isrecent }, { "label", 1, 1, 0, exp_label }, { "length", 0, 0, 0, exp_length }, + { "movable", 0, 0, 0, exp_movable }, { "navigate", 2, 2, EXP_MAGIC, exp_navigate }, { "ne", 2, 2, 0, exp_ne }, { "new", 1, 1, EXP_MAGIC, exp_new }, @@ -1569,6 +1652,7 @@ static const struct cgi_expansion expansions[] = { { "recent", 1, 1, EXP_MAGIC, exp_recent }, { "removable", 0, 0, 0, exp_removable }, { "resolve", 1, 1, 0, exp_resolve }, + { "right", 1, 3, EXP_MAGIC, exp_right }, { "scratchable", 0, 0, 0, exp_scratchable }, { "search", 2, 3, EXP_MAGIC, exp_search }, { "server-version", 0, 0, 0, exp_server_version }, @@ -1611,8 +1695,6 @@ static void perform_action(cgi_sink *output, dcgi_state *ds, const char *action) { int n; - /* If we have a login cookie it'd better appear in all responses */ - header_cookie(output); /* We don't ever want anything to be cached */ cgi_header(output->sink, "Cache-Control", "no-cache"); if((n = TABLE_FIND(actions, struct action, name, action)) >= 0) @@ -1624,7 +1706,14 @@ static void perform_action(cgi_sink *output, dcgi_state *ds, void disorder_cgi(cgi_sink *output, dcgi_state *ds) { const char *action = cgi_get("action"); - if(!action) action = "playing"; + if(!action) { + /* We allow URLs which are just confirm=... in order to keep confirmation + * URLs, which are user-facing, as short as possible. */ + if(cgi_get("c")) + action = "confirm"; + else + action = "playing"; + } perform_action(output, ds, action); } @@ -1634,6 +1723,22 @@ void disorder_cgi_error(cgi_sink *output, dcgi_state *ds, perform_action(output, ds, "error"); } +/** @brief Log in as the current user or guest if none */ +void disorder_cgi_login(dcgi_state *ds, cgi_sink *output) { + /* Create a new connection */ + ds->g->client = disorder_new(0); + /* Forget everything we knew */ + ds->g->flags = 0; + /* Reconnect */ + if(disorder_connect_cookie(ds->g->client, login_cookie)) { + disorder_cgi_error(output, ds, "connect"); + exit(0); + } + /* If there was a cookie but it went bad, we forget it */ + if(login_cookie && !strcmp(disorder_user(ds->g->client), "guest")) + login_cookie = 0; +} + /* Local Variables: c-basic-offset:2