forked from GerHobbelt/xmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MkUsers.cpp
408 lines (331 loc) · 11.6 KB
/
MkUsers.cpp
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
* XMail by Davide Libenzi (Intranet and Internet mail server)
* Copyright (C) 1999,..,2010 Davide Libenzi
*
* 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
*
* Davide Libenzi <[email protected]>
*
*/
#include "SysInclude.h"
#include "SysDep.h"
#include "SvrDefines.h"
#if defined(WIN32)
#if 0
#include <stdio.h>
#include <io.h>
#include <direct.h>
#include <stdlib.h>
#include <string.h>
#endif
/*
#define SYS_SLASH_CHAR '\\'
#define SYS_SLASH_STR "\\"
#define SYS_MAX_PATH 256
*/
int SysPathExist(char const *pszPathName)
{
return (_access(pszPathName, 0) == 0) ? 1 : 0;
}
int SysMakeDir(char const *pszPathName)
{
return (_mkdir(pszPathName) == 0) ? 1 : 0;
}
int SysErrNo(void)
{
return errno;
}
char const *SysErrStr(void)
{
return strerror(errno);
}
#else // #if defined(WIN32)
#if defined(__LINUX__) || defined(__SOLARIS__) || defined(__BSD__)
#if 0
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <dirent.h>
#define SYS_SLASH_CHAR '/'
#define SYS_SLASH_STR "/"
#define SYS_MAX_PATH 256
#endif
int SysPathExist(char const *pszPathName)
{
return (access(pszPathName, 0) == 0) ? 1 : 0;
}
int SysMakeDir(char const *pszPathName)
{
return (mkdir(pszPathName, 0700) == 0) ? 1 : 0;
}
int SysErrNo(void)
{
return errno;
}
char const *SysErrStr(void)
{
return strerror(errno);
}
#else // #if defined(__LINUX__) || defined(__SOLARIS__)
#error system type not defined !
#endif // #if defined(__LINUX__) || defined(__SOLARIS__)
#endif // #if defined(WIN32)
#define MAX_MB_SIZE 10000
#define StrNCpy(t, s, n) do { strncpy(t, s, n); (t)[(n) - 1] = '\0'; } while (0)
#define StrSNCpy(t, s) StrNCpy(t, s, sizeof(t))
char *StrCrypt(char const *pszInput, char *pszCrypt)
{
strcpy(pszCrypt, "");
for (int ii = 0; pszInput[ii] != '\0'; ii++) {
unsigned int uChar = (unsigned int) pszInput[ii];
char szByte[32] = "";
sprintf(szByte, "%02x", (uChar ^ 101) & 0xff);
strcat(pszCrypt, szByte);
}
return pszCrypt;
}
int CreateUser(char const *pszRootDir, char const *pszDomain,
char const *pszUsername, char const *pszPassword,
unsigned int uUserId, char const *pszRealName,
char const *pszHomePage, unsigned int uMBSize, bool bMaildir, FILE * pUsrFile)
{
FILE *pTabFile;
char szPathName[SYS_MAX_PATH] = "";
char szCryptPwd[256] = "";
/* Check-create domain directory */
sprintf(szPathName, "%s" MAIL_DOMAINS_DIR SYS_SLASH_STR "%s", pszRootDir, pszDomain); /* [i_a] */
if (!SysPathExist(szPathName) && !SysMakeDir(szPathName)) {
perror(szPathName);
return SysErrNo();
}
/* Check-create domain/user directory */
sprintf(szPathName, "%s" MAIL_DOMAINS_DIR SYS_SLASH_STR "%s" SYS_SLASH_STR "%s", pszRootDir, /* [i_a] */
pszDomain, pszUsername);
if (!SysPathExist(szPathName) && !SysMakeDir(szPathName)) {
perror(szPathName);
return SysErrNo();
}
if (bMaildir) {
/* Check-create domain/user/Maildir/(tmp,new,cur) directories */
sprintf(szPathName,
"%s" MAIL_DOMAINS_DIR SYS_SLASH_STR "%s" SYS_SLASH_STR "%s" SYS_SLASH_STR MAILDIR_DIRECTORY, /* [i_a] */
pszRootDir, pszDomain, pszUsername);
if (!SysPathExist(szPathName) && !SysMakeDir(szPathName)) {
perror(szPathName);
return SysErrNo();
}
sprintf(szPathName,
"%s" MAIL_DOMAINS_DIR SYS_SLASH_STR "%s" SYS_SLASH_STR "%s" SYS_SLASH_STR MAILDIR_DIRECTORY /* [i_a] */
SYS_SLASH_STR "tmp", pszRootDir, pszDomain, pszUsername);
if (!SysPathExist(szPathName) && !SysMakeDir(szPathName)) {
perror(szPathName);
return SysErrNo();
}
sprintf(szPathName,
"%s" MAIL_DOMAINS_DIR SYS_SLASH_STR "%s" SYS_SLASH_STR "%s" SYS_SLASH_STR MAILDIR_DIRECTORY /* [i_a] */
SYS_SLASH_STR "new", pszRootDir, pszDomain, pszUsername);
if (!SysPathExist(szPathName) && !SysMakeDir(szPathName)) {
perror(szPathName);
return SysErrNo();
}
sprintf(szPathName,
"%s" MAIL_DOMAINS_DIR SYS_SLASH_STR "%s" SYS_SLASH_STR "%s" SYS_SLASH_STR MAILDIR_DIRECTORY /* [i_a] */
SYS_SLASH_STR "cur", pszRootDir, pszDomain, pszUsername);
if (!SysPathExist(szPathName) && !SysMakeDir(szPathName)) {
perror(szPathName);
return SysErrNo();
}
} else {
/* Check-create domain/user/mailbox directory */
sprintf(szPathName,
"%s" MAIL_DOMAINS_DIR SYS_SLASH_STR "%s" SYS_SLASH_STR "%s" SYS_SLASH_STR MAILBOX_DIRECTORY, /* [i_a] */
pszRootDir, pszDomain, pszUsername);
if (!SysPathExist(szPathName) && !SysMakeDir(szPathName)) {
perror(szPathName);
return SysErrNo();
}
}
/* Check-create domain/user/mailbox/user.tab file */
sprintf(szPathName,
"%s" MAIL_DOMAINS_DIR SYS_SLASH_STR "%s" SYS_SLASH_STR "%s" SYS_SLASH_STR USER_PROFILE_FILE, /* [i_a] */
pszRootDir, pszDomain, pszUsername);
if (!SysPathExist(szPathName)) {
if ((pTabFile = fopen(szPathName, "wt")) == NULL) {
perror(szPathName);
return SysErrNo();
}
fprintf(pTabFile,
"\"RealName\"\t\"%s\"\n"
"\"HomePage\"\t\"%s\"\n"
"\"MaxMBSize\"\t\"%u\"\n", pszRealName, pszHomePage, uMBSize);
fclose(pTabFile);
}
/* Add user to users file */
fprintf(pUsrFile,
"\"%s\"\t"
"\"%s\"\t"
"\"%s\"\t"
"\"%u\"\t"
"\"%s\"\t"
"\"U\"\n", pszDomain, pszUsername, StrCrypt(pszPassword, szCryptPwd), uUserId,
pszUsername);
return 0;
}
void ShowUsage(char const *pszProgName)
{
fprintf(stderr,
"use : %s [-adfursih]\n"
" -a numusers = number of users to create in auto-mode\n"
" -d domain = domain name in auto-mode\n"
" -f inputFile = input file name {stdin}\n"
" -u username = radix user name in auto-mode\n"
" -r rootdir = mail root path {.%s}\n"
" -s mboxsize = mailbox maximum size {%d}\n"
" -i useridbase = base user id {1}\n"
" -m = create Maildir boxes\n"
" -h = show this message\n",
pszProgName, SYS_SLASH_STR, MAX_MB_SIZE);
}
int main(int argc, char *argv[])
{
int ii;
int iNumUsers = 0;
bool bMaildir = false;
unsigned int uMBSize = MAX_MB_SIZE;
unsigned int uUserId = 1;
FILE *pUsrFile;
FILE *pInFile = stdin;
char szRootDir[SYS_MAX_PATH] = "." SYS_SLASH_STR;
char szInputFile[SYS_MAX_PATH] = "";
char szPathName[SYS_MAX_PATH] = "";
char szAutoDomain[256] = "mkuser.net";
char szAutoUsr[128] = "mkuser";
char szUsrLine[1024] = "";
for (ii = 1; ii < argc; ii++) {
if (argv[ii][0] != '-')
continue;
switch (argv[ii][1]) {
case ('a'):
if (++ii < argc)
iNumUsers = atoi(argv[ii]);
break;
case ('d'):
if (++ii < argc)
StrSNCpy(szAutoDomain, argv[ii]);
break;
case ('f'):
if (++ii < argc)
StrSNCpy(szInputFile, argv[ii]);
break;
case ('u'):
if (++ii < argc)
StrSNCpy(szAutoUsr, argv[ii]);
break;
case ('r'):
if (++ii < argc)
StrSNCpy(szRootDir, argv[ii]);
break;
case ('s'):
if (++ii < argc)
uMBSize = (unsigned int) atol(argv[ii]);
break;
case ('i'):
if (++ii < argc)
uUserId = (unsigned int) atol(argv[ii]);
break;
case ('m'):
bMaildir = true;
break;
case ('h'):
ShowUsage(argv[0]);
return 0;
default:
ShowUsage(argv[0]);
return 1;
}
}
/* Root directory slash termination */
if (szRootDir[strlen(szRootDir) - 1] != SYS_SLASH_CHAR)
strcat(szRootDir, SYS_SLASH_STR);
/* Check-create domains directory */
sprintf(szPathName, "%s%s", szRootDir, MAIL_DOMAINS_DIR); /* [i_a] */
if (!SysPathExist(szPathName) && !SysMakeDir(szPathName)) {
perror(szPathName);
return SysErrNo();
}
/* Create mailusers.tab file */
sprintf(szPathName, "%s%s", szRootDir, SVR_TABLE_FILE); /* [i_a] */
if (SysPathExist(szPathName)) {
fprintf(stderr, "%s already exist\n", szPathName);
return 1;
}
if ((pUsrFile = fopen(szPathName, "wt")) == NULL) {
perror(szPathName);
return SysErrNo();
}
if (iNumUsers == 0) {
if ((strlen(szInputFile) != 0) && ((pInFile = fopen(szInputFile, "rt")) == NULL)) {
perror(szPathName);
fclose(pUsrFile);
return SysErrNo();
}
/* Get input from stdin */
while (fgets(szUsrLine, sizeof(szUsrLine) - 1, pInFile) != NULL) {
char *pszDomain;
char *pszUsername;
char *pszPassword;
char *pszRealName;
char *pszHomePage;
szUsrLine[strlen(szUsrLine) - 1] = '\0';
if ((szUsrLine[0] == '#') ||
((pszDomain = strtok(szUsrLine, ";")) == NULL) ||
((pszUsername = strtok(NULL, ";")) == NULL) ||
((pszPassword = strtok(NULL, ";")) == NULL) ||
((pszRealName = strtok(NULL, ";")) == NULL) ||
((pszHomePage = strtok(NULL, ";")) == NULL))
continue;
if (CreateUser(szRootDir, pszDomain, pszUsername, pszPassword, uUserId++,
pszRealName, pszHomePage, uMBSize, bMaildir,
pUsrFile) != 0) {
fprintf(stderr, "error creating <%s@%s> : %s\n",
pszUsername, pszDomain, SysErrStr());
}
}
if (pInFile != stdin)
fclose(pInFile);
} else {
/* Automatically generate users */
for (ii = 1; ii <= iNumUsers; ii++) {
char szUsername[256] = "";
char szHomePage[256] = "";
sprintf(szUsername, "%s%d", szAutoUsr, ii);
sprintf(szHomePage, "http://www.%s/~%s", szAutoDomain, szUsername);
if (CreateUser(szRootDir, szAutoDomain, szUsername, szUsername, uUserId++,
szUsername, szHomePage, uMBSize, bMaildir, pUsrFile) != 0)
{
fprintf(stderr, "error creating <%s@%s> : %s\n",
szUsername, szAutoDomain, SysErrStr());
}
}
}
fclose(pUsrFile);
return 0;
}