summaryrefslogtreecommitdiff
path: root/src/socket.c
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@gnutls.org>2014-08-09 09:43:45 +0200
committerNikos Mavrogiannopoulos <nmav@gnutls.org>2014-08-09 09:52:09 +0200
commitdbb1983fd4406f07d72714b4a2f689dacbfbc9ca (patch)
tree4aeb8b2434bf8ae854aa85e9daae0b9d7774a44f /src/socket.c
parente6d195fbe36c5cbe260877ed06265437d8fd0ff9 (diff)
downloadgnutls-dbb1983fd4406f07d72714b4a2f689dacbfbc9ca.tar.gz
danetool: supports SMTP starttls
Diffstat (limited to 'src/socket.c')
-rw-r--r--src/socket.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/socket.c b/src/socket.c
index ca38027f51..6885efcc0d 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -112,6 +112,63 @@ socket_send_range(const socket_st * socket, const void *buffer,
return ret;
}
+static
+ssize_t send_line(int fd, const char *txt)
+{
+ int len = strlen(txt);
+ int ret;
+
+ ret = send(fd, txt, len, 0);
+
+ if (ret == -1) {
+ fprintf(stderr, "error sending %s\n", txt);
+ exit(1);
+ }
+
+ return ret;
+}
+
+static
+ssize_t wait_for_text(int fd, const char *txt, unsigned txt_size)
+{
+ char buf[256];
+ int ret;
+
+ alarm(10);
+ do {
+ ret = recv(fd, buf, sizeof(buf), 0);
+ if (ret == -1) {
+ fprintf(stderr, "error receiving %s\n", txt);
+ exit(1);
+ }
+ } while(ret < (int)txt_size || strncmp(buf, txt, txt_size) != 0);
+
+ alarm(0);
+
+ return ret;
+}
+
+void
+socket_starttls(socket_st * socket, const char *app_proto)
+{
+ if (socket->secure)
+ return;
+
+ if (app_proto == NULL || strcasecmp(app_proto, "https") == 0)
+ return;
+
+ if (strcasecmp(app_proto, "smtp") == 0) {
+ send_line(socket->fd, "EHLO mail.example.com\n");
+ wait_for_text(socket->fd, "220 ", 4);
+ send_line(socket->fd, "STARTTLS\n");
+ wait_for_text(socket->fd, "220 ", 4);
+ } else {
+ fprintf(stderr, "unknown protocol %s\n", app_proto);
+ }
+
+ return;
+}
+
void socket_bye(socket_st * socket)
{
int ret;