Implemented SMTP OAuth 2.0

dvh-chacham15-master 1.2.pre1
Hoa V. DINH 10 years ago
parent b4edf1bac7
commit 20aa2b2b15

Binary file not shown.

@ -27,6 +27,9 @@
C64BB21B16E2FC2F000DB34C /* qresync.c in Sources */ = {isa = PBXBuildFile; fileRef = C64BB21616E2FC2F000DB34C /* qresync.c */; };
C64BB21C16E2FC2F000DB34C /* qresync.c in Sources */ = {isa = PBXBuildFile; fileRef = C64BB21616E2FC2F000DB34C /* qresync.c */; };
C64BB21D16E2FC2F000DB34C /* qresync.c in Sources */ = {isa = PBXBuildFile; fileRef = C64BB21616E2FC2F000DB34C /* qresync.c */; };
C64DC0181788AB3C0012CA0B /* mailsmtp_oauth2.c in Sources */ = {isa = PBXBuildFile; fileRef = C64DC0151788AB3C0012CA0B /* mailsmtp_oauth2.c */; };
C64DC0191788AB3C0012CA0B /* mailsmtp_oauth2.c in Sources */ = {isa = PBXBuildFile; fileRef = C64DC0151788AB3C0012CA0B /* mailsmtp_oauth2.c */; };
C64DC01A1788AB3C0012CA0B /* mailsmtp_oauth2.c in Sources */ = {isa = PBXBuildFile; fileRef = C64DC0151788AB3C0012CA0B /* mailsmtp_oauth2.c */; };
C64EA7B716A00CA700778456 /* xgmmsgid.c in Sources */ = {isa = PBXBuildFile; fileRef = 365DFFD115D1C93100F2DD85 /* xgmmsgid.c */; };
C64EA7B816A00CA700778456 /* xgmmsgid.c in Sources */ = {isa = PBXBuildFile; fileRef = 365DFFD115D1C93100F2DD85 /* xgmmsgid.c */; };
C64EA7C116A00CC500778456 /* mailimap_id_parser.c in Sources */ = {isa = PBXBuildFile; fileRef = C64EA7B916A00CC100778456 /* mailimap_id_parser.c */; };
@ -589,6 +592,9 @@
C64BB21616E2FC2F000DB34C /* qresync.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = qresync.c; sourceTree = "<group>"; };
C64BB21716E2FC2F000DB34C /* qresync.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = qresync.h; sourceTree = "<group>"; };
C64BB21E16E32CDB000DB34C /* qresync_private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = qresync_private.h; sourceTree = "<group>"; };
C64DC0151788AB3C0012CA0B /* mailsmtp_oauth2.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = mailsmtp_oauth2.c; sourceTree = "<group>"; };
C64DC0161788AB3C0012CA0B /* mailsmtp_oauth2.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = mailsmtp_oauth2.h; sourceTree = "<group>"; };
C64DC0171788AB3C0012CA0B /* mailsmtp_private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = mailsmtp_private.h; sourceTree = "<group>"; };
C64EA7B916A00CC100778456 /* mailimap_id_parser.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = mailimap_id_parser.c; sourceTree = "<group>"; };
C64EA7BA16A00CC100778456 /* mailimap_id_parser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = mailimap_id_parser.h; sourceTree = "<group>"; };
C64EA7BB16A00CC200778456 /* mailimap_id_sender.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = mailimap_id_sender.c; sourceTree = "<group>"; };
@ -1657,6 +1663,9 @@
C6F9EAB1105335BC0059C3BA /* mailsmtp.h */,
C6F9EAB2105335BC0059C3BA /* mailsmtp_helper.c */,
C6F9EAB3105335BC0059C3BA /* mailsmtp_helper.h */,
C64DC0151788AB3C0012CA0B /* mailsmtp_oauth2.c */,
C64DC0161788AB3C0012CA0B /* mailsmtp_oauth2.h */,
C64DC0171788AB3C0012CA0B /* mailsmtp_private.h */,
C6F9EAB4105335BC0059C3BA /* mailsmtp_socket.c */,
C6F9EAB5105335BC0059C3BA /* mailsmtp_socket.h */,
C6F9EAB6105335BC0059C3BA /* mailsmtp_ssl.c */,
@ -2002,6 +2011,7 @@
C6F9ECB5105335BD0059C3BA /* mailimf_types.c in Sources */,
C6F9ECB7105335BD0059C3BA /* mailimf_types_helper.c in Sources */,
C6F9ECBA105335BD0059C3BA /* mailimf_write_file.c in Sources */,
C64DC0181788AB3C0012CA0B /* mailsmtp_oauth2.c in Sources */,
C6F9ECBC105335BD0059C3BA /* mailimf_write_generic.c in Sources */,
C6F9ECBE105335BD0059C3BA /* mailimf_write_mem.c in Sources */,
C6F9ECC6105335BD0059C3BA /* maildir.c in Sources */,
@ -2185,6 +2195,7 @@
C682E29215B315EF00BE9DA7 /* mime_message_driver.c in Sources */,
C682E29315B315EF00BE9DA7 /* mmapstring.c in Sources */,
C682E29415B315EF00BE9DA7 /* newsfeed.c in Sources */,
C64DC01A1788AB3C0012CA0B /* mailsmtp_oauth2.c in Sources */,
C682E29515B315EF00BE9DA7 /* newsfeed_item.c in Sources */,
C682E29615B315EF00BE9DA7 /* newsfeed_item_enclosure.c in Sources */,
C682E29715B315EF00BE9DA7 /* newsfeed_private.c in Sources */,
@ -2368,6 +2379,7 @@
C69AB29D1054704000F32FBD /* mime_message_driver.c in Sources */,
C69AB29F1054704000F32FBD /* mmapstring.c in Sources */,
C69AB2A21054704000F32FBD /* newsfeed.c in Sources */,
C64DC0191788AB3C0012CA0B /* mailsmtp_oauth2.c in Sources */,
C69AB2A41054704000F32FBD /* newsfeed_item.c in Sources */,
C69AB2A61054704000F32FBD /* newsfeed_item_enclosure.c in Sources */,
C69AB2A81054704000F32FBD /* newsfeed_private.c in Sources */,

@ -33,7 +33,7 @@ EXTRA_DIST = TODO
etpaninclude_HEADERS = \
mailsmtp.h mailsmtp_helper.h mailsmtp_socket.h mailsmtp_ssl.h \
mailsmtp_types.h
mailsmtp_types.h mailsmtp_oauth2.h
AM_CPPFLAGS = -I$(top_builddir)/include \
-I$(top_srcdir)/src/data-types
@ -41,4 +41,5 @@ AM_CPPFLAGS = -I$(top_builddir)/include \
noinst_LTLIBRARIES = libsmtp.la
libsmtp_la_SOURCES = \
mailsmtp.c mailsmtp_helper.c mailsmtp_socket.c mailsmtp_ssl.c
mailsmtp.c mailsmtp_helper.c mailsmtp_socket.c mailsmtp_ssl.c \
mailsmtp_oauth2.c

@ -40,6 +40,7 @@
#endif
#include "mailsmtp.h"
#include "mailsmtp_private.h"
#include "connect.h"
#include "base64.h"
#include "mail.h"
@ -1476,3 +1477,13 @@ void mailsmtp_set_logger(mailsmtp * session, void (* logger)(mailsmtp * session,
session->smtp_logger = logger;
session->smtp_logger_context = logger_context;
}
int mailsmtp_send_command(mailsmtp * f, char * command)
{
return send_command(f, command);
}
int mailsmtp_read_response(mailsmtp * session)
{
return read_response(session);
}

@ -45,6 +45,7 @@ extern "C" {
#include <libetpan/mailsmtp_helper.h>
#include <libetpan/mailsmtp_socket.h>
#include <libetpan/mailsmtp_ssl.h>
#include <libetpan/mailsmtp_oauth2.h>
LIBETPAN_EXPORT

@ -0,0 +1,98 @@
#include "mailsmtp_oauth2.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "base64.h"
#include "mailsmtp_private.h"
#define SMTP_STRING_SIZE 513
LIBETPAN_EXPORT
int mailsmtp_oauth2_authenticate(mailsmtp * session, const char * auth_user,
const char * access_token)
{
int r;
char command[SMTP_STRING_SIZE];
char * ptr;
char * full_auth_string;
char * full_auth_string_b64;
int auth_user_len;
int access_token_len;
int full_auth_string_len;
int res;
full_auth_string = NULL;
full_auth_string_b64 = NULL;
/* Build client response string */
auth_user_len = strlen(auth_user);
access_token_len = strlen(access_token);
full_auth_string_len = 5 + auth_user_len + 1 + 12 + access_token_len + 2;
full_auth_string = malloc(full_auth_string_len + 1);
if (full_auth_string == NULL) {
res = MAILSMTP_ERROR_MEMORY;
goto free;
}
ptr = memcpy(full_auth_string, "user=", 5);
ptr = memcpy(ptr + 5, auth_user, auth_user_len);
ptr = memcpy(ptr + auth_user_len, "\1auth=Bearer ", 13);
ptr = memcpy(ptr + 13, access_token, access_token_len);
ptr = memcpy(ptr + access_token_len, "\1\1\0", 3);
/* Convert to base64 */
full_auth_string_b64 = encode_base64(full_auth_string, full_auth_string_len);
if (full_auth_string_b64 == NULL) {
res = MAILSMTP_ERROR_MEMORY;
goto free;
}
snprintf(command, SMTP_STRING_SIZE, "AUTH XOAUTH2 %s\r\n", full_auth_string_b64);
r = mailsmtp_send_command(session, command);
if (r == -1) {
res = MAILSMTP_ERROR_STREAM;
goto free;
}
r = mailsmtp_read_response(session);
switch (r) {
case 220:
case 235:
res = MAILSMTP_NO_ERROR;
goto free;
case 334:
/* AUTH in progress */
/* There's probably an error, send an empty line as acknowledgement. */
snprintf(command, SMTP_STRING_SIZE, "\r\n");
r = mailsmtp_send_command(session, command);
if (r == -1) {
res = MAILSMTP_ERROR_STREAM;
goto free;
}
r = mailsmtp_read_response(session);
switch (r) {
case 535:
res = MAILSMTP_ERROR_AUTH_LOGIN;
goto free;
default:
res = MAILSMTP_ERROR_UNEXPECTED_CODE;
goto free;
}
break;
default:
res = MAILSMTP_ERROR_UNEXPECTED_CODE;
goto free;
}
free:
free(full_auth_string);
free(full_auth_string_b64);
return res;
}

@ -0,0 +1,11 @@
#ifndef MAILSMTP_OAUTH2_H
#define MAILSMTP_OAUTH2_H
#include <libetpan/mailsmtp_types.h>
LIBETPAN_EXPORT
int mailsmtp_oauth2_authenticate(mailsmtp * session, const char * auth_user,
const char * access_token);
#endif

@ -0,0 +1,11 @@
#ifndef MAILSMTP_PRIVATE_H
#define MAILSMTP_PRIVATE_H
#include <libetpan/mailsmtp_types.h>
int mailsmtp_send_command(mailsmtp * f, char * command);
int mailsmtp_read_response(mailsmtp * session);
#endif
Loading…
Cancel
Save