-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpam_smtp.cpp
More file actions
154 lines (135 loc) · 5.72 KB
/
Copy pathpam_smtp.cpp
File metadata and controls
154 lines (135 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// pam_smtp module, v1.2.0, 2026-05-10
//
// Copyright (C) 2023, Martin Young <martin_young@live.cn>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or any
// later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//------------------------------------------------------------------------
#include <memory>
#include <string>
#include <cstring>
#include <syslog.h>
#include <security/pam_modules.h>
#include <security/pam_ext.h>
#include <curl/curl.h>
using namespace std;
PAM_EXTERN int pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
const char *emptys{""};
const char *proto[]{"smtp", "smtps"};
const char *puser{nullptr}, *ppwd{nullptr}, *pproto{nullptr}, *pdomain{nullptr};
long usessl{0};
auto close_curl_hd = [](CURL *hd) { curl_easy_cleanup(hd); };
auto getproto = [argv,proto] {
return strcmp(argv[1],"starttls")==0? proto[0] :
(strcmp(argv[1],"tls" )==0? proto[1] : nullptr);
};
try {
switch(argc) {
case 0:
pam_syslog(pamh, LOG_ERR, "No option");
return PAM_SERVICE_ERR;
case 1: // no protocol and domain_name arguments
pproto = proto[0];
usessl = CURLUSESSL_NONE;
pdomain= emptys;
break;
case 2:
if( argv[1][0]=='@' ) { // 2nd argument is domain_name, no protocol argument
pproto = proto[0];
usessl = CURLUSESSL_NONE;
pdomain= argv[1];
} else { // 2nd argument is protocol, no domain_name argument
if( !(pproto=getproto()) ) {
pam_syslog(pamh, LOG_ERR, "Bad protocol option: %s. Expected: starttls|tls", argv[1]);
return PAM_SERVICE_ERR;
}
usessl = CURLUSESSL_CONTROL;
pdomain= emptys;
}
break;
default:
if( !(pproto=getproto()) ) { // 2nd argument is protocol
pam_syslog(pamh, LOG_ERR, "Bad protocol option: %s. Expected: starttls|tls", argv[1]);
return PAM_SERVICE_ERR;
}
if( argv[2][0]!='@' ) { // 3rd argument is domain_name
pam_syslog(pamh, LOG_ERR, "Bad domain_name option: %s. Expected: the leading '@'", argv[2]);
return PAM_SERVICE_ERR;
}
usessl = CURLUSESSL_CONTROL;
pdomain= argv[2];
}
if (int retval {pam_get_item(pamh, PAM_USER, (const void **)&puser)}; retval != PAM_SUCCESS) {
pam_syslog(pamh, LOG_ERR, "Cannot get username: %s", pam_strerror(pamh, retval));
return PAM_SERVICE_ERR;
}
if (!(puser && *puser)) {
pam_syslog(pamh, LOG_NOTICE, "Empty username");
return PAM_AUTH_ERR;
}
if( int retval {pam_get_authtok(pamh, PAM_AUTHTOK, &ppwd, nullptr)}; retval != PAM_SUCCESS ) {
pam_syslog(pamh, LOG_ERR, "Cannot get password: %s", pam_strerror(pamh, retval));
return PAM_SERVICE_ERR;
}
if( !(ppwd && *ppwd) ) {
pam_syslog(pamh, LOG_NOTICE, "Empty password");
return PAM_AUTH_ERR;
}
unique_ptr<CURL, decltype(close_curl_hd)> curlhd(curl_easy_init(), close_curl_hd);
if( !curlhd ) {
pam_syslog(pamh, LOG_ERR, "CURL library failed: curl_easy_init()");
return PAM_SERVICE_ERR;
}
if( curl_easy_setopt(curlhd.get(), CURLOPT_URL, (pproto+"://"s+argv[0]).c_str()) != CURLE_OK ||
curl_easy_setopt(curlhd.get(), CURLOPT_USERPWD, (string(puser)+pdomain+":"s+ppwd).c_str()) != CURLE_OK ||
curl_easy_setopt(curlhd.get(), CURLOPT_SSL_VERIFYPEER, 0L) != CURLE_OK ||
curl_easy_setopt(curlhd.get(), CURLOPT_SSL_VERIFYHOST, 0L) != CURLE_OK ||
curl_easy_setopt(curlhd.get(), CURLOPT_USE_SSL, usessl) != CURLE_OK )
{
pam_syslog(pamh, LOG_ERR, "CURL library function call failed: curl_easy_setopt()");
return PAM_SERVICE_ERR;
}
if( CURLcode clcode {curl_easy_perform(curlhd.get())}; clcode != CURLE_OK ) {
pam_syslog(pamh, LOG_NOTICE, "SMTP auth failed: %s", curl_easy_strerror(clcode));
return PAM_AUTH_ERR;
}
pam_syslog(pamh, LOG_INFO, "SMTP auth success for user %s", puser);
return PAM_SUCCESS;
} catch (const exception& e) {
pam_syslog(pamh, LOG_ERR, "Standard exception caught: %s", e.what());
} catch (...) {
pam_syslog(pamh, LOG_ERR, "Unknown exception caught");
}
return PAM_SERVICE_ERR;
}
PAM_EXTERN int pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
return PAM_SUCCESS;
}
PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
return PAM_IGNORE;
}
PAM_EXTERN int pam_sm_open_session(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
return PAM_IGNORE;
}
PAM_EXTERN int pam_sm_close_session(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
return PAM_IGNORE;
}
PAM_EXTERN int pam_sm_chauthtok(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
return PAM_IGNORE;
}