SMTP specific parts moved to smtp.c, and some definitions were moved
authorLadislav Láska <ladislav.laska@gmail.com>
Wed, 20 Oct 2010 17:25:51 +0000 (19:25 +0200)
committerLadislav Láska <ladislav.laska@gmail.com>
Wed, 20 Oct 2010 17:25:51 +0000 (19:25 +0200)
to according .h files.

main.h [new file with mode: 0644]
smtp.c [new file with mode: 0644]
smtp.h [new file with mode: 0644]

diff --git a/main.h b/main.h
new file mode 100644 (file)
index 0000000..b06192f
--- /dev/null
+++ b/main.h
@@ -0,0 +1,36 @@
+/******************************************************************************
+ * Filename: main.h
+ * Description:
+ *
+ * Version: 1.0
+ * Created: Oct 20 2010 17:38:43
+ * Last modified: Oct 20 2010 17:38:43
+ *
+ * Author: Ladislav Láska
+ * e-mail: ladislav.laska@gmail.com
+ *
+ ******************************************************************************/
+#ifndef _MAIN_H_
+#define _MAIN_H_
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <gnutls/gnutls.h>
+
+enum {
+       S_UNREACHABLE = -1,
+       S_NO_X509 = -2,
+       S_OK = 0,
+       S_WARNING = 1,
+       S_ERROR = 2,
+       S_UNKNOWN = 3,
+};
+
+#define BUFFER_SIZE 1024
+
+
+#define die(msg) { fprintf(stderr, "Error: " msg "\n" ); exit(S_ERROR); }
+#define dief(msg, ...) { fprintf(stderr, "Error: " msg "\n", __VA_ARGS__ ); exit(S_ERROR); }
+#define gnutls_die(code) { gnutls_perror(code); exit(S_ERROR); }
+
+#endif
diff --git a/smtp.c b/smtp.c
new file mode 100644 (file)
index 0000000..4adb179
--- /dev/null
+++ b/smtp.c
@@ -0,0 +1,54 @@
+/******************************************************************************
+ * Filename: smtp.c
+ * Description: Simple smtp handling routines for cert-checker
+ *
+ * Version: 1.0
+ * Created: Oct 20 2010 17:31:31
+ * Last modified: Oct 20 2010 17:31:31
+ *
+ * Author: Ladislav Láska
+ * e-mail: ladislav.laska@gmail.com
+ *
+ ******************************************************************************/
+
+#define _XOPEN_SOURCE 500
+
+#include <unistd.h>
+#include <string.h>
+
+#include "main.h"
+
+/* Expect status code */
+char* smtp_expect(int fd, char *str) {
+       char buffer[BUFFER_SIZE];
+       int len = strlen(str);
+       int bytes = read(fd, buffer, BUFFER_SIZE);
+       if (bytes < len) goto fail;
+       if (!strncmp(buffer,str,len)) {
+               while (bytes == BUFFER_SIZE)
+                       bytes = read(fd, buffer, BUFFER_SIZE) > 0;
+               return NULL;
+       }
+
+       fail:;
+       char *ptr = strdup(buffer);
+       while (bytes == BUFFER_SIZE)
+               bytes = read(fd, buffer, BUFFER_SIZE) > 0;
+       return ptr;
+}
+
+/* Send EHLO and check for STARTTLS extension. */
+int smtp_ehlo(int fd) {
+       return 0;
+}
+
+int smtp_starttls(int fd) {
+       smtp_expect(fd, "220"); /* Read everything in buffer */
+       char buffer[] = "STARTTLS\n";
+       write(fd, buffer, sizeof(buffer));
+       char *b;
+       if ((b = smtp_expect(fd, "220 "))) {
+               dief("STARTTLS declined: %s.", b);
+       }
+       return 0;
+}
diff --git a/smtp.h b/smtp.h
new file mode 100644 (file)
index 0000000..3c5634e
--- /dev/null
+++ b/smtp.h
@@ -0,0 +1,20 @@
+/******************************************************************************
+ * Filename: smtp.h
+ * Description:
+ *
+ * Version: 1.0
+ * Created: Oct 20 2010 17:34:54
+ * Last modified: Oct 20 2010 17:34:54
+ *
+ * Author: Ladislav Láska
+ * e-mail: ladislav.laska@gmail.com
+ *
+ ******************************************************************************/
+#ifndef _SMTP_H_
+#define _SMTP_H_
+
+int smtp_ehlo(int fd);
+char* smtp_expect(int fd, char *str);
+int smtp_starttls(int fd);
+
+#endif