123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 |
- #include "libbb.h"
- #include "mail.h"
- #define MAX_HEADERS 256
- static void send_r_n(const char *s)
- {
- if (verbose)
- bb_error_msg("send:'%s'", s);
- printf("%s\r\n", s);
- }
- static int smtp_checkp(const char *fmt, const char *param, int code)
- {
- char *answer;
- char *msg = send_mail_command(fmt, param);
-
-
-
-
-
-
- while ((answer = xmalloc_fgetline(stdin)) != NULL) {
- if (verbose)
- bb_error_msg("recv:'%.*s'", (int)(strchrnul(answer, '\r') - answer), answer);
- if (strlen(answer) <= 3 || '-' != answer[3])
- break;
- free(answer);
- }
- if (answer) {
- int n = atoi(answer);
- if (timeout)
- alarm(0);
- free(answer);
- if (-1 == code || n == code) {
- free(msg);
- return n;
- }
- }
- bb_error_msg_and_die("%s failed", msg);
- }
- static int smtp_check(const char *fmt, int code)
- {
- return smtp_checkp(fmt, NULL, code);
- }
- static char *sane_address(char *str)
- {
- char *s;
- trim(str);
- s = str;
- while (*s) {
-
- if (!isalnum(*s) && !strchr("=+_-.@", *s)) {
- bb_error_msg("bad address '%s'", str);
-
- str[0] = '\0';
- return str;
- }
- s++;
- }
- return str;
- }
- static char *angle_address(char *str)
- {
- char *s, *e;
- e = trim(str);
- if (e != str && e[-1] == '>') {
- s = strrchr(str, '<');
- if (s) {
- *e = '\0';
- str = s + 1;
- }
- }
- return sane_address(str);
- }
- static void rcptto(const char *s)
- {
- if (!*s)
- return;
-
- if (250 != smtp_checkp("RCPT TO:<%s>", s, -1))
- bb_error_msg("Bad recipient: <%s>", s);
- }
- static void rcptto_list(const char *list)
- {
- char *free_me = xstrdup(list);
- char *str = free_me;
- char *s = free_me;
- char prev = 0;
- int in_quote = 0;
- while (*s) {
- char ch = *s++;
- if (ch == '"' && prev != '\\') {
- in_quote = !in_quote;
- } else if (!in_quote && ch == ',') {
- s[-1] = '\0';
- rcptto(angle_address(str));
- str = s;
- }
- prev = ch;
- }
- if (prev != ',')
- rcptto(angle_address(str));
- free(free_me);
- }
- int sendmail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int sendmail_main(int argc UNUSED_PARAM, char **argv)
- {
- char *opt_connect;
- char *opt_from = NULL;
- char *s;
- llist_t *list = NULL;
- char *host = sane_address(safe_gethostname());
- unsigned nheaders = 0;
- int code;
- enum {
- HDR_OTHER = 0,
- HDR_TOCC,
- HDR_BCC,
- } last_hdr = 0;
- int check_hdr;
- int has_to = 0;
- enum {
-
- OPT_t = 1 << 0,
- OPT_f = 1 << 1,
- OPT_o = 1 << 2,
- OPT_i = 1 << 3,
-
- OPT_w = 1 << 4,
- OPT_H = 1 << 5,
- OPT_S = 1 << 6,
- OPT_a = 1 << 7,
- OPT_v = 1 << 8,
- };
-
- INIT_G();
-
- opt_connect = getenv("SMTPHOST");
- if (!opt_connect)
- opt_connect = (char *)"127.0.0.1";
-
- xdup2(STDIN_FILENO, 3);
- G.fp0 = xfdopen_for_read(3);
-
-
-
-
- opts = getopt32(argv, "^"
- "tf:o:iw:+H:S:a:*:v"
- "\0"
-
- "vv:H--S:S--H",
- &opt_from, NULL,
- &timeout, &opt_connect, &opt_connect, &list, &verbose
- );
-
- argv += optind;
-
- if ((opts & OPT_a) && !list)
- bb_show_usage();
- while (list) {
- char *a = (char *) llist_pop(&list);
- if ('u' == a[0])
- G.user = xstrdup(a+1);
- if ('p' == a[0])
- G.pass = xstrdup(a+1);
-
-
-
- }
-
-
-
-
- if (opts & OPT_H) {
- const char *delay;
- const char *args[] = { "sh", "-c", opt_connect, NULL };
-
- launch_helper(args);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- delay = getenv("SMTP_ANTISPAM_DELAY");
- if (delay)
- sleep(atoi(delay));
- code = smtp_check("NOOP", -1);
- if (code == 220)
-
-
- smtp_check(NULL, 250);
- else
- if (code != 250)
- bb_error_msg_and_die("SMTP init failed");
- } else {
-
- int fd;
- fd = create_and_connect_stream_or_die(opt_connect, 25);
-
- xmove_fd(fd, STDIN_FILENO);
- xdup2(STDIN_FILENO, STDOUT_FILENO);
-
- smtp_check(NULL, 220);
- }
-
- if (250 != smtp_checkp("EHLO %s", host, -1))
- smtp_checkp("HELO %s", host, 250);
-
- if (opts & OPT_a) {
- smtp_check("AUTH LOGIN", 334);
-
- if (!G.user || !G.pass)
- get_cred_or_die(4);
- encode_base64(NULL, G.user, NULL);
- smtp_check("", 334);
- encode_base64(NULL, G.pass, NULL);
- smtp_check("", 235);
- }
-
-
-
-
-
-
-
-
-
-
-
- if (!opt_from) {
- opt_from = xasprintf("%s@%s",
- G.user ? G.user : xuid2uname(getuid()),
- xgethostbyname(host)->h_name);
- }
- free(host);
- smtp_checkp("MAIL FROM:<%s>", opt_from, 250);
-
-
-
-
- code = 0;
- while ((s = xmalloc_fgetline(G.fp0)) != NULL) {
- dump:
-
- if (code) {
-
-
-
-
- if ('.' == s[0] )
- bb_putchar('.');
-
- send_r_n(s);
- free(s);
- continue;
- }
-
-
- check_hdr = (0 == strncasecmp("To:", s, 3));
- has_to |= check_hdr;
- if (opts & OPT_t) {
- if (check_hdr || 0 == strncasecmp("Bcc:" + 1, s, 3)) {
- rcptto_list(s+3);
- last_hdr = HDR_TOCC;
- goto addheader;
- }
-
- if (0 == strncasecmp("Bcc:", s, 4)) {
- rcptto_list(s+4);
- free(s);
- last_hdr = HDR_BCC;
- continue;
- }
- }
- check_hdr = (list && isspace(s[0]));
- if (strchr(s, ':') || check_hdr) {
-
-
-
-
- if (check_hdr && last_hdr != HDR_OTHER) {
- rcptto_list(s+1);
- if (last_hdr == HDR_BCC)
- continue;
-
- } else {
- last_hdr = HDR_OTHER;
- }
- addheader:
-
- if (MAX_HEADERS && ++nheaders >= MAX_HEADERS)
- goto bail;
- llist_add_to_end(&list, s);
- } else {
-
-
- reenter:
-
- check_hdr = 1;
- while (*argv) {
- char *t = sane_address(*argv);
- rcptto(t);
-
-
- if (!has_to) {
- const char *hdr;
- if (check_hdr && argv[1])
- hdr = "To: %s,";
- else if (check_hdr)
- hdr = "To: %s";
- else if (argv[1])
- hdr = "To: %s," + 3;
- else
- hdr = "To: %s" + 3;
- llist_add_to_end(&list,
- xasprintf(hdr, t));
- check_hdr = 0;
- }
- argv++;
- }
-
-
-
- if (354 != smtp_check("DATA", -1))
- goto bail;
-
- while (list) {
- send_r_n((char *) llist_pop(&list));
- }
-
- code++;
-
-
- if (!s) {
- send_r_n("");
- break;
- }
-
-
- goto dump;
- }
- }
-
-
- if (!code)
- goto reenter;
-
- smtp_check(".", 250);
- bail:
-
- smtp_check("QUIT", 221);
-
- if (ENABLE_FEATURE_CLEAN_UP)
- fclose(G.fp0);
- return EXIT_SUCCESS;
- }
|