/* * vconvert * part of the vpopmail package * * Copyright (C) 1999,2001 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 #include #include #include #include #include #include #include #include #include #include #include "config.h" #include "vpopmail.h" #include "vauth.h" #include "vmysql.h" #ifdef HAS_SHADOW #include #endif int do_all_domains(); #define MAX_BUFF 500 char User[MAX_BUFF]; char Passwd[MAX_BUFF]; char Quota[MAX_BUFF]; char Gecos[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[128]; int PasswdFormat; int main(int argc, char *argv[]) { get_options(argc,argv); if ( optind == argc ) { do_all_domains(); } else { for(;optindd_name,".", 1)==0) continue; strncpy( domain, mydirent->d_name, 100); printf("converting %s ", domain); if ( conv_domain( domain ) != 0 ) { printf("domain conversion failed\n"); } else { printf("done\n"); } } closedir(mydir); 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 converstion\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 converstion\n"); return(-1); } break; case PASSWD_SITE: return( passwd_to_vpopmail( domain )); default: printf("unknown converstion\n"); return(-1); } } int cdb_to_default( char *domain ) { #ifdef USE_SQL FILE *fs; char tmpbuf[200]; struct vqpasswd *pw; vauth_deldomain(domain); vauth_adddomain(domain); snprintf(tmpbuf, 200, "%s/%s/%s/vpasswd", VPOPMAILDIR, DOMAINS_DIR, domain); fs = fopen(tmpbuf,"r"); if ( fs == NULL ) { return(-1); } while( (pw=vgetent(fs)) != NULL ) { vauth_adduser(pw->pw_name, domain, pw->pw_passwd, pw->pw_gecos, pw->pw_dir, pw->pw_uid); vauth_setpw(pw, domain); } fclose(fs); #endif return(0); } int sql_to_cdb( char *domain) { #ifdef USE_SQL struct vqpasswd *pw; FILE *fs; char tmpbuf[255]; snprintf(tmpbuf, 255, "%s/%s/%s/vpasswd", VPOPMAILDIR, DOMAINS_DIR, domain); if ( (fs = fopen(tmpbuf,"w")) == NULL ) { printf("could not open vpasswd file %s\n", tmpbuf); return(-1); } pw = vauth_getall(domain, 1, 0); while( pw != NULL ) { 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); pw = vauth_getall(domain, 0, 0); } fclose(fs); printf("%s done\n", domain); #endif 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); strncpy( passwd, smypw->sp_pwdp, i ); #else i = strlen(mypw->pw_name)+1; passwd = malloc(i); strncpy( passwd, mypw->pw_passwd, i ); #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"); fprintf(stdout, " the second option sets which format to convert TO"); 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; strncpy(PasswdFile, optarg, 128); 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[200]; char tmpbuf1[200]; 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, 200, fs) != NULL ) { if ( (user=strtok(tmpbuf, PASSWD_TOKENS))==NULL) continue; if ( (crypted_passwd=strtok(NULL, PASSWD_TOKENS))==NULL) continue; strncpy( Gecos, user, MAX_BUFF); 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, 200, "%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); }