main.c
3.03 KB
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
#include <grp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "imap.h"
#include "imap_commands.h"
#include "command.h"
static void init(void);
static int mainloop(void);
static void cleanup(void);
static void greeting(void);
static Command* parse_command(char *string);
#define action(cmd) cmd->info->action ? cmd->info->action(cmd) : BAD;
#define free_command(cmd) free(cmd);
/* state the server is in */
STATES state = NON_AUTH;
FILE *input = NULL;
FILE *output = NULL;
/* output strings, maps STATUS to strings */
static char *status_word[3] = {"BAD", "NO", "OK"};
static char *status_code[3] = {"invalid", "failed", "completed"};
static imap4_cmd_t COMMANDS[] = {
{ "capability", imap_capability, , , },
{ "noop" , imap_noop, , , },
{ "logout" , imap_logout, , , },
{ "login" , imap_login, , , },
{ "", , NULL, , , }
};
int main(int argc, char *argv[]) {
input = stdin;
output = stdout;
init();
greeting();
mainloop();
cleanup();
fclose(input);
fclose(output);
return 0;
}
static void init(void) {
struct group *gr;
gr = getgrnam("mail");
if( gr == NULL )
exit(-1);
if( setgid(gr->gr_gid) == -1 )
exit(-1);
openlog("GNU IMAP4", LOG_PID, LOG_MAIL);
if( output == stdout )
setvbuf(output, NULL, _IOLBF, 0);
}
static void cleanup(void) {
closelog();
}
static void greeting(void) {
fprintf(output, "* OK IMAP4rev1 Service Ready\r\n");
}
static int mainloop(void) {
char *client_string = NULL;
size_t len = 0;
Command *command;
STATUS status;
while( getline(&client_string, &len, input) != -1 ) {
command = parse_command(client_string);
if(command) {
status = action(command);
fprintf(output, "%s %s %s %s\r\n", command->tag, status_word[status],
command->cmd, status_code[status]);
free_command(command);
}
else {
/* FIXME: properly handle this */
fprintf(output, "");
}
if( state == LOGOUT ) /* all done, let's go */
break;
}
free(client_string);
return 1;
}
imap4_action_t check_command(const char *command) {
for(i = 0; COMMANDS[i]; ++i) {
if(strcasecmp(command, COMMANDS[i].cmd) == 0)
return COMMANDS[i];
}
return NULL;
}
Command* parse_command(char *string) {
size_t len, tmp;
Command *command;
if( string == NULL )
return NULL;
command = malloc(sizeof(Command));
memset(command, '\0', sizeof(Command));
len = strcspn(string, " ");
if( len > 0 ) {
tmp = strcspn(string, "\r\n");
string[tmp] = '\0';
strncpy(command->tag, string, len > 15 ? 15 : len);
} else {
strcpy(command->tag, "gibberish");
return command;
}
tmp = len;
++tmp; /* skip space char */
len = strcspn(string + tmp, " ");
if( len > 0 )
strncpy(command->cmd, string + tmp, len > 15 ? 15 : len);
else {
strcpy(command->cmd, "gibberish");
return command;
}
/* args is the rest of the string */
command->args = string + len + tmp + 1;
if( command->args[0] == '\0' ) /* no args */
command->args = NULL;
command->info = check_command(command->cmd);
return command;
}