summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosé Fonseca <jrfonseca@users.sourceforge.net>2003-07-04 16:28:14 +0000
committerJosé Fonseca <jrfonseca@users.sourceforge.net>2003-07-04 16:28:14 +0000
commitcd2170b37673352903432c4851a31168a5e5e5bb (patch)
tree4d8f8e90808274ea0b4930724052a41e8d4acea5
parent15350708d628c8be91ca0ac00ec7956bbf3f6c80 (diff)
Search for the global configuration file.
Do not make the CRLF line translation for the MDA pipe.
-rw-r--r--local.c6
-rw-r--r--main.c22
-rw-r--r--main.h6
-rw-r--r--message.c11
-rw-r--r--parser.y117
-rw-r--r--rcfile.h11
-rw-r--r--smtp.c5
7 files changed, 114 insertions, 64 deletions
diff --git a/local.c b/local.c
index 24c11af..a2545bf 100644
--- a/local.c
+++ b/local.c
@@ -49,7 +49,7 @@ void local_init(message_t *message)
if (!mda)
{
- fprintf(stderr, "local delivery not possible without a MDA");
+ fprintf(stderr, "Local delivery not possible without a MDA\n");
exit(EX_OSFILE);
}
@@ -167,7 +167,7 @@ void local_init(message_t *message)
if(!(mda_fp = popen(before, "w")))
{
- fprintf(stderr, "MDA open failed\n");
+ fprintf(stderr, "Failed to connect to MDA\n");
exit(EX_OSERR);
}
@@ -184,7 +184,7 @@ void local_flush(message_t *message)
do {
n = message_read(message, buffer, BUFSIZ);
- if(fwrite(buffer, 1, n, mda_fp) != n)
+ if(ferror(mda_fp))
{
perror(NULL);
exit(EX_OSERR);
diff --git a/main.c b/main.c
index 022c8d7..8945533 100644
--- a/main.c
+++ b/main.c
@@ -13,6 +13,7 @@
#include "message.h"
#include "smtp.h"
#include "local.h"
+#include "rcfile.h"
/** Modes of operation. */
@@ -35,14 +36,12 @@ int main (int argc, char **argv)
enum notify_flags notify = Notify_NOTSET;
char *from = NULL;
message_t *message;
- int parse_headers, local, remote;
+ int parse_headers = 0, local, remote;
opmode_t mode;
+ char *rcfile = NULL;
identities_init();
- /* Parse the rc file. */
- parse_rcfile();
-
/* Set the default mode of operation. */
if (strcmp(argv[0], "mailq") == 0) {
mode = MAILQ;
@@ -298,6 +297,9 @@ int main (int argc, char **argv)
exit (EX_USAGE);
}
+ /* Parse the rc file. */
+ rcfile_parse(rcfile);
+
message = message_new();
/** Parse the envelope headers */
@@ -330,18 +332,8 @@ int main (int argc, char **argv)
else
{
local_init(message);
-
smtp_send(message);
-
- if(ferror(mda_fp))
- {
- perror(NULL);
- exit(EX_OSERR);
- }
-
- if(!message_eof(message))
- local_flush(message);
-
+ local_flush(message);
local_cleanup();
}
diff --git a/main.h b/main.h
index 5533d54..6686bc7 100644
--- a/main.h
+++ b/main.h
@@ -9,6 +9,7 @@
#include <stdio.h>
+
/**
* Error codes as specified in sendmail's sysexits.h
*/
@@ -35,9 +36,4 @@ extern FILE *log_fp;
extern int verbose;
-extern char *rcfile;
-
-
-extern void parse_rcfile(void);
-
#endif
diff --git a/message.c b/message.c
index 1513691..8adb463 100644
--- a/message.c
+++ b/message.c
@@ -133,8 +133,17 @@ static char *message_buffer_readline(message_t *message)
static void message_buffer_fill(message_t *message)
{
FILE *fp = message->fp ? message->fp : stdin;
+ size_t n;
- message->buffer_stop += fread(message->buffer, 1, message->buffer_size - message->buffer_stop, fp);
+ if((n = fread(message->buffer + message->buffer_stop, 1, message->buffer_size - message->buffer_stop, fp)))
+ {
+ /* hook for the MDA pipe */
+ if(mda_fp)
+ fwrite(message->buffer + message->buffer_stop, 1, n, mda_fp);
+
+ message->buffer_stop += n;
+ }
+
}
static size_t message_buffer_flush(message_t *message, char *ptr, size_t size)
diff --git a/parser.y b/parser.y
index fa786ec..61eb173 100644
--- a/parser.y
+++ b/parser.y
@@ -21,7 +21,8 @@
/* parser reads these */
-char *rcfile = NULL; /* path name of dot file */
+
+static const char *rcfile = NULL; /* path name of dot file */
/* parser sets these */
int yydebug; /* in case we didn't generate with -- debug */
@@ -96,53 +97,99 @@ extern int lineno;
extern char *yytext;
extern FILE *yyin;
+/**
+ * Report a syntax error.
+ */
void yyerror (const char *s)
-/* report a syntax error */
{
fprintf(stderr, "%s:%d: %s at %s\n", rcfile, lineno, s,
(yytext && yytext[0]) ? yytext : "end of input");
exit(EX_CONFIG);
}
-#define RCFILE ".esmtprc"
+#define RCFILE "esmtprc"
+#define DOT_RCFILE "." RCFILE
+#define ETC_RCFILE "/etc/" RCFILE
-void parse_rcfile (void)
+void rcfile_parse(const char *_rcfile)
{
- if(!rcfile)
- {
- char *home;
-
- /* Setup the rcfile name. */
- if (!(home = getenv("HOME")))
- return;
-
- if (!(rcfile = malloc(strlen(home) + strlen(RCFILE) + 2)))
- return;
-
- strcpy(rcfile, home);
- if (rcfile[strlen(rcfile) - 1] != '/')
- strcat(rcfile, "/");
- strcat(rcfile, RCFILE);
- }
-
- identity = default_identity;
-
- /* Open the configuration file and feed it to the lexer. */
- if (!(yyin = fopen(rcfile, "r")))
- {
- if (errno != ENOENT)
+ char *temp = NULL;
+
+ if(_rcfile)
{
- fprintf(stderr, "open: %s: %s\n", rcfile, strerror(errno));
+ /* Configuration file specified on the command line */
+ if(!(yyin = fopen(_rcfile, "r")))
+ goto failure;
+
+ rcfile = _rcfile;
+ goto success;
}
- }
- else
- {
- yyparse(); /* parse entire file */
+
+ /* Search for the user configuration file */
+ do
+ {
+ char *home;
+
+ if (!(home = getenv("HOME")))
+ break;
+
+ temp = xmalloc(strlen(home) + strlen(DOT_RCFILE) + 2);
+
+ strcpy(temp, home);
+ if (temp[strlen(temp) - 1] != '/')
+ strcat(temp, "/");
+ strcat(temp, DOT_RCFILE);
+
+ if(!(yyin = fopen(temp, "r")))
+ {
+ if(errno == ENOENT)
+ break;
+ else
+ goto failure;
+ }
+
+ rcfile = temp;
+ goto success;
+ }
+ while(0);
+
+ /* Search for the global configuration file */
+ do {
+ if(!(yyin = fopen(ETC_RCFILE, "r")))
+ {
+ if(errno == ENOENT)
+ break;
+ else
+ goto failure;
+ }
+
+ rcfile = ETC_RCFILE;
+ goto success;
+ }
+ while(0);
+
+ /* No configuration file found */
+ return;
+
+success:
+ /* Configuration file opened */
+
+ identity = default_identity;
+
+ yyparse(); /* parse entire file */
fclose(yyin); /* not checking this should be safe, file mode was r */
- }
-
- free(rcfile);
+
+ rcfile = NULL;
+ if(temp)
+ free(temp);
+
+ return;
+
+failure:
+ /* Failure to open the configuration file */
+ fprintf(stderr, "open: %s: %s\n", rcfile, strerror(errno));
+ exit(EX_CONFIG);
}
/* easier to do this than cope with variations in where the library lives */
diff --git a/rcfile.h b/rcfile.h
new file mode 100644
index 0000000..f6efc07
--- /dev/null
+++ b/rcfile.h
@@ -0,0 +1,11 @@
+/**
+ * \file rcfile.h
+ * Parsing of the configuration file.
+ */
+
+#ifndef _RCFILE_H
+#define _RCFILE_H
+
+extern void rcfile_parse(const char *rcfile);
+
+#endif
diff --git a/smtp.c b/smtp.c
index e71b5cd..3c3e4fa 100644
--- a/smtp.c
+++ b/smtp.c
@@ -18,7 +18,6 @@
#include <libesmtp.h>
#include "smtp.h"
-#include "local.h"
#include "main.h"
#include "xmalloc.h"
@@ -149,10 +148,6 @@ static const char * message_cb (void **buf, int *len, void *arg)
*len = message_read(message, *buf, BUFSIZ);
- /** hook for the MDA */
- if(mda_fp)
- fwrite(*buf, 1, *len, mda_fp);
-
return *buf;
}