forked from CERN-CERT/pam_2fa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsms.c
238 lines (202 loc) · 6.67 KB
/
sms.c
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <time.h>
#include "pam_2fa.h"
static int send_mail(char *dst, char *text, module_config *cfg);
static int rnd_numb(char *otp, int length);
void* sms_pre_auth_func (pam_handle_t * pamh, user_config * user_cfg, module_config * cfg);
int sms_auth_func (pam_handle_t * pamh, user_config * user_cfg, module_config * cfg, const char *otp, void* data);
const auth_mod sms_auth = {
.pre_auth = &sms_pre_auth_func,
.do_auth = &sms_auth_func,
.name = "SMS OTP",
.prompt = "Please put this code here: ",
.otp_len = 0,
};
void sms_load_user_file(pam_handle_t *pamh, const module_config *cfg,
struct passwd *user_entry, user_config *user_cfg)
{
int fd, retval;
char *filename;
char buf[SMS_MOBILE_LEN+2];
char *buf_pos;
size_t i, buf_rem, buf_len;
ssize_t bytes_read;
if (asprintf(&filename, "%s/%s", user_entry->pw_dir, cfg->sms_user_file) < 0) {
pam_syslog(pamh, LOG_DEBUG, "Can't allocate filename buffer");
return;
}
{
// check the exitence of the file
struct stat st;
retval = stat(filename, &st);
if (retval < 0) {
pam_syslog(pamh, LOG_DEBUG, "Can't get stats of file '%s'", filename);
free(filename);
return;
}
if (!S_ISREG(st.st_mode)) {
pam_syslog(pamh, LOG_ERR, "Not a regular file '%s'", filename);
free(filename);
return;
}
}
fd = open(filename, O_RDONLY);
if (fd < 0) {
pam_syslog(pamh, LOG_ERR, "Can't open file '%s'", filename);
free(filename);
return;
}
free(filename);
buf_pos = buf;
buf_rem = SMS_MOBILE_LEN+1;
while ((bytes_read = read(fd, buf_pos, buf_rem)) > 0) {
buf_pos += (size_t)bytes_read; // This is always > 0 by construct
buf_rem = (size_t)((ssize_t)buf_rem - bytes_read);
if (buf_rem == 0)
break;
}
*buf_pos = 0;
close(fd);
buf_len = (size_t)(buf_pos - buf); // This is always >= 0 by construct
if (buf_len > SMS_MOBILE_LEN) {
pam_syslog(pamh, LOG_ERR, "SMS number too long (%li)'", buf_pos - buf);
return;
}
for (i = 0; i <= buf_len && buf[i] >= '0' && buf[i] <= '9'; ++i);
if (i != buf_len + 1 && buf[i] != '\n' && buf[i] != '\r') {
pam_syslog(pamh, LOG_ERR, "SMS number contain non integer: \"%.*s\" '%i' %zu %zu", (int)(i+1), buf, buf[i], i, buf_len);
return;
}
memcpy(user_cfg->sms_mobile, buf, i);
user_cfg->sms_mobile[i] = 0;
}
void* sms_pre_auth_func (pam_handle_t * pamh, user_config * user_cfg, module_config * cfg) {
int retval;
char * code;
code = malloc(cfg->sms_otp_length + 1);
if (code == NULL) {
pam_syslog(pamh, LOG_CRIT, "Out of memory");
return NULL;
}
//GENERATE OTP/RANDOM CODE
rnd_numb(code, (int) cfg->sms_otp_length);
if (user_cfg->sms_mobile[0]) {
char * dst, * txt;
if (asprintf(&dst, "%s@%s", user_cfg->sms_mobile, cfg->sms_gateway) < 0) {
pam_syslog(pamh, LOG_ERR, "%s Failed to allocate SMS destination", LOG_PREFIX);
pam_prompt(pamh, PAM_ERROR_MSG, NULL, "Failed to allocate SMS destination");
free(code);
return NULL;
}
if (asprintf(&txt, "%s%s", cfg->sms_text, code) < 0) {
pam_syslog(pamh, LOG_ERR, "%s Failed to allocate SMS text", LOG_PREFIX);
pam_prompt(pamh, PAM_ERROR_MSG, NULL, "Failed to allocate SMS text");
free(code);
free(dst);
return NULL;
}
DBG(("Mail [%s] %s: %s", dst, cfg->sms_subject, txt));
pam_syslog(pamh, LOG_DEBUG, "Sending SMS to %s", dst);
retval = send_mail(dst, txt, cfg);
DBG(("Return status = %d", retval));
free(dst);
free(txt);
if (retval != 0) {
pam_syslog(pamh, LOG_ERR, "%s Failed to send authentication code by SMS!",
LOG_PREFIX);
pam_prompt(pamh, PAM_ERROR_MSG, NULL, "Failed to send authentication code by SMS!\n");
free(code);
return NULL;
}
}
pam_prompt(pamh, PAM_TEXT_INFO, NULL, SMS_TEXT_WAIT);
return code;
}
int sms_auth_func (pam_handle_t * pamh, user_config * user_cfg, module_config * cfg, const char *otp, void *data) {
char * code;
int retval;
code = (char*) data;
if (otp == NULL) {
DBG(("Module error: auth called without an otp"));
free(code);
return PAM_AUTH_ERR;
}
DBG(("code entered = %s", otp));
// VERIFY IF VALID INPUT !
retval = strncmp(code, otp, cfg->sms_otp_length + 1);
free(code);
if (retval == 0 && strlen(otp) == cfg->sms_otp_length) {
DBG(("Correct code from user"));
return PAM_SUCCESS;
} else {
DBG(("INCORRRECT code from user!"));
return PAM_AUTH_ERR;
}
}
static int send_mail(char *dst, char *text, module_config *cfg)
{
#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX 512
#endif /* HOST_NAME_MAX */
char from[HOST_NAME_MAX+1];
pid_t child_pid;
int child_stdin[2];
char * const child_args[7] = { "mail", "-r", from, "-s", cfg->sms_subject, dst, NULL };
gethostname(from, HOST_NAME_MAX + 1);
from[HOST_NAME_MAX] = '\0';
if (pipe(child_stdin) < 0) {
DBG((Unable to call 'pipe'))
return -1;
}
child_pid = fork();
switch (child_pid) {
case -1:
DBG(("Fork failure"));
return -1;
case 0:
// Child process
if (dup2(child_stdin[0], STDIN_FILENO) < 0)
exit(-1);
close(child_stdin[1]);
execv("/bin/mail", child_args);
// Should not be taken
exit(-1);
default:
// Parent process
close(child_stdin[0]);
{
ssize_t written;
size_t pos = 0;
size_t text_len = strlen(text);
while ((written = write(child_stdin[1], text + pos, text_len - pos)) > 0 ) {
pos += (size_t) written; // This is > 0 due to the check on the previous line
if (text_len >= pos)
break;
}
}
close(child_stdin[1]);
{
int ret;
if (waitpid(child_pid, &ret, 0) == child_pid)
if (WIFEXITED(ret))
return WEXITSTATUS(ret);
}
return -1;
}
}
static int rnd_numb(char *otp, int length)
{
int i;
srand((unsigned int) time(NULL));
for (i = 0; i < length; ++i) {
otp[i] = (char) ((int) (10 * (rand() / (RAND_MAX + 1.0))) + (int) '0');
}
otp[i] = 0;
return OK;
}