示例#1
0
static int notify_email_init (void)
{
  char server[MAXSTRING];

  auth_client_init();
  if (!(session = smtp_create_session ())) {
    ERROR ("notify_email plugin: cannot create SMTP session");
    return (-1);
  }

  smtp_set_monitorcb (session, monitor_cb, NULL, 1);
  smtp_set_hostname (session, hostname_g);
  ssnprintf(server, sizeof (server), "%s:%i",
      (smtp_host == NULL) ? DEFAULT_SMTP_HOST : smtp_host,
      smtp_port);
  smtp_set_server (session, server);

  if (smtp_user && smtp_password) {
    authctx = auth_create_context ();
    auth_set_mechanism_flags (authctx, AUTH_PLUGIN_PLAIN, 0);
    auth_set_interact_cb (authctx, authinteract, NULL);
  }

  if ( !smtp_auth_set_context (session, authctx)) {
    ERROR ("notify_email plugin: cannot set SMTP auth context");
    return (-1);   
  }

  return (0);
} /* int notify_email_init */
示例#2
0
/*
 * Builds the message and tries to send it
 * via the specified SMTP server. Returns 0
 * if everything went ok, -1 if someting goes
 * south and sets `global_smtp_error_msg'.
 * If AUTH is required, a dialog will pop up
 * asking the user to authenticate.
 */
int
send_pr(PROBLEM_REPORT *mypr)
{
  smtp_session_t session;
  smtp_message_t message;
  smtp_recipient_t recipient;
  const smtp_status_t *status;
  auth_context_t authctx;
  int noauth = 0;
  char *host = NULL;
  int i = 0;

  enum notify_flags notify = Notify_SUCCESS|Notify_FAILURE;
  FILE *fp;
  char tempfile[FILENAME_MAX + 1];
  char *tmpdir;
  int  tempfd;

  char my_smtp_server[1024];
  char my_recipient[1024];
  char buf[128];

  if (gsp_auth_done != TRUE) {

    my_auth = malloc(sizeof(GSP_AUTH));
    my_auth->username = malloc(1024);
    memset(my_auth->username, 0, 1024);
    my_auth->password = malloc(1024);
    memset(my_auth->password, 0, 1024);

  }

  tmpdir = getenv("TMPDIR");

  memset(tempfile, 0, sizeof(tempfile));

  if (tmpdir != NULL) {

    snprintf(tempfile, FILENAME_MAX, "%s/gtk-send-pr.XXXXXXXX", tmpdir);

  } else {

    snprintf(tempfile, FILENAME_MAX , "/tmp/gtk-send-pr.XXXXXXXX");

  }

  tempfd = mkstemp(tempfile);
  fp = fdopen(tempfd, "w");

  build_message(fp, mypr);

  fclose(fp);

  auth_client_init();
  session = smtp_create_session();
  message = smtp_add_message(session);

  switch (mypr->ssl_mode) {

  case GSP_SSL_NO :

    i = smtp_starttls_enable(session, Starttls_DISABLED);
    break;

  case GSP_SSL_EN :

    i = smtp_starttls_enable(session, Starttls_ENABLED);
    break;

  case GSP_SSL_RE :

    i = smtp_starttls_enable(session, Starttls_REQUIRED);
    break;

  }

  snprintf(my_smtp_server, 1024, "%s:%s", mypr->smtp_server, mypr->smtp_port);

  host = my_smtp_server;

  smtp_set_server(session, host);

  authctx = auth_create_context();
  auth_set_mechanism_flags(authctx, AUTH_PLUGIN_PLAIN, 0);
  auth_set_interact_cb(authctx, authinteract, NULL);

  smtp_set_eventcb(session, event_cb, NULL);

  if (!noauth) {

    smtp_auth_set_context(session, authctx);

  }

  smtp_set_header(message, "From", mypr->originator, mypr->smtp_from);
  smtp_set_header(message, "To", mypr->smtp_rcpt, mypr->smtp_to);
  smtp_set_header(message, "Subject", mypr->smtp_subject);
  smtp_set_header(message, "Message-Id", NULL);
  smtp_set_reverse_path(message, mypr->smtp_from);
  
  fp = fopen(tempfile, "r");

  smtp_set_message_fp(message, fp);

  /* Recipient must be in RFC2821 format */
  snprintf(my_recipient, 1024, "%s", mypr->smtp_to);
  recipient = smtp_add_recipient(message, my_recipient);
  smtp_dsn_set_notify(recipient, notify);

  if (mypr->smtp_cc_num > 0) {

    for (i = 0; i < mypr->smtp_cc_num; i++) {

      smtp_set_header(message, "CC", NULL, mypr->smtp_cc[i]);
      snprintf(my_recipient, 1024, "%s", mypr->smtp_cc[i]);
      recipient = smtp_add_recipient(message, my_recipient);
      smtp_dsn_set_notify(recipient, notify);			

    }

  }

  if (!smtp_start_session(session)) {

    snprintf(global_smtp_error_msg, 1024, "SMTP server problem : %s\n",
	     smtp_strerror(smtp_errno(), buf, sizeof buf));

    status = smtp_message_transfer_status(message);
    smtp_destroy_session(session);
    auth_destroy_context(authctx);
    fclose(fp);
    unlink(tempfile);
    auth_client_exit();

    return(-1);

  } else {

    status = smtp_message_transfer_status(message);

    if (status != NULL) {

      /* Something went wrong... */
      if (status->code >= 400) {

	snprintf(global_smtp_error_msg, 1024, "SMTP server problem : %i %s\n",
		 status->code, status->text);

	smtp_destroy_session(session);
	auth_destroy_context(authctx);
	fclose(fp);
	unlink(tempfile);
	auth_client_exit();

	return(-1);

      }

    }

    smtp_destroy_session(session);
    auth_destroy_context(authctx);
    fclose(fp);
    unlink(tempfile);
    auth_client_exit();

    return(0);

  }

}