-
Notifications
You must be signed in to change notification settings - Fork 3
/
vconvert.c
494 lines (440 loc) · 11.1 KB
/
vconvert.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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
* Copyright (C) 1999-2009 Inter7 Internet Technologies, Inc.
*
* 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <pwd.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include "config.h"
#include "vpopmail.h"
#include "vauth.h"
#include "vmysql.h"
#ifdef HAS_SHADOW
#include <shadow.h>
#endif
int do_all_domains();
char User[MAX_BUFF];
char Passwd[MAX_BUFF];
char Gecos[MAX_BUFF];
char Dir[MAX_BUFF];
#define MYSQL_SITE_ARG "-m"
#define CDB_SITE_ARG "-c"
#define ETC_SITE_ARG "-e"
#define ETC_FILE_ARG "-f"
#define PASSWD_FILE_ARG "-p"
#define MYSQL_SITE 1
#define CDB_SITE 2
#define ETC_SITE 3
#define PASSWD_SITE 4
#define SQWEBMAIL_SITE 5
#define FORMAT_NOFORMAT 0
#define FORMAT_USERPASS 1
#define PASSWD_TOKENS " :\n\t"
int FromFormat;
int ToFormat;
int Debug;
int conv_domain( char *);
int set_sqwebmail_pass( char *);
int cdb_to_default( char *);
int sql_to_cdb( char *);
int etc_to_default( char *);
void usage();
void get_options(int argc, char **argv);
int passwd_to_vpopmail( char *domain );
char PasswdFile[MAX_BUFF];
int PasswdFormat;
int main(int argc, char *argv[])
{
if( vauth_open( 1 )) {
vexiterror( stderr, "Initial open." );
}
get_options(argc,argv);
if ( optind == argc ) {
do_all_domains();
} else {
for(;optind<argc;++optind){
printf("converting %s ", argv[optind]);
lowerit(argv[optind]);
if ( conv_domain( argv[optind] ) != 0 ) {
printf("domain conversion failed\n");
/* should exit -1 here? */
} else {
printf("done\n");
}
}
}
return(vexit(0));
}
int do_all_domains()
{
domain_entry *e;
e = get_domain_entries("");
while( e ) {
if( strcmp( e->realdomain, e->domain ) != 0 ) { // is an alias
printf( "%s is an alias of %s.\n",
e->domain, e->realdomain );
} else {
printf( "converting %s... ", e->realdomain );
if ( conv_domain( e->realdomain ) != 0 ) {
printf("domain conversion failed\n");
/* should vexit -1 here? */
} else {
printf("done\n");
}
}
e = get_domain_entries( NULL );
}
return(0);
}
int conv_domain( char *domain )
{
switch ( FromFormat ) {
case SQWEBMAIL_SITE:
return(set_sqwebmail_pass( domain));
break;
case MYSQL_SITE:
switch (ToFormat) {
case CDB_SITE:
return(sql_to_cdb( domain));
default:
printf("unknown conversion\n");
return(-1);
}
break;
case CDB_SITE:
return(cdb_to_default( domain));
break;
case ETC_SITE:
switch ( ToFormat ) {
case MYSQL_SITE:
return(etc_to_default( domain));
case CDB_SITE:
return(etc_to_default( domain ));
default:
printf("unknown conversion\n");
return(-1);
}
break;
case PASSWD_SITE:
return( passwd_to_vpopmail( domain ));
default:
printf("unknown converstion\n");
return(-1);
}
printf(".");
}
int cdb_to_default( char *domain )
{
#ifdef USE_SQL
FILE *fs;
char tmpbuf[MAX_BUFF];
struct vqpasswd *pw;
int domain_str_len = strlen( domain );
char domain_dir[MAX_BUFF];
FILE *assign_fs;
static char assignbuf[MAX_BUFF];
int i, colon_count, dir_count;
int bFoundDomain = 0;
char assign_file[MAX_BUFF];
uid_t uid;
gid_t gid;
int r;
snprintf(assign_file, sizeof(assign_file), "%s/users/assign", QMAILDIR);
if ( (assign_fs=fopen(assign_file, "r"))==NULL ) {
r = snprintf(tmpbuf, sizeof(tmpbuf), "could not open qmail assign file at %s\n", assign_file);
if (r == -1) {
perror("temporary string buffer too short\n");
}
perror(tmpbuf);
return(-1);
}
while ( fgets(assignbuf, sizeof(assignbuf), assign_fs) != NULL && !bFoundDomain )
{
/* search for the matching domain record */
if ( strncmp( domain, &assignbuf[1], domain_str_len ) == 0 )
{
bFoundDomain = 1;
/* found match, now get directory */
for ( i=1, colon_count = 0;
colon_count < 4;
colon_count++, i++ )
{
for( ;
assignbuf[i]!=':';
++i )
; /* skip non-colon characters */
}
/* found 4th colon, so get the directory name */
for( dir_count = 0;
assignbuf[i]!=':';
++i, dir_count++ )
{
domain_dir[dir_count] = assignbuf[i];
}
domain_dir[dir_count] = 0; /* null termination */
}
}
fclose(assign_fs);
vauth_deldomain(domain);
vdel_dir_control(domain);
vauth_adddomain(domain);
vget_assign(domain, Dir, sizeof(Dir), &uid, &gid );
#ifdef USERS_BIG_DIR
open_big_dir (domain, uid, gid);
#endif
r = snprintf(tmpbuf, sizeof(tmpbuf), "%s/vpasswd", Dir);
if (r == -1) {
return (-1);
}
fs = fopen(tmpbuf,"r");
if ( fs == NULL ) return(-1);
while( (pw=vgetent(fs)) != NULL ) {
if (vauth_adduser(pw->pw_name, domain, pw->pw_passwd,
pw->pw_gecos, pw->pw_dir, pw->pw_uid) != 0) {
printf("User %s domain %s did not add\n", pw->pw_name, domain);
continue;
}
vauth_setpw(pw, domain);
#ifdef USERS_BIG_DIR
next_big_dir (uid, gid); /* increment user count */
#endif
}
fclose(fs);
#ifdef USERS_BIG_DIR
close_big_dir (domain, uid, gid);
#endif
#endif /* USE_SQL */
return(0);
}
int sql_to_cdb( char *domain)
{
#ifdef USE_SQL
struct vqpasswd *pw;
FILE *fs;
int r;
char tmpbuf[MAX_BUFF];
if (vget_assign(domain, Dir, sizeof(Dir), NULL, NULL ) == NULL) {
printf("Error. Domain not found\n");
return (-1);
}
r = snprintf(tmpbuf, sizeof(tmpbuf), "%s/vpasswd", Dir);
if (r == -1) {
printf("temporary string buffer too short\n");
return (-1);
}
if ( (fs = fopen(tmpbuf,"w")) == NULL ) {
printf("could not open vpasswd file %s\n", tmpbuf);
return(-1);
}
pw = vauth_getall(domain, 1, 1);
while( pw != NULL ) {
#ifdef CLEAR_PASS
fprintf(fs, "%s:%s:%d:%d:%s:%s:%s:%s\n",
pw->pw_name,
pw->pw_passwd,
pw->pw_uid,
pw->pw_gid,
pw->pw_gecos,
pw->pw_dir,
pw->pw_shell,
pw->pw_clear_passwd);
#else /* CLEAR_PASS */
fprintf(fs, "%s:%s:%d:%d:%s:%s:%s\n",
pw->pw_name,
pw->pw_passwd,
pw->pw_uid,
pw->pw_gid,
pw->pw_gecos,
pw->pw_dir,
pw->pw_shell);
#endif /* CLEAR_PASS */
pw = vauth_getall(domain, 0, 1);
}
fclose(fs);
printf("%s done\n", domain);
#endif /* USE_SQL */
return(0);
}
int etc_to_default( char *domain )
{
struct passwd *mypw;
struct vqpasswd *newpw = NULL;
char *passwd;
int i;
#ifdef HAS_SHADOW
struct spwd *smypw;
#endif
while( (mypw = getpwent()) != NULL ) {
#ifdef HAS_SHADOW
if ( (smypw = getspnam(mypw->pw_name)) == NULL) continue;
i = strlen(smypw->sp_pwdp)+1;
passwd = malloc(i);
snprintf( passwd, i, "%s", smypw->sp_pwdp );
#else
i = strlen(mypw->pw_passwd)+1;
passwd = malloc(i);
snprintf( passwd, i, "%s", mypw->pw_passwd );
#endif
if ( strlen(passwd) > 2 ) {
if (vadduser( mypw->pw_name, domain, "xxxx",
mypw->pw_gecos, USE_POP) != 0) {
printf("user %s domain %s did not add\n",
mypw->pw_name, domain);
continue;
}
newpw = vauth_getpw( mypw->pw_name, domain);
newpw->pw_passwd = passwd;
vauth_setpw( newpw, domain);
} else {
printf("skipping %s\n", mypw->pw_name);
}
free(passwd);
}
return(0);
}
void usage()
{
fprintf(stdout, "vconvert: usage\n");
fprintf(stdout, " The first option sets which format to convert FROM,\n");
fprintf(stdout, " the second option sets which format to convert TO.\n");
fprintf(stdout, " -e = etc format\n");
fprintf(stdout, " -c = cdb format\n");
fprintf(stdout, " -m = sql format\n");
fprintf(stdout, " -S = set sqwebmail passwords\n");
fprintf(stdout, " -v = version\n");
fprintf(stdout, " -d = debug info\n");
/*
fprintf(stdout, " [-f file] lets you override /etc/passwd as the\n");
fprintf(stdout, " default file to use for -e option\n");
*/
}
void get_options(int argc, char **argv)
{
int c;
int errflag;
errflag = 0;
FromFormat = -1;
ToFormat = -1;
PasswdFile[0] = 0;
PasswdFormat = 0;
Debug = 0;
while( !errflag && (c=getopt(argc,argv,"mcep:Svd")) != -1 ) {
switch(c) {
case 'd':
Debug = 1;
break;
case 'v':
printf("version: %s\n", VERSION);
break;
case 'S':
FromFormat = SQWEBMAIL_SITE;
ToFormat = SQWEBMAIL_SITE;
break;
case 'm':
if ( FromFormat == -1 ) FromFormat = MYSQL_SITE;
else ToFormat = MYSQL_SITE;
break;
case 'c':
if ( FromFormat == -1 ) FromFormat = CDB_SITE;
else ToFormat = CDB_SITE;
break;
case 'e':
if ( FromFormat == -1 ) FromFormat = ETC_SITE;
else ToFormat = ETC_SITE;
break;
case 'p':
if ( FromFormat == -1 ) FromFormat = PASSWD_SITE;
else ToFormat = PASSWD_SITE;
PasswdFormat = FORMAT_USERPASS;
snprintf(PasswdFile, sizeof(PasswdFile), "%s", optarg);
break;
default:
errflag = 1;
break;
}
}
if ( FromFormat == -1 || ToFormat == -1 || errflag > 0 ) {
usage();
vexit(-1);
}
}
int passwd_to_vpopmail( char *domain )
{
FILE *fs,*fs1;
char tmpbuf[MAX_BUFF];
char tmpbuf1[MAX_BUFF];
char *user;
char *crypted_passwd;
struct vqpasswd *mypw;
int err = 0;
if ( (fs=fopen(PasswdFile, "r")) == NULL) {
printf("Could not open passwd file %s\n", PasswdFile);
perror("fopen");
return(-1);
}
while( fgets(tmpbuf, sizeof(tmpbuf), fs) != NULL ) {
if ( (user=strtok(tmpbuf, PASSWD_TOKENS))==NULL) continue;
if ( (crypted_passwd=strtok(NULL, PASSWD_TOKENS))==NULL) continue;
snprintf(Gecos, sizeof(Gecos), "%s", user);
if ( (err=vadduser(user, domain, "foob", user, USE_POP )) < 0 ) {
printf("Error: %s\n", verror(err));
break;
}
if ( (mypw = vauth_getpw( user, domain )) == NULL ) {
printf("no such user %s@%s\n", user, domain);
break;
}
mypw->pw_passwd = crypted_passwd;
if ( (err=vauth_setpw( mypw, domain )) != 0 ) {
printf("Error: %s\n", verror(err));
break;
}
snprintf(tmpbuf1, sizeof(tmpbuf1),
"%s/Maildir/sqwebmail-pass", mypw->pw_dir);
if ( (fs1=fopen(tmpbuf1, "w")) == NULL) {
break;
}
fprintf(fs1, "\t%s", crypted_passwd);
fclose(fs1);
}
fclose(fs);
return(err);
}
int set_sqwebmail_pass( char *domain)
{
struct vqpasswd *pw;
if ( Debug == 1 ) {
printf("Setting sqwebmail passwords for %s\n", domain);
}
pw = vauth_getall(domain, 1, 0);
while( pw != NULL ) {
if ( Debug == 1 ) {
printf("%s\n", pw->pw_name);
}
vsqwebmail_pass( pw->pw_dir, pw->pw_passwd,
VPOPMAILUID, VPOPMAILGID);
pw = vauth_getall(domain, 0, 0);
}
return(0);
}