From: Adam Chlipala Date: Sun, 4 Nov 2007 20:51:10 +0000 (+0000) Subject: vmailpasswd X-Git-Tag: release_2010-11-19~168 X-Git-Url: https://git.hcoop.net/hcoop/domtool2.git/commitdiff_plain/7aa78013a17ce35ec805110a0ef622e403882b6e?hp=3bf720f753c0a50e6c5f753a9a786d68fa15af93 vmailpasswd --- diff --git a/Makefile b/Makefile index f538e94..84b3623 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,7 @@ -all: mlton +all: mlton bin/vmailpasswd + +bin/vmailpasswd: src/mail/vmailpasswd.c + $(CC) -lcrypt -o $@ $< COMMON_DEPS := configDefault/config.sig configDefault/configDefault.sml \ openssl/openssl_sml.so config.sml @@ -169,6 +172,7 @@ install: -cp bin/setsa /usr/local/bin/ -cp bin/smtplog /usr/local/bin/ -cp bin/mysql-fixperms /usr/local/bin/ + -cp bin/vmailpasswd /usr/local/bin/ cp src/plugins/domtool-postgres /usr/local/sbin/ cp src/plugins/domtool-mysql /usr/local/sbin/ diff --git a/bin/.cvsignore b/bin/.cvsignore index 452990c..7686142 100644 --- a/bin/.cvsignore +++ b/bin/.cvsignore @@ -8,3 +8,4 @@ vmail setsa smtplog mysql-fixperms +vmailpasswd diff --git a/scripts/domtool-publish b/scripts/domtool-publish index fd8b8fc..fe111df 100755 --- a/scripts/domtool-publish +++ b/scripts/domtool-publish @@ -55,6 +55,7 @@ case $1 in redo_exim ;; courier) + /usr/sbin/makeuserdb /bin/cat /etc/courier/userdb/* >/etc/courier/exim /bin/chmod o-r /etc/courier/exim /usr/sbin/exim_dbmbuild /etc/courier/exim /etc/courier/exim.dat diff --git a/src/mail/vmailpasswd.c b/src/mail/vmailpasswd.c new file mode 100644 index 0000000..13b38a9 --- /dev/null +++ b/src/mail/vmailpasswd.c @@ -0,0 +1,121 @@ +/* +Domtool (http://hcoop.sf.net/) +Copyright (C) 2005-2007 Adam Chlipala + +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. +*/ + +// Setting a virtual mailbox's password if you know its current password + +#define _XOPEN_SOURCE +#include +#include +#include +#include + +int validDomain(const char *dom) { + for (; *dom; ++dom) + if (*dom != '.' && *dom != '_' && *dom != '-' && !isalnum(*dom)) + return 0; + return 1; +} + +main(int argc, char *argv[]) { + char *domain, addr[1024], cmd[1024], *crypted; + FILE *pw; + int rc; + + if (argc != 4) { + puts("Wrong number of arguments"); + return 1; + } + + if (strlen(argv[1]) >= 1024) { + puts("Address too long"); + return 1; + } + strcpy(addr, argv[1]); + + domain = strchr(addr, '@'); + + if (!domain) { + puts("No @-sign found in address"); + return 1; + } + *domain = 0; + ++domain; + + if (!validDomain(addr)) { + puts("Invalid local part"); + return 1; + } + if (!validDomain(domain)) { + puts("Invalid domain"); + return 1; + } + + sprintf(cmd, "/usr/sbin/userdb -show \"%s/%s\" | /bin/grep ^systempw=", domain, argv[1]); + pw = popen(cmd, "r"); + if (!pw) { + puts("popen failure"); + return 1; + } + + if (fscanf(pw, "systempw=%1023[^\n]", cmd) != 1) { + puts("Error parsing userdb output"); + return 1; + } + + pclose(pw); + + memcpy(addr, cmd, 2); + addr[2] = 0; + + crypted = crypt(argv[2], addr); + + if (!crypted) { + puts("Error encrypting"); + return 1; + } + + if (strlen(crypted) > 200){ + puts("Encrypted password too long"); + return 1; + } + + if (strcmp(crypted, cmd)) { + puts("Wrong password"); + return 2; + } + + sprintf(cmd, "/usr/sbin/userdbpw | /usr/sbin/userdb \"%s/%s\" set systempw", domain, argv[1]); + pw = popen(cmd, "w"); + + if (!pw) { + puts("Error running userdbpw"); + return 1; + } + + fputs(argv[3], pw); + fputc('\n', pw); + + rc = pclose(pw); + if (rc) { + puts("userdbpw error"); + return rc; + } + + return 0; +}