mailer.c 8.56 KB
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
/* GNU mailutils - a suite of utilities for electronic mail
   Copyright (C) 1999, 2000 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Library Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   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 Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <errno.h>
#include <stdlib.h>
#include <stdarg.h>
#include <fcntl.h>
#include <ctype.h>

#include <mailer0.h>

int 	_mailer_sock_connect(char *host, int port);
char	*_mailer_find_mailbox(char *addr);
int 	_mailer_send_command(mailer_t ml, message_t msg, int cmd);
char 	*nb_fgets(char *buf, int size, int s);
const char 	*nb_fprintf(int s, const char *format, ...);
static int _mailer_rctp(mailer_t ml, char *str);

#define nb_read		read
#define nb_write 	write
#define BUFFSIZE 	4096

int
mailer_create(mailer_t *pml, message_t msg)
{
	mailer_t ml;

	(void)msg;
	if (!pml)
	  return EINVAL;

	ml = calloc (1, sizeof (*ml));
	if (ml == NULL)
		return (ENOMEM);

	*pml = ml;

	return (0);
}

int
mailer_destroy(mailer_t *pml)
{
	mailer_t ml;

	if (!pml)
		return (EINVAL);
	ml = *pml;
	if (ml->hostname)
		free(ml->hostname);
	mailer_disconnect(ml);
	free(ml);
	*pml = NULL;

	return (0);
}

int
mailer_connect(mailer_t ml, char *host)
{
	if (!ml || !host)
		return (EINVAL);

	if ((ml->socket = _mailer_sock_connect(host, 25)) < 0)
		return (-1);
	do
	{
		nb_fgets(ml->line_buf, MAILER_LINE_BUF_SIZE, ml->socket); /* read header line */
	} while ( strlen(ml->line_buf) > 4 && *(ml->line_buf+3) == '-');

	return (0);
}

int
mailer_disconnect(mailer_t ml)
{
	if (!ml || (ml->socket != -1))
		return (EINVAL);

	close(ml->socket);
	return (0);
}

int
mailer_send_header(mailer_t ml, message_t msg)
{
	header_t 	hdr;
	char		buf[64];

	if (!ml || !msg || (ml->socket == -1))
		return (EINVAL);

	if (!ml->hostname)
	{
	    if (gethostname(buf, 64) < 0)
	    	return (-1);
	    ml->hostname = strdup(buf);
	}

	if (_mailer_send_command(ml, msg, MAILER_HELO) != 0)
		return (-1);
	if (_mailer_send_command(ml, msg, MAILER_MAIL) != 0)
		return (-1);
	if (_mailer_send_command(ml, msg, MAILER_RCPT) != 0)
		return (-1);
	if (_mailer_send_command(ml, msg, MAILER_DATA) != 0)
		return (-1);

	message_get_header(msg, &hdr);
	header_get_stream(hdr, &(ml->stream));

	ml->state = MAILER_STATE_HDR;

	return (0);
}

int
mailer_send_message(mailer_t ml, message_t msg)
{
	int 	status, data_len = 0;
	size_t consumed = 0, len = 0;
	char 	*data, *p, *q;

	if (!ml || !msg || (ml->socket == -1))
		return (EINVAL);

	// alloca
	if (!(data = alloca(MAILER_LINE_BUF_SIZE)))
		return (ENOMEM);

	memset(data, 0, 1000);
	if ((status = stream_read(ml->stream, data, MAILER_LINE_BUF_SIZE, ml->offset, &len)) != 0)
		return (-1);

	if ((len == 0) && (ml->state == MAILER_STATE_HDR))
	{
		ml->state = MAILER_STATE_MSG;
		ml->offset = 0;
		message_get_stream(msg, &(ml->stream));
		return (1);
	}
	else if (len == 0)
	{
		strcpy(ml->line_buf, "\r\n.\r\n");
		consumed = strlen(data);
	}
	else
	{
		p = data;
		q = ml->line_buf;
		memset(ml->line_buf, 0, MAILER_LINE_BUF_SIZE);
		while (consumed < len)
		{
			// RFC821: if the first character on a line is a '.' you must add an
			//         extra '.' to the line which will get stipped off at the other end
			if ((*p == '.') && (ml->last_char == '\n'))
				ml->add_dot = 1;
			ml->last_char = *p;
			*q++ = *p++; 	// store the character
			data_len++;		// increase the length by 1
			consumed++;
			if (((MAILER_LINE_BUF_SIZE - data_len) > 1) && (ml->add_dot == 1))
			{
				*q++ = '.';
				data_len++;
				ml->add_dot = 0;
			}
		}
	}

	ml->offset += consumed;
	nb_fprintf(ml->socket, "%s\r\n", ml->line_buf);

	if (len == 0)
	{
		ml->state = MAILER_STATE_COMPLETE;
		return (0);
	}

	return (consumed);
}

int
_mailer_sock_connect(char *host, int port)
{
	struct sockaddr_in saddr;
	struct hostent *hp;
	int s;

	memset(&saddr, 0, sizeof(struct sockaddr_in));
	saddr.sin_family = AF_INET;
	saddr.sin_port = htons(port);
	if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1)
			return (-1);
	if ((hp = gethostbyname(host)) == 0)
			return (-1);
	memcpy(&saddr.sin_addr, hp->h_addr, hp->h_length);
	if (connect(s, (struct sockaddr *)&saddr, sizeof(saddr)) == 0)
			return (s);
	close(s);

	return (-1);
}

char *
_mailer_find_mailbox(char *addr)
{
    char *p, *c;
    p = addr;
    if ( (c = strchr( p, '<')) != 0)
    {
        p = c+1;
        if ( (c = strchr( p, '>')) )
            *c = '\0';
    }
    else if ( (c = strchr( p, '(' )) != 0 )
    {
        --c;
        while ( c > p && *c && isspace( *c ) ) {
            *c = '\0';
            --c;
        }
    }
    return p;
}

static int
_mailer_rctp(mailer_t ml, char *str)
{
	char 		*p, *c = NULL, *q = NULL;

	for (q = p = str; q && *p; p = q+1)
	{
		if ( (q = strchr( p, ',')) )
			*q = '\0';
		while ( p && *p && isspace( *p ) )
			p++;
		c = strdup(p);
		p = _mailer_find_mailbox(c);
		nb_fprintf(ml->socket, "RCPT TO:<%s>\r\n", p);
		free(c);
		nb_fgets(ml->line_buf, sizeof(ml->line_buf), ml->socket);
		if (strncmp(ml->line_buf, "250", 3))
			return (-strtol(ml->line_buf, 0, 10));
	}
	return (0);
}

int
_mailer_send_command(mailer_t ml, message_t msg, int cmd)
{
	header_t	hdr;
	char 		*p;
	char		str[128];
	size_t		str_len;
	const char		*success = "250";

	switch (cmd)
	{
		case MAILER_HELO:
			nb_fprintf(ml->socket, "HELO %s\r\n", ml->hostname);
			break;
		case MAILER_MAIL:
			message_get_header(msg, &hdr);
			header_get_value(hdr, MU_HEADER_FROM, str, 128, &str_len);
			str[str_len] = '\0';
			p = _mailer_find_mailbox(str);
			nb_fprintf(ml->socket, "MAIL From: %s\r\n", p);
			break;
		case MAILER_RCPT:
			message_get_header(msg, &hdr);
			header_get_value(hdr, MU_HEADER_TO, str, 128, &str_len);
			str[str_len] = '\0';
			if (_mailer_rctp(ml, str) == -1)
				return (-1);
			header_get_value(hdr, MU_HEADER_CC, str, 128, &str_len);
			str[str_len] = '\0';
			if (_mailer_rctp(ml, str) == -1)
				return (-1);
			return (0);
			break;
		case MAILER_DATA:
			nb_fprintf(ml->socket, "DATA\r\n");
			success = "354";
			break;
		case MAILER_RSET:
			nb_fprintf(ml->socket, "RSET\r\n");
			break;
		case MAILER_QUIT:
			nb_fprintf(ml->socket, "QUIT\r\n");
			success = "221";
			break;
	}

	nb_fgets(ml->line_buf, sizeof(ml->line_buf), ml->socket);
	if (strncmp(ml->line_buf, success, 3) == 0)
		return (0);
	else
		return (-strtol(ml->line_buf, 0, 10));
}

char *
nb_fgets( char *buf, int size, int s )
{
	static char *buffer[25];
	char *p, *b, *d;
	int bytes, i;
	int flags;

	if ( !buffer[s] && !( buffer[s] = calloc( BUFFSIZE+1, 1 ) ) )
		return 0;
	bytes = i = strlen( p = b = buffer[s] );
	*( d = buf ) = '\0';
	for ( ; i-- > 0; p++ )
		{
		if ( *p == '\n' )
			{
			char c = *( p+1 );

			*( p+1 ) = '\0';
			strcat( d, b );
			*( p+1 ) = c;
			memmove( b, p+1, i+1 );
			return buf;
			}
		}
	flags = fcntl( s, F_GETFL );
	fcntl( s, F_SETFL, O_NONBLOCK );
	while ( bytes <= size )
		{
		fd_set fds;

		FD_ZERO( &fds );
		FD_SET( s, &fds );
		select( s+1, &fds, 0, 0, 0 ); /* we really don't care what it returns */
		if ( ( i = nb_read( s, p, BUFFSIZE - bytes ) ) == -1 )
			{
			*b = '\0';
			return 0;
			}
		else if ( i == 0 )
			{
			*( p+1 ) = '\0';
			strcat( d, b );
			*b = '\0';
			fcntl( s, F_SETFL, flags );
			return strlen( buf ) ? buf : 0;
			}
		*( p+i ) = '\0';
		bytes += i;
		for ( ; i-- > 0; p++ )
			{
			if ( *p == '\n' )
				{
				char c = *( p+1 );

				*( p+1 ) = '\0';
				strcat( d, b );
				*( p+1 ) = c;
				memmove( b, p+1, i+1 );
				fcntl( s, F_SETFL, flags );
				return buf;
				}
			}
		if ( bytes == BUFFSIZE )
			{
			memcpy( d, b, BUFFSIZE );
			d += BUFFSIZE;
			size -= BUFFSIZE;
			bytes = 0;
			*( p = b ) = '\0';
			}
		}
	memcpy( d, b, size );
	memmove( b, b+size, strlen( b+size )+1 );
	fcntl( s, F_SETFL, flags );
	return buf;
}

const char *
nb_fprintf( int s, const char *format, ... )
{
	char buf[MAILER_LINE_BUF_SIZE];
	va_list vl;
	int i;

	va_start( vl, format );
	vsprintf( buf, format, vl );
	va_end( vl );
	i = strlen( buf );
	if ( nb_write( s, buf, i ) != i )
		return 0;
	return format;
}