1 /* qmail-lookup.c - qmail CVM lookup routines
2  * Copyright (C) 2010  Bruce Guenter <bruce@untroubled.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18 #include <stdlib.h>
19 #include <bglibs/str.h>
20 
21 #include "module.h"
22 #include "qmail.h"
23 
24 static const char* missingdomain = 0;
25 static const char* missinguser = "alias";
26 
qmail_lookup_init(void)27 int qmail_lookup_init(void)
28 {
29   if (qmail_init() != 0
30       || qmail_users_init() != 0
31       || qmail_domains_init() != 0)
32     return -1;
33 
34   if ((missingdomain = getenv("CVM_QMAIL_MISSINGDOMAIN")) != 0)
35     if (*missingdomain == 0)
36       missingdomain = "localhost";
37   if ((missinguser = getenv("CVM_QMAIL_MISSINGUSER")) == 0
38       || *missinguser == 0)
39     missinguser = "alias";
40 
41   return 0;
42 }
43 
44 /* Look up the CVM domain and account name in the qmail configuration.
45  * Returns:
46  * -1 System or other error
47  * 0 Success
48  * 1 Domain not found
49  * 2 Account not found */
qmail_lookup_cvm(struct qmail_user * user,str * domain,str * username,str * ext)50 int qmail_lookup_cvm(struct qmail_user* user,
51 		     str* domain,
52 		     str* username,
53 		     str* ext)
54 {
55   static str prefix;
56   static str fullname;
57 
58   if (cvm_module_credentials[CVM_CRED_DOMAIN].len == 0)
59     if (!str_copys(&cvm_module_credentials[CVM_CRED_DOMAIN],
60 		   qmail_envnoathost))
61       return CVME_IO;
62 
63   if (qmail_users_reinit() != 0
64       || qmail_domains_reinit() != 0)
65     return -1;
66 
67   switch (qmail_domains_lookup(&cvm_module_credentials[CVM_CRED_DOMAIN],
68 			       domain, &prefix)) {
69   case -1:
70     return -1;
71   case 0:
72     if (missingdomain == 0)
73       return 1;
74     if (!str_copys(domain, missingdomain)
75 	|| !str_copys(&fullname, missinguser))
76       return -1;
77     break;
78   default:
79     fullname.len = 0;
80     if (prefix.len > 0)
81       if (!str_copy(&fullname, &prefix)
82 	  || !str_catc(&fullname, '-'))
83 	return -1;
84     if (!str_cat(&fullname, &cvm_module_credentials[CVM_CRED_ACCOUNT]))
85       return -1;
86   }
87 
88   switch (qmail_users_lookup_split(user, fullname.s, username, ext)) {
89   case -1:
90     return -1;
91   case 0:
92     return 2;
93   }
94   return 0;
95 }
96