2007-08-31 01:57:58 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* smtp.c
|
2007-08-27 01:12:17 +02:00
|
|
|
* smtp SMTP E-mail sender
|
2007-08-31 01:57:58 +02:00
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
|
|
|
*
|
|
|
|
* Heavily leveraged from uIP 1.0 which also has a BSD-like license:
|
2007-08-27 01:12:17 +02:00
|
|
|
*
|
|
|
|
* The Simple Mail Transfer Protocol (SMTP) as defined by RFC821 is
|
|
|
|
* the standard way of sending and transfering e-mail on the
|
|
|
|
* Internet. This simple example implementation is intended as an
|
|
|
|
* example of how to implement protocols in uIP, and is able to send
|
|
|
|
* out e-mail but has not been extensively tested.
|
|
|
|
*
|
2007-08-31 01:57:58 +02:00
|
|
|
* Author: Adam Dunkels <adam@dunkels.com>
|
|
|
|
* Copyright (c) 2004, Adam Dunkels.
|
|
|
|
* All rights reserved.
|
2007-08-27 01:12:17 +02:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
2007-08-31 01:57:58 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
2007-08-27 01:12:17 +02:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
2007-09-09 19:20:56 +02:00
|
|
|
#include <stdio.h>
|
2007-08-31 01:57:58 +02:00
|
|
|
#include <stdlib.h>
|
2007-09-09 19:20:56 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2007-08-31 01:57:58 +02:00
|
|
|
#include <semaphore.h>
|
2007-09-02 23:58:35 +02:00
|
|
|
#include <sys/socket.h>
|
2007-08-27 01:12:17 +02:00
|
|
|
|
|
|
|
#include <net/uip/uip.h>
|
|
|
|
#include <net/uip/smtp.h>
|
|
|
|
|
2007-09-22 21:56:13 +02:00
|
|
|
#include "netutil-strings.h"
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-08-31 01:57:58 +02:00
|
|
|
#define SMTP_INPUT_BUFFER_SIZE 512
|
2007-08-27 01:12:17 +02:00
|
|
|
|
|
|
|
#define ISO_nl 0x0a
|
|
|
|
#define ISO_cr 0x0d
|
|
|
|
|
|
|
|
#define ISO_period 0x2e
|
|
|
|
|
|
|
|
#define ISO_2 0x32
|
|
|
|
#define ISO_3 0x33
|
|
|
|
#define ISO_4 0x34
|
|
|
|
#define ISO_5 0x35
|
|
|
|
|
2007-08-31 01:57:58 +02:00
|
|
|
/* This structure represents the state of a single SMTP transaction */
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-08-31 01:57:58 +02:00
|
|
|
struct smtp_state
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
2007-08-31 01:57:58 +02:00
|
|
|
uint8 state;
|
|
|
|
boolean connected;
|
|
|
|
sem_t sem;
|
|
|
|
uip_ipaddr_t smtpserver;
|
|
|
|
char *localhostname;
|
|
|
|
char *to;
|
|
|
|
char *cc;
|
|
|
|
char *from;
|
|
|
|
char *subject;
|
|
|
|
char *msg;
|
|
|
|
int msglen;
|
|
|
|
int sentlen;
|
|
|
|
int textlen;
|
|
|
|
int sendptr;
|
|
|
|
char buffer[SMTP_INPUT_BUFFER_SIZE];
|
|
|
|
};
|
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
static inline int smtp_send_message(int sockfd, struct smtp_state *psmtp)
|
2007-08-31 01:57:58 +02:00
|
|
|
{
|
2007-09-09 19:20:56 +02:00
|
|
|
if (recv(sockfd, psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, 0) < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-08-31 01:57:58 +02:00
|
|
|
if (strncmp(psmtp->buffer, smtp_220, 3) != 0)
|
|
|
|
{
|
2007-09-09 19:20:56 +02:00
|
|
|
return ERROR;
|
2007-08-31 01:57:58 +02:00
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
snprintf(psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, "%s%s\r\n", smtp_helo, psmtp->localhostname);
|
|
|
|
if (send(sockfd, psmtp->buffer, strlen(psmtp->buffer), 0) < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
if (recv(sockfd, psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, 0) < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-08-31 01:57:58 +02:00
|
|
|
if (psmtp->buffer[0] != ISO_2)
|
|
|
|
{
|
2007-09-09 19:20:56 +02:00
|
|
|
return ERROR;
|
2007-08-31 01:57:58 +02:00
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
snprintf(psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, "%s%s\r\n", smtp_mail_from, psmtp->from);
|
|
|
|
if (send(sockfd, psmtp->buffer, strlen(psmtp->buffer), 0) < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
if (recv(sockfd, psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, 0) < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-08-31 01:57:58 +02:00
|
|
|
if (psmtp->buffer[0] != ISO_2)
|
|
|
|
{
|
2007-09-09 19:20:56 +02:00
|
|
|
return ERROR;
|
2007-08-31 01:57:58 +02:00
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
snprintf(psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, "%s%s\r\n", smtp_rcpt_to, psmtp->to);
|
|
|
|
if (send(sockfd, psmtp->buffer, strlen(psmtp->buffer), 0) < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
if (recv(sockfd, psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, 0) < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-08-31 01:57:58 +02:00
|
|
|
if (psmtp->buffer[0] != ISO_2)
|
|
|
|
{
|
2007-09-09 19:20:56 +02:00
|
|
|
return ERROR;
|
2007-08-31 01:57:58 +02:00
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-08-31 01:57:58 +02:00
|
|
|
if (psmtp->cc != 0)
|
|
|
|
{
|
2007-09-09 19:20:56 +02:00
|
|
|
snprintf(psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, "%s%s\r\n", smtp_rcpt_to, psmtp->cc);
|
|
|
|
if (send(sockfd, psmtp->buffer, strlen(psmtp->buffer), 0) < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
if (recv(sockfd, psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, 0) < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-08-31 01:57:58 +02:00
|
|
|
if (psmtp->buffer[0] != ISO_2)
|
|
|
|
{
|
2007-09-09 19:20:56 +02:00
|
|
|
return ERROR;
|
2007-08-31 01:57:58 +02:00
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
if (send(sockfd, smtp_data, strlen(smtp_data), 0) < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
2007-08-31 01:57:58 +02:00
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
if (recv(sockfd, psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, 0) < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-08-31 01:57:58 +02:00
|
|
|
if (psmtp->buffer[0] != ISO_3)
|
|
|
|
{
|
2007-09-09 19:20:56 +02:00
|
|
|
return ERROR;
|
2007-08-31 01:57:58 +02:00
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
snprintf(psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, "%s%s\r\n", smtp_to, psmtp->to);
|
|
|
|
if (send(sockfd, psmtp->buffer, strlen(psmtp->buffer), 0) < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-08-31 01:57:58 +02:00
|
|
|
if (psmtp->cc != 0)
|
|
|
|
{
|
2007-09-09 19:20:56 +02:00
|
|
|
snprintf(psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, "%s%s\r\n", smtp_to, psmtp->cc);
|
|
|
|
if (send(sockfd, psmtp->buffer, strlen(psmtp->buffer), 0) < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
2007-08-31 01:57:58 +02:00
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
snprintf(psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, "%s%s\r\n", smtp_from, psmtp->from);
|
|
|
|
if (send(sockfd, psmtp->buffer, strlen(psmtp->buffer), 0) < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
snprintf(psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, "%s%s\r\n", smtp_subject, psmtp->subject);
|
|
|
|
if (send(sockfd, psmtp->buffer, strlen(psmtp->buffer), 0) < 0)
|
2007-08-31 01:57:58 +02:00
|
|
|
{
|
2007-09-09 19:20:56 +02:00
|
|
|
return ERROR;
|
2007-08-31 01:57:58 +02:00
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
if (send(sockfd, psmtp->msg, psmtp->msglen, 0) < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
if (send(sockfd, smtp_crnlperiodcrnl, strlen(smtp_crnlperiodcrnl), 0) < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
if (recv(sockfd, psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, 0) < 0)
|
2007-08-31 01:57:58 +02:00
|
|
|
{
|
2007-09-09 19:20:56 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
2007-08-31 01:57:58 +02:00
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
if (psmtp->buffer[0] != ISO_2)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
2007-08-31 01:57:58 +02:00
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
if (send(sockfd, smtp_quit, strlen(smtp_quit), 0) < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
2007-08-31 01:57:58 +02:00
|
|
|
}
|
2007-09-09 19:20:56 +02:00
|
|
|
return OK;
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
|
|
|
|
2007-08-31 01:57:58 +02:00
|
|
|
/* Specificy an SMTP server and hostname.
|
2007-08-27 01:12:17 +02:00
|
|
|
*
|
|
|
|
* This function is used to configure the SMTP module with an SMTP
|
|
|
|
* server and the hostname of the host.
|
|
|
|
*
|
2007-08-31 01:57:58 +02:00
|
|
|
* lhostname The hostname of the uIP host.
|
2007-08-27 01:12:17 +02:00
|
|
|
*
|
2007-08-31 01:57:58 +02:00
|
|
|
* server A pointer to a 4-byte array representing the IP
|
2007-08-27 01:12:17 +02:00
|
|
|
* address of the SMTP server to be configured.
|
|
|
|
*/
|
2007-08-31 01:57:58 +02:00
|
|
|
|
|
|
|
void smtp_configure(void *handle, char *lhostname, void *server)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
2007-08-31 01:57:58 +02:00
|
|
|
struct smtp_state *psmtp = (struct smtp_state *)handle;
|
|
|
|
psmtp->localhostname = lhostname;
|
|
|
|
uip_ipaddr_copy(psmtp->smtpserver, server);
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
2007-08-31 01:57:58 +02:00
|
|
|
|
|
|
|
/* Send an e-mail.
|
2007-08-27 01:12:17 +02:00
|
|
|
*
|
2007-08-31 01:57:58 +02:00
|
|
|
* to The e-mail address of the receiver of the e-mail.
|
|
|
|
* cc The e-mail address of the CC: receivers of the e-mail.
|
|
|
|
* from The e-mail address of the sender of the e-mail.
|
|
|
|
* subject The subject of the e-mail.
|
|
|
|
* msg The actual e-mail message.
|
|
|
|
* msglen The length of the e-mail message.
|
2007-08-27 01:12:17 +02:00
|
|
|
*/
|
2007-08-31 01:57:58 +02:00
|
|
|
|
|
|
|
int smtp_send(void *handle, char *to, char *cc, char *from, char *subject, char *msg, int msglen)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
2007-08-31 01:57:58 +02:00
|
|
|
struct smtp_state *psmtp = (struct smtp_state *)handle;
|
2007-09-02 23:58:35 +02:00
|
|
|
struct sockaddr_in server;
|
|
|
|
int sockfd;
|
2007-09-09 19:20:56 +02:00
|
|
|
int ret;
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-09-02 23:58:35 +02:00
|
|
|
/* Setup */
|
2007-08-31 01:57:58 +02:00
|
|
|
|
|
|
|
psmtp->connected = TRUE;
|
|
|
|
psmtp->to = to;
|
|
|
|
psmtp->cc = cc;
|
|
|
|
psmtp->from = from;
|
|
|
|
psmtp->subject = subject;
|
|
|
|
psmtp->msg = msg;
|
|
|
|
psmtp->msglen = msglen;
|
|
|
|
|
2007-09-02 23:58:35 +02:00
|
|
|
/* Create a socket */
|
|
|
|
|
|
|
|
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (sockfd < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
2007-09-01 20:06:15 +02:00
|
|
|
|
2007-09-02 23:58:35 +02:00
|
|
|
/* Connect to server. First we have to set some fields in the
|
|
|
|
* 'server' structure. The system will assign me an arbitrary
|
|
|
|
* local port that is not in use.
|
|
|
|
*/
|
|
|
|
|
|
|
|
server.sin_family = AF_INET;
|
|
|
|
memcpy(&server.sin_addr.s_addr, &psmtp->smtpserver, sizeof(in_addr_t));
|
|
|
|
server.sin_port = HTONS(25);
|
|
|
|
|
|
|
|
if (connect(sockfd, (struct sockaddr *)&server, sizeof(struct sockaddr_in)) < 0)
|
|
|
|
{
|
2007-09-09 19:20:56 +02:00
|
|
|
close(sockfd);
|
2007-09-02 23:58:35 +02:00
|
|
|
return ERROR;
|
2007-09-09 19:20:56 +02:00
|
|
|
}
|
2007-08-31 01:57:58 +02:00
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
/* Send the message */
|
2007-09-01 20:06:15 +02:00
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
ret = smtp_send_message(sockfd, psmtp);
|
2007-08-31 01:57:58 +02:00
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
close(sockfd);
|
|
|
|
return ret;
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
2007-08-31 01:57:58 +02:00
|
|
|
|
|
|
|
void *smtp_open(void)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
2007-08-31 01:57:58 +02:00
|
|
|
/* Allocate the handle */
|
|
|
|
|
|
|
|
struct smtp_state *psmtp = (struct smtp_state *)malloc(sizeof(struct smtp_state));
|
|
|
|
if (psmtp)
|
|
|
|
{
|
|
|
|
/* Initialize the handle */
|
|
|
|
|
|
|
|
memset(psmtp, 0, sizeof(struct smtp_state));
|
|
|
|
(void)sem_init(&psmtp->sem, 0, 0);
|
|
|
|
}
|
|
|
|
return (void*)psmtp;
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
2007-08-31 01:57:58 +02:00
|
|
|
|
|
|
|
void smtp_close(void *handle)
|
|
|
|
{
|
|
|
|
struct smtp_state *psmtp = (struct smtp_state *)handle;
|
|
|
|
if (psmtp)
|
|
|
|
{
|
|
|
|
sem_destroy(&psmtp->sem);
|
|
|
|
free(psmtp);
|
|
|
|
}
|
2007-09-04 01:35:18 +02:00
|
|
|
}
|