summaryrefslogtreecommitdiff
path: root/femtomail.c
blob: 4442b93d805819ef39394ba7a2e78a2f87b0b684 (plain)
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
/**
 * femtomail - Minimal sendmail replacement for forwarding mail to a single
 * Maildir box. Note: this program does not try to implement sendmail.
 *
 * Installation commands:
 * $ cc -DUSERNAME=\"$USER\" femtomail.c -o femtomail
 * (Optional override: -DMAILBOX_PATH=\".Maildir/inbox\")
 * # install -m 755 femtomail /usr/bin/sendmail
 * # setcap cap_setuid,cap_setgid=ep /usr/bin/sendmail
 *
 * Copyright (C) 2013 Peter Wu <lekensteyn@gmail.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 3 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, see <http://www.gnu.org/licenses/>.
 */

#define _GNU_SOURCE /* for setresgid/setresuid */
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#include <sys/types.h>
#include <pwd.h>
#include <sys/prctl.h>

#ifndef USERNAME
#    error Please define the user to deliver mail to with USERNAME
#endif

/* Maildir directory relative to home dir of USERNAME (see above) */
#ifndef MAILBOX_PATH
#    define MAILBOX_PATH ".local/share/local-mail/inbox"
#endif

/* change user/group context to username and fill in maildir path */
int
init_user(const char *username, char *maildir, size_t maildir_len) {
    struct passwd *pwd;
    uid_t uid;
    gid_t gid;

    if ((pwd = getpwnam(username)) == NULL) {
        fprintf(stderr, "Unknown user %s, cannot locate Maildir.\n", username);
        return 1;
    }

    uid = pwd->pw_uid;
    gid = pwd->pw_gid;

    if (setresgid(gid, gid, gid) || setresuid(uid, uid, uid)) {
        perror("Failed to change uid/gid");
        return 1;
    }

    snprintf(maildir, maildir_len, "%s/" MAILBOX_PATH "/new", pwd->pw_dir);
    return 0;
}

/* get a random number between 0 and 999 (inclusive) */
unsigned
get_random(void) {
    FILE *fp;
    int rnd;

    fp = fopen("/dev/urandom", "r");
    if (fp != NULL) {
        unsigned char buf[2];

        fread(buf, 2, 1, fp);
        rnd = (buf[0] << 8) | buf[1];

        fclose(fp);
    } else {
        rnd = rand();
    }

    return rnd % 1000;
}

/* get the current hostname, sanitizing '/' and ':' chars  */
void
get_hostname(char *hostname, size_t hostname_len) {
    if (gethostname(hostname, hostname_len) >= 0) {
        char *p;
        /* sanitized per http://cr.yp.to/proto/maildir.html */
        for (p = hostname; *p; p++) {
            if (*p == '/') *p = '\057';
            if (*p == ':') *p = '\072';
        }
    } else {
        strncpy(hostname, "unknown", hostname_len);
    }
}

/* generate a filename suitable for Maildir consisting of a timestamp, a random
 * middle part and a hostname (per http://cr.yp.to/proto/maildir.html) */
void
make_name(char *name, size_t name_len, time_t tm) {
    char hostname[128];

    get_hostname(hostname, sizeof(hostname));
    snprintf(name, name_len, "%llu.R%u.%s",
        (unsigned long long) tm, get_random(), hostname);
}

/* write Received header to file */
void
write_headers(FILE *mail_fp, const char *address, time_t tm) {
    char timestr[200];
    struct tm *tmp;

    tmp = localtime(&tm);
    if (tmp == NULL) {
        perror("localtime");
        return;
    }

    if (!strftime(timestr, sizeof(timestr), "%a, %d %b %Y %T %z", tmp)) {
        fprintf(stderr, "strftime() failed!\n");
        return;
    }

    fprintf(mail_fp, "Received: for %s with local (femtomail); %s\n", address, timestr);
}

/* read text from stdin and write to file */
int
read_and_write(FILE *mail_fp) {
    size_t r;
    char buf[4096];

    while ((r = fread(buf, 1, sizeof(buf), stdin)) > 0) {
        if (fwrite(buf, 1, r, mail_fp) != r) {
            /* disk full? */
            return 1;
        }
    }

    return 0;
}

/* validate the recipient address as in `sendmail [address]` */
bool
valid_address(const char *address) {
    char c;

    while ((c = *address++) != 0) {
        /* reject unprintable chars, including newlines */
        if (!isgraph(c)) {
            return false;
        }
    }

    return true;
}

int
main(int argc, char **argv) {
    char maildir[256];
    char *address = NULL, file_path[256];
    time_t tm;
    int i, mail_fd, ret;
    FILE *mail_fp;

    for (i = 1; i < argc; i++) {
        if (argv[i][0] == '-') {
            /* ignore arg, hopefully next arg does not contain a value */
            continue;
        }

        if (!valid_address(argv[i])) {
            fprintf(stderr, "Illegal characters in address!\n");
            return (EXIT_FAILURE);
        }

        address = argv[i];
        break;
    }

    if (!address) {
        fprintf(stderr, "Missing recipient address.\n");
        return (EXIT_FAILURE);
    }

    if (init_user((USERNAME), maildir, sizeof(maildir))) {
        return (EXIT_FAILURE);
    }

    if (chdir(maildir)) {
        fprintf(stderr, "chdir(%s): %s\n", maildir, strerror(errno));
        return (EXIT_FAILURE);
    }

    tm = time(NULL);
    make_name(file_path, sizeof(file_path), tm);

    /* ensure that no file gets overwritten */
    mail_fd = open(file_path, O_WRONLY | O_CREAT | O_EXCL, 0644);
    if (mail_fd < 0) {
        perror("open");
        return (EXIT_FAILURE);
    }

    mail_fp = fdopen(mail_fd, "w");

    write_headers(mail_fp, address, tm);
    ret = read_and_write(mail_fp);
    if (ret) {
            perror("fwrite");
    }

    fclose(mail_fp);
    return ret;
}

/* vim: set sw=4 ts=4 et: */