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
|
/*
* This file is part of libESMTP, a library for submission of RFC 2822
* formatted electronic mail messages using the SMTP protocol described
* in RFC 2821.
*
* Copyright (C) 2001,2002 Brian Stafford <brian@stafford.uklinux.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <missing.h> /* declarations for missing library functions */
#include <errno.h>
#include "api.h"
#include "libesmtp-private.h"
#include "headers.h"
/* This file contains the SMTP client library's external API. For the
most part, it just sanity checks function arguments and either carries
out the simple stuff directly, or passes complicated stuff into the
bowels of the library and RFC hell.
*/
smtp_session_t
smtp_create_session (void)
{
smtp_session_t session;
if ((session = malloc (sizeof (struct smtp_session))) == NULL)
{
set_errno (ENOMEM);
return 0;
}
memset (session, 0, sizeof (struct smtp_session));
return session;
}
int
smtp_set_server (smtp_session_t session, const char *hostport)
{
char *host, *service;
SMTPAPI_CHECK_ARGS (session != NULL && hostport != NULL, 0);
if ((host = strdup (hostport)) == NULL)
{
set_errno (ENOMEM);
return 0;
}
if ((service = strchr (host, ':')) != NULL)
*service++ = '\0';
if (service == NULL)
session->port = "587";
else
session->port = service;
session->host = host;
return 1;
}
int
smtp_set_hostname (smtp_session_t session, const char *hostname)
{
#ifdef HAVE_GETHOSTNAME
SMTPAPI_CHECK_ARGS (session != NULL, 0);
#else
SMTPAPI_CHECK_ARGS (session != NULL && hostname != NULL, 0);
#endif
if (session->localhost != NULL)
free (session->localhost);
#ifdef HAVE_GETHOSTNAME
if (hostname == NULL)
{
session->localhost = NULL;
return 1;
}
#endif
session->localhost = strdup (hostname);
if (session->localhost == NULL)
{
set_errno (ENOMEM);
return 0;
}
return 1;
}
smtp_message_t
smtp_add_message (smtp_session_t session)
{
smtp_message_t message;
SMTPAPI_CHECK_ARGS (session != NULL, NULL);
if ((message = malloc (sizeof (struct smtp_message))) == NULL)
{
set_errno (ENOMEM);
return 0;
}
memset (message, 0, sizeof (struct smtp_message));
message->session = session;
APPEND_LIST (session->messages, session->end_messages, message);
return message;
}
int
smtp_enumerate_messages (smtp_session_t session,
smtp_enumerate_messagecb_t cb, void *arg)
{
smtp_message_t message;
SMTPAPI_CHECK_ARGS (session != NULL && cb != NULL, 0);
for (message = session->messages; message != NULL; message = message->next)
(*cb) (message, arg);
return 1;
}
const smtp_status_t *
smtp_message_transfer_status (smtp_message_t message)
{
SMTPAPI_CHECK_ARGS (message != NULL, NULL);
return &message->message_status;
}
int
smtp_set_reverse_path (smtp_message_t message, const char *mailbox)
{
SMTPAPI_CHECK_ARGS (message != NULL, 0);
if (message->reverse_path_mailbox != NULL)
free (message->reverse_path_mailbox);
if (mailbox == NULL)
message->reverse_path_mailbox = NULL;
else if ((message->reverse_path_mailbox = strdup (mailbox)) == NULL)
{
set_errno (ENOMEM);
return 0;
}
return 1;
}
const smtp_status_t *
smtp_reverse_path_status (smtp_message_t message)
{
SMTPAPI_CHECK_ARGS (message != NULL, NULL);
return &message->reverse_path_status;
}
int
smtp_message_reset_status (smtp_message_t message)
{
SMTPAPI_CHECK_ARGS (message != NULL, 0);
reset_status (&message->reverse_path_status);
reset_status (&message->message_status);
return 1;
}
smtp_recipient_t
smtp_add_recipient (smtp_message_t message, const char *mailbox)
{
smtp_recipient_t recipient;
SMTPAPI_CHECK_ARGS (message != NULL && mailbox != NULL, NULL);
if ((recipient = malloc (sizeof (struct smtp_recipient))) == NULL)
{
set_errno (ENOMEM);
return 0;
}
memset (recipient, 0, sizeof (struct smtp_recipient));
recipient->message = message;
recipient->mailbox = strdup (mailbox);
if (recipient->mailbox == NULL)
{
free (recipient);
set_errno (ENOMEM);
return 0;
}
APPEND_LIST (message->recipients, message->end_recipients, recipient);
return recipient;
}
int
smtp_enumerate_recipients (smtp_message_t message,
smtp_enumerate_recipientcb_t cb, void *arg)
{
smtp_recipient_t recipient;
SMTPAPI_CHECK_ARGS (message != NULL, 0);
for (recipient = message->recipients;
recipient != NULL;
recipient = recipient->next)
(*cb) (recipient, recipient->mailbox, arg);
return 1;
}
const smtp_status_t *
smtp_recipient_status (smtp_recipient_t recipient)
{
SMTPAPI_CHECK_ARGS (recipient != NULL, NULL);
return &recipient->status;
}
int
smtp_recipient_check_complete (smtp_recipient_t recipient)
{
SMTPAPI_CHECK_ARGS (recipient != NULL, 0);
return recipient->complete;
}
int
smtp_recipient_reset_status (smtp_recipient_t recipient)
{
SMTPAPI_CHECK_ARGS (recipient != NULL, 0);
reset_status (&recipient->status);
recipient->complete = 0;
return 1;
}
/* DSN (RFC 1891) */
int
smtp_dsn_set_ret (smtp_message_t message, enum ret_flags flags)
{
SMTPAPI_CHECK_ARGS (message != NULL, 0);
message->dsn_ret = flags;
if (flags != Ret_NOTSET)
message->session->required_extensions |= EXT_DSN;
return 1;
}
int
smtp_dsn_set_envid (smtp_message_t message, const char *envid)
{
SMTPAPI_CHECK_ARGS (message != NULL, 0);
message->dsn_envid = strdup (envid);
if (message->dsn_envid == NULL)
{
set_errno (ENOMEM);
return 0;
}
message->session->required_extensions |= EXT_DSN;
return 1;
}
int
smtp_dsn_set_notify (smtp_recipient_t recipient, enum notify_flags flags)
{
SMTPAPI_CHECK_ARGS (recipient != NULL, 0);
recipient->dsn_notify = flags;
if (flags != Notify_NOTSET)
recipient->message->session->required_extensions |= EXT_DSN;
return 1;
}
int
smtp_dsn_set_orcpt (smtp_recipient_t recipient,
const char *address_type, const char *address)
{
SMTPAPI_CHECK_ARGS (recipient != NULL, 0);
recipient->dsn_addrtype = strdup (address_type);
if (recipient->dsn_addrtype == NULL)
{
set_errno (ENOMEM);
return 0;
}
recipient->dsn_orcpt = strdup (address);
if (recipient->dsn_orcpt == NULL)
{
free (recipient->dsn_addrtype);
set_errno (ENOMEM);
return 0;
}
recipient->message->session->required_extensions |= EXT_DSN;
return 1;
}
/* SIZE (RFC 1870) */
int
smtp_size_set_estimate (smtp_message_t message, unsigned long size)
{
SMTPAPI_CHECK_ARGS (message != NULL, 0);
message->size_estimate = size;
return 1;
}
/* 8BITMIME (RFC 1652) */
int
smtp_8bitmime_set_body (smtp_message_t message, enum e8bitmime_body body)
{
SMTPAPI_CHECK_ARGS (message != NULL, 0);
message->e8bitmime = body;
if (body != E8bitmime_NOTSET)
message->session->required_extensions |= EXT_8BITMIME;
return 1;
}
/* DELIVERBY (RFC 2852) */
int
smtp_deliverby_set_mode (smtp_message_t message,
long time, enum by_mode mode, int trace)
{
SMTPAPI_CHECK_ARGS (message != NULL, 0);
SMTPAPI_CHECK_ARGS ((-999999999 <= time && time <= +999999999), 0);
SMTPAPI_CHECK_ARGS (!(mode == By_RETURN && time <= 0), 0);
message->by_time = time;
message->by_mode = mode;
message->by_trace = !!trace;
return 1;
}
int
smtp_set_messagecb (smtp_message_t message, smtp_messagecb_t cb, void *arg)
{
SMTPAPI_CHECK_ARGS (message != NULL && cb != NULL, 0);
message->cb = cb;
message->cb_arg = arg;
return 1;
}
int
smtp_set_eventcb (smtp_session_t session, smtp_eventcb_t cb, void *arg)
{
SMTPAPI_CHECK_ARGS (session != NULL, 0);
session->event_cb = cb;
session->event_cb_arg = arg;
return 1;
}
int
smtp_set_monitorcb (smtp_session_t session, smtp_monitorcb_t cb, void *arg,
int headers)
{
SMTPAPI_CHECK_ARGS (session != NULL, 0);
session->monitor_cb = cb;
session->monitor_cb_arg = arg;
session->monitor_cb_headers = headers;
return 1;
}
int
smtp_start_session (smtp_session_t session)
{
smtp_message_t message;
SMTPAPI_CHECK_ARGS (session != NULL && session->host != NULL, 0);
#ifndef HAVE_GETHOSTNAME
SMTPAPI_CHECK_ARGS (session->localhost != NULL, 0);
#endif
/* Check that every message has a callback set */
for (message = session->messages; message != NULL; message = message->next)
if (message->cb == NULL)
{
set_error (SMTP_ERR_INVAL);
return 0;
}
return do_session (session);
}
int
smtp_destroy_session (smtp_session_t session)
{
smtp_message_t message;
smtp_recipient_t recipient;
SMTPAPI_CHECK_ARGS (session != NULL, 0);
reset_status (&session->mta_status);
destroy_auth_mechanisms (session);
#ifdef USE_ETRN
destroy_etrn_nodes (session);
#endif
if (session->host != NULL)
free (session->host);
if (session->localhost != NULL)
free (session->localhost);
if (session->msg_source != NULL)
msg_source_destroy (session->msg_source);
while (session->messages != NULL)
{
message = session->messages->next;
reset_status (&session->messages->reverse_path_status);
free (session->messages->reverse_path_mailbox);
while (session->messages->recipients != NULL)
{
recipient = session->messages->recipients->next;
reset_status (&session->messages->recipients->status);
free (session->messages->recipients->mailbox);
if (session->messages->recipients->dsn_addrtype != NULL)
free (session->messages->recipients->dsn_addrtype);
if (session->messages->recipients->dsn_orcpt != NULL)
free (session->messages->recipients->dsn_orcpt);
free (session->messages->recipients);
session->messages->recipients = recipient;
}
destroy_header_table (session->messages);
if (session->messages->dsn_envid != NULL)
free (session->messages->dsn_envid);
free (session->messages);
session->messages = message;
}
free (session);
return 1;
}
void *
smtp_set_application_data (smtp_session_t session, void *data)
{
void *old;
SMTPAPI_CHECK_ARGS (session != NULL, 0);
old = session->application_data;
session->application_data = data;
return old;
}
void *
smtp_get_application_data (smtp_session_t session)
{
SMTPAPI_CHECK_ARGS (session != NULL, 0);
return session->application_data;
}
void *
smtp_message_set_application_data (smtp_message_t message, void *data)
{
void *old;
SMTPAPI_CHECK_ARGS (message != NULL, 0);
old = message->application_data;
message->application_data = data;
return old;
}
void *
smtp_message_get_application_data (smtp_message_t message)
{
SMTPAPI_CHECK_ARGS (message != NULL, 0);
return message->application_data;
}
void *
smtp_recipient_set_application_data (smtp_recipient_t recipient, void *data)
{
void *old;
SMTPAPI_CHECK_ARGS (recipient != NULL, 0);
old = recipient->application_data;
recipient->application_data = data;
return old;
}
void *
smtp_recipient_get_application_data (smtp_recipient_t recipient)
{
SMTPAPI_CHECK_ARGS (recipient != NULL, 0);
return recipient->application_data;
}
int
smtp_version (void *buf, size_t len, int what)
{
static const char version[] = VERSION;
SMTPAPI_CHECK_ARGS (buf != NULL && len > 0, 0);
SMTPAPI_CHECK_ARGS (what == 0, 0);
if (len < sizeof version)
{
set_error (SMTP_ERR_INVAL);
return 0;
}
memcpy (buf, version, sizeof version);
return 1;
}
#ifdef USE_REQUIRE_ALL_RECIPIENTS
/* Deprecated: do not use */
/* Some applications can't handle one recipient from many failing
particularly well. If the 'require_all_recipients' option is
set, this will fail the entire transaction even if some of the
recipients were accepted in the RCPT commands. */
int
smtp_option_require_all_recipients (smtp_session_t session, int state)
{
SMTPAPI_CHECK_ARGS (session != NULL, 0);
session->require_all_recipients = !!state;
return 1;
}
#endif
|