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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
|
/*
* Send SMS through email
*
* Copyright (C) 2010 by Multi-Tech Systems
*
* Author: James Maki <jmaki@multitech.com>
*
* 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 2 of the License, or
* (at your option) 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/limits.h>
#include <string.h>
#include <ctype.h>
#include "global.h"
#if HAVE_LIBESMTP
#include <openssl/ssl.h>
#include <auth-client.h>
#include <libesmtp.h>
#endif
#include "utils.h"
#include "cmd_options.h"
#include "sms_utils.h"
#include "atcmd.h"
#include "phonebook.h"
struct send_options {
char *file;
char *subject;
};
#if HAVE_LIBESMTP
static FILE *open_message_file(struct send_options *options)
{
FILE *fp;
int tmp;
if (options->file) {
fp = fopen(options->file, "r");
if (!fp) {
log_error("failed to open %s for reading", options->file);
return NULL;
}
} else if (Global.core.interactive && Global.core.edit_file && Global.core.editor) {
tmp = systemf("%s %s", Global.core.editor, Global.core.edit_file);
if (tmp < 0 || !WIFEXITED(tmp) || WEXITSTATUS(tmp)) {
log_error("edit failed");
return NULL;
}
char *path = shell_path_expand(Global.core.edit_file);
if (!path) {
return NULL;
}
fp = fopen(path, "r");
if (!fp) {
log_error("failed to open %s for reading", path);
free(path);
return NULL;
}
free(path);
} else {
fp = stdin;
}
return fp;
}
char *fgets_noecho(char *buf, size_t len, FILE *stream)
{
int err;
struct termios params;
char *ret = NULL;
tcflag_t c_lflag;
err = tcgetattr(fileno(stream), ¶ms);
if (err < 0) {
log_error("tcgetattr failed: %m");
return NULL;
}
c_lflag = params.c_lflag;
params.c_lflag &= ~ECHO;
params.c_lflag |= ECHONL;
err = tcsetattr(fileno(stream), TCSAFLUSH, ¶ms);
if (err < 0) {
log_error("tcsetattr failed: %m");
return NULL;
}
ret = fgets(buf, len, stream);
params.c_lflag = c_lflag;
err = tcsetattr(fileno(stream), TCSAFLUSH, ¶ms);
if (err < 0) {
log_error("tcsetattr failed: %m");
return NULL;
}
return ret;
}
struct auth_callback_data {
char user[128];
char passwd[128];
char realm[128];
};
int auth_callback(auth_client_request_t request, char **result, int fields, void *arg)
{
struct auth_callback_data *data = (struct auth_callback_data *) arg;
char *cp;
int i;
for (i = 0; i < fields; i++) {
if (request[i].flags & AUTH_PASS) {
if (!Global.smtp.passwd) {
printf("Password: ");
fflush(stdout);
cp = fgets_noecho(data->passwd,
sizeof(data->passwd), stdin);
if (!cp) {
*data->passwd = '\0';
}
cp = strrchr(data->passwd, '\n');
if (cp) {
*cp = '\0';
}
result[i] = data->passwd;
} else {
result[i] = Global.smtp.passwd;
}
} else if (request[i].flags & AUTH_USER) {
if (!Global.smtp.user) {
printf("Username: ");
fflush(stdout);
cp = fgets(data->user, sizeof(data->user), stdin);
if (!cp) {
*data->user = '\0';
}
cp = strrchr(data->user, '\n');
if (cp) {
*cp = '\0';
}
result[i] = data->user;
} else {
result[i] = Global.smtp.user;
}
} else if (request[i].flags & AUTH_REALM) {
printf("Realm: ");
fflush(stdout);
cp = fgets(data->realm, sizeof(data->realm), stdin);
if (!cp) {
*data->realm = '\0';
}
cp = strrchr(data->realm, '\n');
if (cp) {
*cp = '\0';
}
result[i] = data->realm;
} else {
result[i] = NULL;
}
}
return 1;
}
int tls_callback(char *buf, int len, int flag, void *arg)
{
char *cp;
printf("Certificate Password: ");
fflush(stdout);
cp = fgets_noecho(buf, len, stdin);
if (!cp) {
*buf = '\0';
}
cp = strrchr(buf, '\n');
if (cp) {
*cp = '\0';
}
return strlen(buf);
}
void event_callback(smtp_session_t session, int event, void *args, ...)
{
va_list ap;
long long_arg;
int *ok;
va_start(ap, args);
switch (event) {
case SMTP_EV_CONNECT:
case SMTP_EV_MAILSTATUS:
case SMTP_EV_RCPTSTATUS:
case SMTP_EV_MESSAGEDATA:
case SMTP_EV_MESSAGESENT:
case SMTP_EV_DISCONNECT:
break;
case SMTP_EV_WEAK_CIPHER:
long_arg = va_arg(ap, long);
log_debug("SMTP_EV_WEAK_CIPHER: bits: %ld", long_arg);
ok = va_arg(ap, int *);
*ok = 1;
break;
case SMTP_EV_STARTTLS_OK:
log_debug("SMTP_EV_STARTTLS_OK");
break;
case SMTP_EV_INVALID_PEER_CERTIFICATE:
long_arg = va_arg(ap, long);
log_debug("SMTP_EV_INVALID_PEER_CERTIFICATE: error: %ld", long_arg);
ok = va_arg(ap, int *);
*ok = 1;
break;
case SMTP_EV_NO_PEER_CERTIFICATE:
log_debug("SMTP_EV_NO_PEER_CERTIFICATE");
ok = va_arg(ap, int *);
*ok = 1;
break;
case SMTP_EV_WRONG_PEER_CERTIFICATE:
log_debug("SMTP_EV_WRONG_PEER_CERTIFICATE");
ok = va_arg(ap, int *);
*ok = 1;
break;
case SMTP_EV_NO_CLIENT_CERTIFICATE:
log_debug("SMTP_EV_NO_CLIENT_CERTIFICATE");
ok = va_arg(ap, int *);
*ok = 1;
break;
default:
log_debug("unknown event %d", event);
}
va_end(ap);
}
static void print_recipient_status(smtp_recipient_t recipient,
const char *mailbox, void *arg)
{
const smtp_status_t *status;
status = smtp_recipient_status(recipient);
printf(" - addr: %s\n", mailbox);
printf(" code: %d\n", status->code);
printf(" message: %s\n", status->text);
}
static int do_send_email(struct send_options *options, int argc, char **argv)
{
char buf[1024];
struct sigaction sa;
struct auth_callback_data auth_arg;
FILE *fp = NULL;
int ret = false;
int tmp;
int i;
smtp_session_t session = NULL;
smtp_message_t message;
auth_context_t authctx = NULL;
const smtp_status_t *status;
auth_client_init();
if (argc < 1) {
log_error("at least one recipient addr is required");
goto done;
}
sa.sa_handler = SIG_IGN;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGPIPE, &sa, NULL);
if (!Global.smtp.server) {
log_error("smtp config missing");
goto done;
}
session = smtp_create_session();
message = smtp_add_message(session);
if (Global.smtp.encryption && !strcmp(Global.smtp.encryption, "tls")) {
smtp_starttls_enable(session, Starttls_REQUIRED);
}
if (strchr(Global.smtp.server, ':') || !Global.smtp.port) {
snprintf(buf, sizeof(buf), "%s", Global.smtp.server);
} else {
snprintf(buf, sizeof(buf), "%s:%d",
Global.smtp.server, Global.smtp.port);
}
smtp_set_server(session, buf);
authctx = auth_create_context();
if (!authctx) {
log_error("auth_create_context failed");
goto done;
}
auth_set_mechanism_flags(authctx, AUTH_PLUGIN_PLAIN, 0);
auth_set_interact_cb(authctx, auth_callback, &auth_arg);
smtp_starttls_set_password_cb(tls_callback, NULL);
smtp_set_eventcb(session, event_callback, NULL);
smtp_auth_set_context(session, authctx);
if (Global.user.email) {
smtp_set_reverse_path(message, Global.user.email);
smtp_set_header(message, "From", Global.user.name,
Global.user.email);
}
if (options->subject) {
smtp_set_header(message, "Subject", options->subject);
}
fp = open_message_file(options);
if (!fp) {
log_error("opening sms message file failed");
goto done;
}
tmp = smtp_set_message_fp(message, fp);
int nr_rcpts = 0;
for (i = 0; i < argc; i++) {
if (strchr(argv[i], '@')) {
smtp_add_recipient(message, argv[i]);
nr_rcpts++;
} else if (Global.send_email.domain) {
snprintf(buf, sizeof(buf), "%s@%s",
argv[i], Global.send_email.domain);
smtp_add_recipient(message, buf);
nr_rcpts++;
} else {
log_notice("skipping recipient %s", argv[i]);
}
}
if (!nr_rcpts) {
log_error("email contains no recipients");
goto done;
}
tmp = smtp_start_session(session);
if (!tmp) {
smtp_strerror(smtp_errno(), buf, sizeof(buf));
log_error("smtp_start_session: %s", buf);
goto done;
}
status = smtp_message_transfer_status(message);
printf("transfer-status:\n");
printf(" - code: %d\n", status->code);
printf(" message: %s\n", status->text ?: "");
printf("recipient-status:\n");
smtp_enumerate_recipients(message, print_recipient_status, NULL);
if (status->code == 250 && Global.core.interactive && Global.core.edit_file) {
systemf("rm -f %s", Global.core.edit_file);
}
ret = true;
done:
if (authctx) {
auth_destroy_context(authctx);
}
if (session) {
smtp_destroy_session(session);
}
if (fp) {
fclose(fp);
}
auth_client_exit();
return ret;
}
#else
static int do_send_email(struct send_options *options, int argc, char **argv)
{
return false;
}
#endif
static char *short_options = __CMD_OPT_FILE;
static struct option long_options[] = {
{"file", 1, NULL, CMD_OPT_FILE},
{"subject", 1, NULL, CMD_OPT_SUBJECT},
{NULL, 0, NULL, 0},
};
void sms_send_email_help(FILE *out) {
fprintf(out, "usage: send-email [ OPTIONS ... ] <number>\n");
fprintf(out, "where OPTIONS := { \n");
fprintf(out, " -f, --file <input-file> |\n");
fprintf(out, " --subject <email-subject>\n");
fprintf(out, " }\n");
fprintf(out, "\n");
}
int sms_send_email(int argc, char **argv)
{
int i;
int option_index;
int ret;
struct send_options options;
options.file = NULL;
options.subject = NULL;
while ((i = getopt_long(argc, argv, short_options, long_options, &option_index)) >= 0) {
switch (i) {
case 0:
break;
case CMD_OPT_FILE:
options.file = optarg;
break;
case CMD_OPT_SUBJECT:
options.subject = optarg;
break;
default:
sms_send_email_help(stderr);
return false;
}
}
if (optind >= argc) {
sms_send_email_help(stderr);
return false;
}
argc -= optind;
argv += optind;
ret = do_send_email(&options, argc, argv);
return ret;
}
|