-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaxmail.c
300 lines (259 loc) · 5.96 KB
/
axmail.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
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
/* axmail.c - The command parser and main function */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <syslog.h>
#include <pwd.h>
#include <crypt.h>
#include <error.h>
#include <grp.h>
#include "config.h"
#include "axmail.h"
#include "utils.h"
#include "adduser.h"
#include "quit.h"
#include "mbox.h"
/* Parse c-style escapes (neat to have!) */
// int setgroups(void);
static char *parse_string(char *str)
{
char *cp = str;
unsigned long num;
while (*str != '\0' && *str != '\"') {
if (*str == '\\') {
str++;
switch (*str++) {
case 'n':
*cp++ = '\n';
break;
case 't':
*cp++ = '\t';
break;
case 'v':
*cp++ = '\v';
break;
case 'b':
*cp++ = '\b';
break;
case 'r':
*cp++ = '\r';
break;
case 'f':
*cp++ = '\f';
break;
case 'a':
*cp++ = '\007';
break;
case '\\':
*cp++ = '\\';
break;
case '\"':
*cp++ = '\"';
break;
case 'x':
num = strtoul(--str, &str, 16);
*cp++ = (char) num;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
num = strtoul(--str, &str, 8);
*cp++ = (char) num;
break;
case '\0':
return NULL;
default:
*cp++ = *(str - 1);
break;
};
} else {
*cp++ = *str++;
}
}
if (*str == '\"')
str++; /* skip final quote */
*cp = '\0'; /* terminate string */
return str;
}
/* Parse command line to argv, honoring quotes and such */
int parse_args(char *argv[],char *cmd)
{
int ct = 0;
int quoted;
while (ct < 31)
{
quoted = 0;
while (*cmd && isspace(*cmd))
cmd++;
if (*cmd == 0)
break;
if (*cmd == '"') {
quoted++;
cmd++;
}
argv[ct++] = cmd;
if (quoted) {
if ((cmd = parse_string(cmd)) == NULL)
return 0;
} else {
while (*cmd && !isspace(*cmd))
cmd++;
}
if (*cmd)
*cmd++ = 0;
}
argv[ct] = NULL;
return ct;
}
/* Find the command from the command table and execute it. */
int cmdparse(struct cmd *cmds, char *cmdline)
{
struct cmd *cmdp;
int argc;
char *argv[32];
if ((argc = parse_args(argv, cmdline)) == 0 || *argv[0] == '#')
return 0;
strlwr(argv[0]);
for (cmdp = cmds; cmdp->function != NULL; cmdp++)
if (strncasecmp(cmdp->name, argv[0], strlen(argv[0])) == 0)
break;
if (cmdp->function == NULL)
return -1;
return (*cmdp->function)(argc, argv);
}
/* Signal handlers (which don't do all the things they SHOULD do) */
static void alarm_handler(int sig)
{
printf("\nTimeout! Closing...\n");
exit(1);
}
static void term_handler(int sig)
{
printf("System going down! Closing...\n");
exit(1);
}
/* Initialisation for the user */
int init_user(char *call)
{
struct passwd *pw;
char pass[13], salt[3];
char *p;
char axhome[64];
/* Strip SSID */
if (local) {
pw = getpwuid(getuid());
} else {
strcpy(callsign, call);
strcpy(username, callsign);
strlwr(username);
p = strchr(username, '-');
if (p) *p = '\0';
pw = getpwnam(username);
}
if (!pw) {
if (local)
panic("Ouch, you don't seem to be in the password file.\n");
if (autocreate) {
syslog(LOG_NOTICE, "Adding new user %s", username);
if (new_user(username) < 0) {
syslog(LOG_NOTICE, "Could not add new user %s", username);
printf("Sorry, could not add new user.\n");
return -1;
}
printf("You have been added to the user list of %s.\nWelcome.\n", hostname);
pw = getpwnam(username);
} else {
syslog(LOG_NOTICE, "New user %s not accepted", username);
printf("Sorry, new users are not accepted at this time.\n");
return -1;
}
} else {
if (local) {
strcpy(username, pw->pw_name);
strcpy(callsign, username);
}
/* Strip full name from the gecos field... */
if (strchr(pw->pw_gecos, ',') == NULL)
strcpy(fullname, pw->pw_gecos);
else
strcpy(fullname, strtok(pw->pw_gecos, ","));
}
if (!local) {
if ((mail_allowed == 1) && (strcmp(pw->pw_passwd, "*"))) {
printf("Sorry, you are not allowed to use axmail (you have a password set).\n");
return -1;
}
if ((mail_allowed == 2) && (!strcmp(pw->pw_passwd, "*"))) {
printf("Sorry, you are not allowed to use axmail (locked out in password file).\n");
return -1;
}
if (mail_allowed == 3) {
sprintf(axhome, "%s/%s", def_homedir, username);
if (strcmp(pw->pw_dir, axhome)) {
printf("Sorry, you are not allowed to use axmail - bad axhome.\n");
return -1;
}
}
if (identification == 1) {
getstr(pass, 12, "Password: ");
strncpy(salt, pw->pw_passwd, 2);
salt[2] = '\0';
if (strcmp(pw->pw_passwd, (char *)crypt(pass, salt))) {
printf("Login incorrect.\n");
return -1;
}
}
/* code supplied by Jaroslav Skarvada */
if ( (setgroups(0, NULL) == -1) || (setgid(pw->pw_gid) == -1) || (setuid(pw->pw_uid) == -1) )
panic("init_user: Argh, cannot setuid() or setgid() to %i.%i", pw->pw_uid, pw->pw_gid);
}
homedir = strdup(pw->pw_dir);
return 0;
}
int main(int argc, char **argv)
{
char *p;
signal(SIGALRM, alarm_handler);
signal(SIGTERM, term_handler);
openlog("axmail", LOG_PID, LOG_DAEMON);
if (getuid() != 0)
local = 1; /* Hey, we're being executed by a "normal"
* user, with user privileges!
*/
if ((!local) && (argc != 2)) {
printf("axmail: Callsign not found as a parameter.\n");
syslog(LOG_NOTICE, "Callsign not found as a parameter\n");
return 1;
}
if (read_config() == -1) /* Read axmail.conf */
return 1;
printf("%s\n", VERSION);
if (init_user(argv[1]) == -1) /* Get user specific data, create user account */
return 1;
tinit(); /* Filenames etc */
getmail(); /* Scan mailbox */
if ((newm) || (messages))
printf("You have %i messages (%i new).\n", messages, newm);
prompt();
fflush(stdout);
p = malloc(1024);
while (1)
{
fgets(p, 1023, stdin);
if (p == NULL)
quit(0, 0);
if (cmdparse(Mailcmds, p) == -1)
printf("Unknown command. Type ? for a list.\n");
prompt();
fflush(stdout);
}
}