mysql: revoke permissions when dropping database
[hcoop/domtool2.git] / src / mail / vmailpasswd.c
CommitLineData
7aa78013
AC
1/*
2Domtool (http://hcoop.sf.net/)
3Copyright (C) 2005-2007 Adam Chlipala
4
5This program is free software; you can redistribute it and/or
6modify it under the terms of the GNU General Public License
7as published by the Free Software Foundation; either version 2
8of the License, or (at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18*/
19
20// Setting a virtual mailbox's password if you know its current password
21
22#define _XOPEN_SOURCE
23#include <unistd.h>
f625c393 24#include <stdlib.h>
7aa78013
AC
25#include <stdio.h>
26#include <ctype.h>
8f0313eb 27#include <crypt.h>
7aa78013
AC
28#include <string.h>
29
30int validDomain(const char *dom) {
31 for (; *dom; ++dom)
32 if (*dom != '.' && *dom != '_' && *dom != '-' && !isalnum(*dom))
33 return 0;
34 return 1;
35}
36
f625c393 37int main(int argc, char *argv[]) {
7aa78013
AC
38 char *domain, addr[1024], cmd[1024], *crypted;
39 FILE *pw;
40 int rc;
41
42 if (argc != 4) {
43 puts("Wrong number of arguments");
44 return 1;
45 }
46
47 if (strlen(argv[1]) >= 1024) {
48 puts("Address too long");
49 return 1;
50 }
51 strcpy(addr, argv[1]);
52
53 domain = strchr(addr, '@');
54
55 if (!domain) {
56 puts("No @-sign found in address");
57 return 1;
58 }
59 *domain = 0;
60 ++domain;
61
62 if (!validDomain(addr)) {
63 puts("Invalid local part");
64 return 1;
65 }
66 if (!validDomain(domain)) {
67 puts("Invalid domain");
68 return 1;
69 }
70
71 sprintf(cmd, "/usr/sbin/userdb -show \"%s/%s\" | /bin/grep ^systempw=", domain, argv[1]);
72 pw = popen(cmd, "r");
73 if (!pw) {
74 puts("popen failure");
75 return 1;
76 }
77
78 if (fscanf(pw, "systempw=%1023[^\n]", cmd) != 1) {
79 puts("Error parsing userdb output");
80 return 1;
81 }
82
83 pclose(pw);
84
85 memcpy(addr, cmd, 2);
86 addr[2] = 0;
87
88 crypted = crypt(argv[2], addr);
89
90 if (!crypted) {
91 puts("Error encrypting");
92 return 1;
93 }
94
95 if (strlen(crypted) > 200){
96 puts("Encrypted password too long");
97 return 1;
98 }
99
100 if (strcmp(crypted, cmd)) {
101 puts("Wrong password");
102 return 2;
103 }
104
105 sprintf(cmd, "/usr/sbin/userdbpw | /usr/sbin/userdb \"%s/%s\" set systempw", domain, argv[1]);
106 pw = popen(cmd, "w");
107
108 if (!pw) {
109 puts("Error running userdbpw");
110 return 1;
111 }
112
113 fputs(argv[3], pw);
114 fputc('\n', pw);
115
116 rc = pclose(pw);
117 if (rc) {
118 puts("userdbpw error");
119 return rc;
120 }
121
37478a27 122 return system("/usr/local/sbin/domtool-publish courier");
7aa78013 123}