| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #include "connection.hh" |
|---|
| 21 | |
|---|
| 22 | #include "../db/db.hh" |
|---|
| 23 | #include "../db/user.hh" |
|---|
| 24 | #include "../logger/logline.hh" |
|---|
| 25 | |
|---|
| 26 | namespace nntp { |
|---|
| 27 | |
|---|
| 28 | class authinfo : public connection::command |
|---|
| 29 | { |
|---|
| 30 | public: |
|---|
| 31 | authinfo(std::string s) { |
|---|
| 32 | connection::register_command(s, this); |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | connection::continuation::ptr |
|---|
| 36 | perform(connection::cb, const std::string [], size_t) const; |
|---|
| 37 | |
|---|
| 38 | void write_capability_line(connection::cb c) const { |
|---|
| 39 | if (c.authenticated()) return; |
|---|
| 40 | if (c.tls_active()) |
|---|
| 41 | { |
|---|
| 42 | c.send_line("AUTHINFO USER"); |
|---|
| 43 | } |
|---|
| 44 | else |
|---|
| 45 | { |
|---|
| 46 | c.send_line("AUTHINFO"); |
|---|
| 47 | } |
|---|
| 48 | } |
|---|
| 49 | }; |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | connection::continuation::ptr |
|---|
| 53 | authinfo::perform(connection::cb c, |
|---|
| 54 | const std::string args[], |
|---|
| 55 | size_t nargs) |
|---|
| 56 | const |
|---|
| 57 | { |
|---|
| 58 | if (c.authenticated()) |
|---|
| 59 | { |
|---|
| 60 | c.send_line("502 multiple authentication " |
|---|
| 61 | "not permitted"); |
|---|
| 62 | return c.dispatch(); |
|---|
| 63 | } |
|---|
| 64 | std::string kw = args[0]; |
|---|
| 65 | boost::to_upper(kw); |
|---|
| 66 | if (kw == "USER") |
|---|
| 67 | { |
|---|
| 68 | if (nargs == 1) |
|---|
| 69 | { |
|---|
| 70 | c.send_line("501 Username missing"); |
|---|
| 71 | return c.dispatch(); |
|---|
| 72 | } |
|---|
| 73 | if (nargs > 2) |
|---|
| 74 | { |
|---|
| 75 | c.send_line("503 spaces in username are " |
|---|
| 76 | "not supported"); |
|---|
| 77 | return c.dispatch(); |
|---|
| 78 | } |
|---|
| 79 | c.identity() = c.dbase().lookup_user(args[1]); |
|---|
| 80 | if (!c.tls_active() && |
|---|
| 81 | (!c.identity() || |
|---|
| 82 | !c.identity()->allow_cleartext_password())) |
|---|
| 83 | { |
|---|
| 84 | c.send_line("483 STARTTLS required"); |
|---|
| 85 | return c.dispatch(); |
|---|
| 86 | } |
|---|
| 87 | if (!c.identity()) |
|---|
| 88 | { |
|---|
| 89 | logger::logline ll; |
|---|
| 90 | ll << c.loghead() |
|---|
| 91 | << "AUTHINFO USER with unknown user '" |
|---|
| 92 | << args[1] |
|---|
| 93 | << "'"; |
|---|
| 94 | ll.close(); |
|---|
| 95 | } |
|---|
| 96 | c.identified() = true; |
|---|
| 97 | c.send_line("381 AUTHINFO PASS required"); |
|---|
| 98 | return c.dispatch(); |
|---|
| 99 | } |
|---|
| 100 | else if (kw == "PASS") |
|---|
| 101 | { |
|---|
| 102 | if (nargs == 1) |
|---|
| 103 | { |
|---|
| 104 | c.send_line("501 password missing"); |
|---|
| 105 | return c.dispatch(); |
|---|
| 106 | } |
|---|
| 107 | if (!c.identified()) |
|---|
| 108 | { |
|---|
| 109 | c.send_line("482 AUTHINFO USER required"); |
|---|
| 110 | return c.dispatch(); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | if (c.identity() && |
|---|
| 118 | nargs == 2 && |
|---|
| 119 | c.identity()->verify_password(args[1])) |
|---|
| 120 | { |
|---|
| 121 | c.authenticated() = true; |
|---|
| 122 | c.send_line("281 cool"); |
|---|
| 123 | return c.dispatch(); |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | if (c.identity()) |
|---|
| 127 | { |
|---|
| 128 | logger::logline ll; |
|---|
| 129 | ll << c.loghead() |
|---|
| 130 | << "AUTHINFO PASS failed for user '" |
|---|
| 131 | << c.identity()->get_userid() |
|---|
| 132 | << "'"; |
|---|
| 133 | ll.close(); |
|---|
| 134 | } |
|---|
| 135 | return c.delay_fail("481 password incorrect"); |
|---|
| 136 | } |
|---|
| 137 | else if (kw == "SASL" || |
|---|
| 138 | kw == "SIMPLE" || |
|---|
| 139 | kw == "GENERIC") |
|---|
| 140 | { |
|---|
| 141 | logger::logline ll; |
|---|
| 142 | ll << c.loghead() |
|---|
| 143 | << "AUTHINFO " |
|---|
| 144 | << args[0] |
|---|
| 145 | << " attempted (not supported)"; |
|---|
| 146 | ll.close(); |
|---|
| 147 | c.send_line("503 AUTHINFO " + args[0] + |
|---|
| 148 | " not supported"); |
|---|
| 149 | return c.dispatch(); |
|---|
| 150 | } |
|---|
| 151 | else |
|---|
| 152 | { |
|---|
| 153 | c.send_line("501 syntax error"); |
|---|
| 154 | return c.dispatch(); |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | }; |
|---|
| 158 | namespace { |
|---|
| 159 | nntp::authinfo authinfo("authinfo"); |
|---|
| 160 | } |
|---|