File: userconfig.c

package info (click to toggle)
solid-pop3d 0.15-27
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,716 kB
  • ctags: 1,497
  • sloc: ansic: 6,364; sh: 2,084; makefile: 486
file content (249 lines) | stat: -rw-r--r-- 7,383 bytes parent folder | download | duplicates (5)
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
static const char rcsid[] = "$Id: userconfig.c,v 1.3 2000/04/28 16:58:55 jurekb Exp $";
/*
 *  Solid POP3 - a POP3 server
 *  Copyright (C) 1999  Jerzy Balamut <jurekb@dione.ids.pl>
 *
 *  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 2 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, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "includes.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

#include "maildrop.h"
#include "const.h"
#include "log.h"
#include "fdfgets.h"

extern char maildrop_name[];
extern char maildrop_type[];
extern char usersuffix[];
#ifdef APOP
extern char apop_secret[];
#endif

void parse_user_cfg(char *homedir) {
	struct stat stbuf;
	char tmpmaildrop_name[PATH_MAX];
	char tmpmaildrop_type[MAXMDTYPENAMELENGTH];
#ifdef APOP
	char tmpapop_secret[MAXARGLN + 1];
#endif
	char cfgfile[PATH_MAX];
	char buf[128];
	int fd, md_set = 0;
#ifdef APOP
	int as_set = 0;
#endif
	int linenr;
	ssize_t tmp;
#ifdef APOP
	size_t tmp3;
#endif	
	char *tmp2;
		
	if (stat(homedir, &stbuf) < 0) {
		pop_log_dbg(pop_priority, "user config: can't stat user home directory: %.1024s", homedir);
		pop_error_dbg("user config: stat");
		return;
	};
	if (stbuf.st_mode & 2) {
		pop_log(pop_priority, "user config: user home directory is world writable");
		return;
	};
	if ((strlen(homedir) + strlen(USERCFG) + 2) > sizeof(cfgfile)) {
		pop_log(pop_priority, "user config: home directory name too long");
		return;
	};
	strcpy(cfgfile, homedir);
	strcat(cfgfile, "/");
	strcat(cfgfile, USERCFG);
	if ((fd = open(cfgfile, O_RDONLY)) < 0) {
		pop_log_dbg(pop_priority, "user config: can't open user config file");
		pop_error_dbg("user config: open");
		return;
	};
	if (fstat(fd, &stbuf) < 0) {
		close(fd);
		pop_log(pop_priority, "user config: can't stat user config file");
		pop_error("user config: fstat");
		return;
	};
	if (!S_ISREG(stbuf.st_mode)) {
		close(fd);
		pop_log(pop_priority, "user config: user config file is not regular file");
		return;
	};
	if (stbuf.st_mode & 0177) {
		close(fd);
		pop_log(pop_priority, "user config: user config file has wrong mode");
		return;
	};
	if (stbuf.st_size == 0) {
		close(fd);
		pop_log(pop_priority, "user config: user config file is empty");
		return;
	};
	fd_initfgets();
	linenr = 0;
	while (1) {
		linenr++;
		if ((tmp = fd_fgets(buf, sizeof(buf), fd)) < 0) {
			memset(buf, 0, sizeof(buf));
			close(fd);
			pop_log(pop_priority, "user config: can't read user config file");
			pop_error("user config: read");
			return;
		};
		if (tmp == 0)
			break;
		if ((tmp == sizeof(buf)) && (buf[sizeof(buf) - 1] != '\n')) {
			memset(buf, 0, sizeof(buf));
			close(fd);
			pop_log(pop_priority, "user config: line too long in user config file");
			return;
		};
		if ((tmp == 1) && (buf[0] == '\n'))
			continue;
		if (buf[tmp - 1] == '\n')
			buf[tmp - 1] = 0;
		strtok(buf, " \t");
		if (strcasecmp(buf, "maildrop") == 0) {
			if ((tmp2 = strtok(NULL, " \t")) == NULL) {
				memset(buf, 0, sizeof(buf));
				close(fd);
				pop_log(pop_priority, "user config: argument missing in maildrop declaration in user config file, line: %u", linenr);
				return;
			};
			if (strlen(tmp2) >= sizeof(tmpmaildrop_name)) {
				memset(buf, 0, sizeof(buf));
				close(fd);
				pop_log(pop_priority, "user config: maildrop file name in user config file is too long");
				return;
			};
			strcpy(tmpmaildrop_name, tmp2);
			if ((tmp2 = strtok(NULL, " \t")) == NULL) {
				memset(buf, 0, sizeof(buf));
				close(fd);
				pop_log(pop_priority, "user config: argument missing in maildrop declaration in user config file, line: %u", linenr);
				return;
			};
			if (strlen(tmp2) >= sizeof(tmpmaildrop_type)) {
				memset(buf, 0, sizeof(buf));
				close(fd);
				pop_log(pop_priority, "user config: maildrop type in user config file is too long");
				return;
			};
#ifdef MDMAILBOX
			if (strcasecmp(tmp2, "mailbox") != 0)
#endif
#ifdef MDMAILDIR
			if (strcasecmp(tmp2, "maildir") != 0)
#endif					    
			{
				memset(buf, 0, sizeof(buf));
				close(fd);
				pop_log(pop_priority, "user config: no such maildrop type: %.40s", tmp2);
				return;
			};
			strcpy(tmpmaildrop_type, tmp2);
			if ((((tmp2 = strtok(NULL, " \t")) == NULL) && (usersuffix[0] == 0)) ||
			 ((tmp2 != NULL) && (usersuffix[0] != 0) && (strcasecmp(tmp2,usersuffix) == 0))) {
				if (md_set == 1) {
					memset(buf, 0, sizeof(buf));
					close(fd);
					pop_log(pop_priority, "user config: maildrop already set in user config file");
					return;
				};

				md_set = 1;
				strcpy(maildrop_name, tmpmaildrop_name);
				strcpy(maildrop_type, tmpmaildrop_type);
			};

			continue;
		};
#ifdef APOP
		if (strcasecmp(buf, "APOPsecret") == 0) {
			if ((tmp2 = strtok(NULL, " \t")) == NULL) {
				memset(buf, 0, sizeof(buf));
				close(fd);
				pop_log(pop_priority, "user config: argument missing, line: %u", linenr);
				return;
			};
			tmp3 = strlen(tmp2);
			if (tmp3 & 1) {
				memset(buf, 0, sizeof(buf));
				close(fd);
				pop_log(pop_priority, "user config: length of encrypted APOP secret should be even number");
				return;
			};
			if ((tmp3/2) >= sizeof(tmpapop_secret)) {
				memset(buf, 0, sizeof(buf));
				close(fd);
				pop_log(pop_priority, "user config: encrypted APOP secret is too long");				
				return;
			};
			for (tmp3 = 0; tmp3 < strlen(tmp2); tmp3++)
				if (((tmp2[tmp3] < 'a') || (tmp2[tmp3] > 'z')) &&
				    (!isdigit(tmp2[tmp3]))) {
					    memset(buf, 0, sizeof(buf));
					    close(fd);
					    pop_log(pop_priority, "user config: wrong character in encrypted APOP secret");					    
					    return;
				    };
			memset(tmpapop_secret, 0, sizeof(tmpapop_secret));
			for (tmp3 = 0; tmp3 < (strlen(tmp2) / 2); tmp3++) {
				if (isdigit(tmp2[tmp3 * 2]))
					tmpapop_secret[tmp3] = (tmp2[tmp3 * 2] - '0') << 4;
				else
					tmpapop_secret[tmp3] = (tmp2[tmp3 * 2] - 'a' + 10) << 4;
				if (isdigit(tmp2[(tmp3 * 2) + 1]))
					tmpapop_secret[tmp3] |= (tmp2[(tmp3 * 2) + 1] - '0');
				else
					tmpapop_secret[tmp3] |= (tmp2[(tmp3 * 2) + 1] - 'a' + 10);
				tmpapop_secret[tmp3] ^= 0xff;
			};
			if ((((tmp2 = strtok(NULL, " \t")) == NULL) && (usersuffix[0] == 0))
			 || ((tmp2 != NULL) && (usersuffix[0] != 0) && (strcasecmp(tmp2,usersuffix) == 0))) {
				if (as_set == 1) {
					memset(buf, 0, sizeof(buf));
					close(fd);
					pop_log(pop_priority, "user config: APOP secret already set in user config file");
					return;
				};

				strcpy(apop_secret, tmpapop_secret);
				as_set = 1;
			};

			continue;
		};
		memset(tmpapop_secret, 0, sizeof(tmpapop_secret));
#endif
		memset(buf, 0, sizeof(buf));
		close(fd);
		pop_log(pop_priority, "user config: unknown option name, line: %u", linenr);
		return;
	};
	close(fd);
	if (md_set == 1) {
	};
#ifdef APOP
	memset(tmpapop_secret, 0, sizeof(tmpapop_secret));
#endif
	return;
}