1 /******************************************************************************
3 * Description: Simple smtp handling routines for cert-checker
6 * Created: Oct 20 2010 17:31:31
7 * Last modified: Oct 20 2010 17:31:31
9 * Author: Ladislav Láska
10 * e-mail: ladislav.laska@gmail.com
12 ******************************************************************************/
14 #define _XOPEN_SOURCE 500
16 #include <gnutls/gnutls.h>
23 /* Expect status code */
24 char* smtp_expect(int fd, char *str) {
25 char buffer[BUFFER_SIZE];
26 int len = strlen(str);
27 int bytes = read(fd, buffer, BUFFER_SIZE);
28 if (bytes < len) goto fail;
29 if (!strncmp(buffer,str,len)) {
30 while (bytes == BUFFER_SIZE)
31 bytes = read(fd, buffer, BUFFER_SIZE) > 0;
36 char *ptr = strdup(buffer);
37 while (bytes == BUFFER_SIZE)
38 bytes = read(fd, buffer, BUFFER_SIZE) > 0;
42 /* Expect status code. This can be done better. */
43 char* ssl_smtp_expect(gnutls_session_t session, char *str) {
44 char buffer[BUFFER_SIZE];
45 int len = strlen(str);
46 int bytes = gnutls_record_recv(session, buffer, BUFFER_SIZE);
47 if (bytes < len) goto fail;
48 if (!strncmp(buffer,str,len)) {
53 return strdup(buffer);
56 /* Send EHLO and check for STARTTLS extension. */
57 int smtp_ehlo(int fd) {
61 int smtp_starttls(int fd) {
62 smtp_expect(fd, "220"); /* Read everything in buffer */
63 char buffer[] = "STARTTLS\n";
64 write(fd, buffer, sizeof(buffer));
66 if ((b = smtp_expect(fd, "220 "))) {
67 dief("STARTTLS declined: %s.", b);
72 /* Ugly, sending could fail and other horrible things may happen. */
73 int smtp_quit(gnutls_session_t session) {
74 char buffer[] = "QUIT\n";
75 int sent = GNUTLS_E_AGAIN;
76 sent = gnutls_record_send(session, buffer, sizeof(buffer));
78 ssl_smtp_expect(session, "221 ");
80 /* TODO: Better (read: some) error handling. */