1
2
3
4
5
6
7
8
9#include "libbb.h"
10#include "mail.h"
11
12static int smtp_checkp(const char *fmt, const char *param, int code)
13{
14 char *answer;
15 const char *msg = command(fmt, param);
16
17
18
19
20
21
22 while ((answer = xmalloc_fgetline(stdin)) != NULL)
23 if (strlen(answer) <= 3 || '-' != answer[3])
24 break;
25 if (answer) {
26 int n = atoi(answer);
27 if (timeout)
28 alarm(0);
29 free(answer);
30 if (-1 == code || n == code)
31 return n;
32 }
33 bb_error_msg_and_die("%s failed", msg);
34}
35
36static int smtp_check(const char *fmt, int code)
37{
38 return smtp_checkp(fmt, NULL, code);
39}
40
41
42static char *sane_address(char *str)
43{
44 char *s = str;
45 char *p = s;
46 while (*s) {
47 if (isalnum(*s) || '_' == *s || '-' == *s || '.' == *s || '@' == *s) {
48 *p++ = *s;
49 }
50 s++;
51 }
52 *p = '\0';
53 return str;
54}
55
56static void rcptto(const char *s)
57{
58 smtp_checkp("RCPT TO:<%s>", s, 250);
59}
60
61int sendmail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
62int sendmail_main(int argc UNUSED_PARAM, char **argv)
63{
64 char *opt_connect = opt_connect;
65 char *opt_from;
66 char *s;
67 llist_t *list = NULL;
68 char *domain = sane_address(safe_getdomainname());
69 int code;
70
71 enum {
72
73 OPT_t = 1 << 0,
74 OPT_f = 1 << 1,
75 OPT_o = 1 << 2,
76
77 OPT_w = 1 << 3,
78 OPT_H = 1 << 4,
79 OPT_S = 1 << 5,
80 OPT_a = 1 << 6,
81 };
82
83
84 INIT_G();
85
86
87 xdup2(STDIN_FILENO, 3);
88 G.fp0 = fdopen(3, "r");
89
90
91
92 opt_complementary = "f:w+:H--S:S--H:a::";
93
94
95
96 opts = getopt32(argv, "tf:o:w:H:S:a::", &opt_from, NULL, &timeout, &opt_connect, &opt_connect, &list);
97
98 argv += optind;
99
100
101 if ((opts & OPT_a) && !list)
102 bb_show_usage();
103 while (list) {
104 char *a = (char *) llist_pop(&list);
105 if ('u' == a[0])
106 G.user = xstrdup(a+1);
107 if ('p' == a[0])
108 G.pass = xstrdup(a+1);
109
110
111
112 }
113
114
115
116
117
118
119 if (opts & OPT_H) {
120 const char *args[] = { "sh", "-c", opt_connect, NULL };
121
122 launch_helper(args);
123
124 } else {
125 int fd;
126
127
128 if (!(opts & OPT_S)) {
129 opt_connect = getenv("SMTPHOST");
130 if (!opt_connect)
131 opt_connect = (char *)"127.0.0.1";
132 }
133
134 fd = create_and_connect_stream_or_die(opt_connect, 25);
135
136 xmove_fd(fd, STDIN_FILENO);
137 xdup2(STDIN_FILENO, STDOUT_FILENO);
138 }
139
140
141
142
143
144 code = smtp_check("NOOP", -1);
145
146 if (220 == code)
147 smtp_check(NULL, 250);
148 else if (250 != code)
149 bb_error_msg_and_die("INIT failed");
150
151
152 if (250 != smtp_checkp("EHLO %s", domain, -1)) {
153 smtp_checkp("HELO %s", domain, 250);
154 }
155 if (ENABLE_FEATURE_CLEAN_UP)
156 free(domain);
157
158
159 if (opts & OPT_a) {
160 smtp_check("AUTH LOGIN", 334);
161
162 if (!G.user || !G.pass)
163 get_cred_or_die(4);
164 encode_base64(NULL, G.user, NULL);
165 smtp_check("", 334);
166 encode_base64(NULL, G.pass, NULL);
167 smtp_check("", 235);
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190 smtp_checkp("MAIL FROM:<%s>", opt_from, 250);
191
192
193
194
195
196
197 code = 0;
198 while ((s = xmalloc_fgetline(G.fp0)) != NULL) {
199
200 if (code) {
201
202
203
204
205 if ('.' == s[0] )
206 printf(".");
207
208 printf("%s\r\n", s);
209 free(s);
210 continue;
211 }
212
213
214
215 if (0 == strncasecmp("To: ", s, 4) || 0 == strncasecmp("Bcc: " + 1, s, 4)) {
216 rcptto(sane_address(s+4));
217
218 llist_add_to_end(&list, s);
219
220 } else if (0 == strncasecmp("Bcc: ", s, 5)) {
221 rcptto(sane_address(s+5));
222 free(s);
223
224
225 } else if (s[0]) {
226
227 llist_add_to_end(&list, s);
228
229 } else {
230 free(s);
231
232 while (*argv) {
233 s = sane_address(*argv);
234 rcptto(s);
235 llist_add_to_end(&list, xasprintf("To: %s", s));
236 argv++;
237 }
238
239 smtp_check("DATA", 354);
240
241 while (list) {
242 printf("%s\r\n", (char *) llist_pop(&list));
243 }
244 printf("%s\r\n" + 2);
245
246 code++;
247 }
248 }
249
250
251 smtp_check(".", 250);
252
253 smtp_check("QUIT", 221);
254
255 if (ENABLE_FEATURE_CLEAN_UP)
256 fclose(G.fp0);
257
258 return EXIT_SUCCESS;
259}
260