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
|
/* imapfront-auth.c - IMAP authentication front-end
* Copyright (C) 2005 Bruce Guenter <bruceg@em.ca> or FutureQuest, Inc.
* Development of this program was sponsored by FutureQuest, Inc.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Contact information:
* FutureQuest Inc.
* PO BOX 623127
* Oviedo FL 32762-3127 USA
* http://www.FutureQuest.net/
* ossi@FutureQuest.net
*/
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sysdeps.h>
#include <cvm/sasl.h>
#include <cvm/v2client.h>
#include <iobuf/iobuf.h>
#include <str/str.h>
const char program[] = "imapfront-auth";
const int msg_show_pid = 1;
#define MAX_ARGC 16
#define QUOTE '"'
#define ESCAPE '\\'
#define LBRACE '{'
#define RBRACE '}'
static const char NOTAG[] = "*";
static const char CONT[] = "+";
static const char* capability;
static const char* cvm;
static const char* domain;
static char** nextcmd;
static str tag;
static str line;
static str cmd;
static str line_args[MAX_ARGC];
static int line_argc;
static struct sasl_auth saslauth = { .prefix = "+ " };
void log_start(const char* tagstr)
{
obuf_puts(&errbuf, program);
obuf_putc(&errbuf, '[');
obuf_putu(&errbuf, getpid());
obuf_puts(&errbuf, "]: ");
if (tagstr) {
obuf_puts(&errbuf, tagstr);
obuf_putc(&errbuf, ' ');
}
}
void log_str(const char* msg) { obuf_puts(&errbuf, msg); }
void log_end(void) { obuf_putsflush(&errbuf, "\n"); }
void log(const char* tagstr, const char* msg)
{
log_start(tagstr);
log_str(msg);
log_end();
}
void respond_start(const char* tagstr)
{
if (tagstr == 0) tagstr = tag.s;
log_start(tagstr);
if (!obuf_puts(&outbuf, tagstr) ||
!obuf_putc(&outbuf, ' '))
exit(1);
}
void respond_str(const char* msg)
{
log_str(msg);
if (!obuf_puts(&outbuf, msg))
exit(1);
}
void respond_end(void)
{
log_end();
if (!obuf_putsflush(&outbuf, CRLF))
exit(1);
}
void respond(const char* tagstr, const char* msg)
{
respond_start(tagstr);
respond_str(msg);
respond_end();
}
#if 0
static int isctl(char ch) { return (ch > 0x1f) && (ch < 0x7f); }
static int isquotedspecial(char ch) { return (ch == QUOTE) || (ch == ESCAPE); }
static int isatomspecial(char ch)
{
switch (ch) {
case '(':
case ')':
case '{':
case ' ':
case '%':
case '*':
return 0;
}
return !isctl(ch) && !isquotedspecial(ch);
}
static int isatom(char ch) { return !isatomspecial(ch); }
static int istag(char ch) { return isatom(ch) && (ch != '+'); }
#endif
/* This parser is rather liberal in what it accepts:
The IMAP standard mandates seperate character sets for tags, commands,
unquoted strings. Since they all must be seperated by spaces, this
parser allows all non-whitespace characters for each of the above. */
static int parse_line(void)
{
#define RESPOND(T,S) do{ respond(T,S); return 0; }while(0)
unsigned i;
unsigned len;
const char* ptr;
str* arg;
/* Parse out the command tag */
str_truncate(&tag, 0);
for (i = 0, ptr = line.s; i < line.len && isspace(*ptr); ++i, ++ptr) ;
for (; i < line.len && !isspace(*ptr); ++i, ++ptr)
str_catc(&tag, line.s[i]);
if (!tag.len) RESPOND(NOTAG, "BAD Syntax error");
if (i >= line.len) RESPOND(0, "BAD Syntax error");
/* Parse out the command itself */
str_truncate(&cmd, 0);
for (; i < line.len && isspace(*ptr); ++i, ++ptr) ;
for (; i < line.len && !isspace(*ptr); ++i, ++ptr)
str_catc(&cmd, line.s[i]);
if (!cmd.len) RESPOND(0, "BAD Syntax error");
/* Parse out the command-line args */
for (line_argc = 0; line_argc < MAX_ARGC; ++line_argc) {
arg = &line_args[line_argc];
str_truncate(arg, 0);
for (; i < line.len && isspace(*ptr); ++i, ++ptr) ;
if (i >= line.len) break;
switch (*ptr) {
case LBRACE:
/* Handle a string literal */
++i, ++ptr;
if (!isdigit(*ptr)) RESPOND(0, "BAD Syntax error: missing integer");
for (len = 0; i < line.len && *ptr != RBRACE; ++i, ++ptr) {
if (!isdigit(*ptr))
RESPOND(0, "BAD Syntax error: invalid integer");
len = len * 10 + *ptr - '0';
}
++i, ++ptr;
if (*ptr != 0) RESPOND(0, "BAD Syntax error: missing LF after integer");
str_ready(arg, len);
respond(CONT, "OK");
if (len > 0)
ibuf_read(&inbuf, arg->s, len);
arg->s[arg->len = len] = 0;
ibuf_getstr_crlf(&inbuf, &line);
i = 0;
ptr = line.s;
break;
case QUOTE:
/* Handle a quoted string */
for (++i, ++ptr; i < line.len && *ptr != QUOTE; ++i, ++ptr) {
if (*ptr == ESCAPE) {
if (++i >= line.len) break;
++ptr;
}
str_catc(arg, *ptr);
}
if (i >= line.len || *ptr != QUOTE)
RESPOND(0, "BAD Syntax error: unterminated quoted string");
++i, ++ptr;
break;
default:
/* Normal case is very simple */
for (; i < line.len && !isspace(*ptr); ++i, ++ptr)
str_catc(arg, *ptr);
}
}
for (; i < line.len && isspace(*ptr); ++i, ++ptr) ;
if (i < line.len) RESPOND(0, "BAD Too many command arguments");
return 1;
}
void cmd_noop(void)
{
respond(0, "OK NOOP completed");
}
void cmd_logout(void)
{
respond(NOTAG, "Logging out");
respond(0, "OK LOGOUT completed");
exit(0);
}
void cmd_capability(void)
{
respond_start(NOTAG);
respond_str("CAPABILITY IMAP4rev1 ");
respond_str(capability);
respond_end();
respond(0, "OK CAPABILITY completed");
}
void do_exec(void)
{
if (!cvm_setugid())
respond(0, "NO Internal error: could not set UID/GID");
else if (!cvm_setenv() ||
(cvm_fact_mailbox != 0 &&
setenv("MAILDIR", cvm_fact_mailbox, 1) == -1) ||
setenv("IMAPLOGINTAG", tag.s, 1) == -1 ||
setenv("AUTHENTICATED", cvm_fact_username, 1) == -1)
respond(0, "NO Internal error: could not set environment");
else {
alarm(0);
execvp(nextcmd[0], nextcmd);
respond(0, "NO Could not execute second stage");
}
exit(1);
}
void cmd_login(int argc, str* argv)
{
int cr;
if (argc != 2)
respond(0, "BAD LOGIN command requires exactly two arguments");
else {
if ((cr = cvm_authenticate_password(cvm, argv[0].s, domain,
argv[1].s, 1)) == 0)
do_exec();
else
respond(0, "NO LOGIN failed");
}
}
void cmd_authenticate(int argc, str* argv)
{
int i;
if (argc == 1)
i = sasl_auth2(&saslauth, argv[0].s, 0);
else if (argc == 2)
i = sasl_auth2(&saslauth, argv[0].s, argv[1].s);
else {
respond(0, "BAD AUTHENTICATE command requires only one or two arguments");
return;
}
if (i == 0)
do_exec();
respond_start(0);
respond_str("NO AUTHENTICATE failed: ");
respond_str(sasl_auth_msg(&i));
respond_end();
}
struct command
{
const char* name;
void (*fn0)(void);
void (*fn1)(int argc, str* argv);
};
struct command commands[] = {
{ "CAPABILITY", cmd_capability, 0 },
{ "NOOP", cmd_noop, 0 },
{ "LOGOUT", cmd_logout, 0 },
{ "LOGIN", 0, cmd_login },
{ "AUTHENTICATE", 0, cmd_authenticate },
{ 0, 0, 0 }
};
static void dispatch_line(void)
{
struct command* c;
str_upper(&cmd);
for (c = commands; c->name != 0; ++c) {
if (str_diffs(&cmd, c->name) == 0) {
if (line_argc == 0) {
if (c->fn0 == 0)
respond(0, "BAD Syntax error: command requires arguments");
else
c->fn0();
}
else {
if (c->fn1 == 0)
respond(0, "BAD Syntax error: command requires no arguments");
else
c->fn1(line_argc, line_args);
}
return;
}
}
respond(0, "BAD Unimplemented command");
}
static int startup(int argc, char* argv[])
{
if (argc < 2) {
respond(NOTAG, "NO Usage: imapfront-auth imapd [args ...]");
return 0;
}
domain = getenv("TCPLOCALHOST");
nextcmd = argv + 1;
if ((cvm = getenv("CVM_SASL_PLAIN")) == 0) {
respond(NOTAG, "NO $CVM_SASL_PLAIN is not set");
return 0;
}
if (!sasl_auth_init(&saslauth)) {
respond(NOTAG, "NO Could not initialize SASL AUTH");
return 0;
}
if ((capability = getenv("IMAP_CAPABILITY")) == 0 &&
(capability = getenv("CAPABILITY")) == 0)
capability = "";
if (strncasecmp(capability, "IMAP4rev1", 9) == 0) capability += 9;
while (isspace(*capability)) ++capability;
return 1;
}
const int authenticating = 1;
extern void set_timeout(void);
int main(int argc, char* argv[])
{
set_timeout();
if (!startup(argc, argv)) return 0;
respond(NOTAG, "OK imapfront ready.");
while (ibuf_getstr_crlf(&inbuf, &line)) {
if (parse_line())
dispatch_line();
}
if (ibuf_timedout(&inbuf))
respond(NOTAG, "NO Connection timed out");
return 0;
}
|