1 /* vmlookup.c - vmailmgr 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 <sys/types.h>
19 #include <errno.h>
20 #include <grp.h>
21 #include <pwd.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 
27 #include <bglibs/cdb.h>
28 #include <bglibs/cdb.h>
29 #include <bglibs/dict.h>
30 #include <bglibs/dict.h>
31 #include <bglibs/iobuf.h>
32 #include <bglibs/path.h>
33 #include <bglibs/str.h>
34 #include <vmailmgr/vpwentry.h>
35 
36 #include "module.h"
37 #include "qmail.h"
38 #include "cvm-vmailmgr.h"
39 
40 static str account;
41 static str baseuser;
42 
43 /* Results from looking up the user */
44 struct qmail_user vmuser;
45 
lookup_reinit(void)46 int lookup_reinit(void)
47 {
48   return 0;
49 }
50 
lookup_init(void)51 int lookup_init(void)
52 {
53   if (!str_truncate(&account, 0) ||
54       !str_truncate(&domain, 0) ||
55       !str_truncate(&baseuser, 0) ||
56       !str_truncate(&virtuser, 0))
57     return CVME_GENERAL;
58 
59   if (qmail_lookup_init() != 0)
60     return CVME_IO;
61 
62   return 0;
63 }
64 
lookup_virtuser(void)65 int lookup_virtuser(void)
66 {
67   int err;
68   int fd;
69   struct cdb cdb;
70 
71   DEBUG("cvm domain = '", cvm_module_credentials[CVM_CRED_DOMAIN].s, "'");
72   switch (qmail_lookup_cvm(&vmuser, &domain, &baseuser, &virtuser)) {
73   case -1:
74     return CVME_IO;
75   case 0:
76     break;
77   default:
78     /* Either the domain was not found, or it was found pointing to a
79      * nonexistant user.  In either case, there is no vmailmgr table to
80      * look up. */
81     cvm_module_fact_uint(CVM_FACT_OUTOFSCOPE, 1);
82     return CVME_PERMFAIL;
83   }
84   if (virtuser.len == 0) {
85     cvm_module_fact_uint(CVM_FACT_OUTOFSCOPE, 1);
86     return CVME_PERMFAIL;
87   }
88 
89   memset(&cdb, 0, sizeof cdb);
90   str_lower(&virtuser);
91   /* Found a virtual user, authenticate it. */
92   if (chdir(vmuser.homedir.s) == -1) return CVME_IO;
93   if ((fd = open(pwfile, O_RDONLY)) == -1) {
94     if (errno == ENOENT) {
95       cvm_module_fact_uint(CVM_FACT_OUTOFSCOPE, 1);
96       return CVME_PERMFAIL;
97     }
98     return CVME_IO;
99   }
100   cdb_init(&cdb, fd);
101   switch (cdb_get(&cdb, &virtuser, &vpwdata)) {
102   case -1:
103     DEBUG("cdb_get returned error", 0, 0);
104     err = CVME_IO;
105     break;
106   case 0:
107     DEBUG("cdb_get failed", 0, 0);
108     /* Only handle the default user when in lookup mode, as
109        authenticating the default user shouldn't happen. */
110     if (cvm_module_lookup_secret != 0) {
111       switch (cdb_get(&cdb, &default_user, &vpwdata)) {
112       case -1:
113 	DEBUG("cdb_get returned error", 0, 0);
114 	err = CVME_IO;
115 	break;
116       case 0:
117 	DEBUG("cdb_get failed", 0, 0);
118 	err = CVME_PERMFAIL;
119 	break;
120       default:
121 	err = 0;
122       }
123     }
124     else
125       err = CVME_PERMFAIL;
126     break;
127   default:
128     err = 0;
129   }
130   cdb_free(&cdb);
131   close(fd);
132   if (err == CVME_PERMFAIL)
133     cvm_module_fact_uint(CVM_FACT_OUTOFSCOPE, 0);
134   return err;
135 }
136