userdb-vpopmail.c revision 356303df200c991580bd24041996a070ad08c05e
45312f52ff3a3d4c137447be4c7556500c2f8bf2Timo Sirainen/* Copyright (C) 2002-2003 Timo Sirainen */
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen/* Thanks to Courier-IMAP for showing how the vpopmail API should be used */
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen#include "config.h"
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen#undef HAVE_CONFIG_H
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen#if defined(PASSDB_VPOPMAIL) || defined(USERDB_VPOPMAIL)
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen#include "common.h"
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen#include "userdb.h"
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen#include "userdb-vpopmail.h"
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainenstruct vqpasswd *vpopmail_lookup_vqp(struct auth_request *request,
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen char vpop_user[VPOPMAIL_LIMIT],
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen char vpop_domain[VPOPMAIL_LIMIT])
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen{
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen struct vqpasswd *vpw;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen /* vpop_user must be zero-filled or parse_email() leaves an
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen extra character after the user name. we'll fill vpop_domain
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen as well just to be sure... */
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen memset(vpop_user, '\0', VPOPMAIL_LIMIT);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen memset(vpop_domain, '\0', VPOPMAIL_LIMIT);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen if (parse_email(request->user, vpop_user, vpop_domain,
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen VPOPMAIL_LIMIT-1) < 0) {
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen auth_request_log_info(request, "vpopmail",
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen "parse_email() failed");
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen return NULL;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen }
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen vpw = vauth_getpw(vpop_user, vpop_domain);
f7539a17ea306191b53b8f5e752e228937df9ec3Timo Sirainen if (vpw == NULL) {
f7539a17ea306191b53b8f5e752e228937df9ec3Timo Sirainen auth_request_log_info(request, "vpopmail", "unknown user");
f7539a17ea306191b53b8f5e752e228937df9ec3Timo Sirainen return NULL;
f7539a17ea306191b53b8f5e752e228937df9ec3Timo Sirainen }
f7539a17ea306191b53b8f5e752e228937df9ec3Timo Sirainen
f7539a17ea306191b53b8f5e752e228937df9ec3Timo Sirainen return vpw;
f7539a17ea306191b53b8f5e752e228937df9ec3Timo Sirainen}
f7539a17ea306191b53b8f5e752e228937df9ec3Timo Sirainen
f7539a17ea306191b53b8f5e752e228937df9ec3Timo Sirainen#ifdef USERDB_VPOPMAIL
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenstatic void vpopmail_lookup(struct auth_request *auth_request,
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen userdb_callback_t *callback, void *context)
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen{
f7539a17ea306191b53b8f5e752e228937df9ec3Timo Sirainen char vpop_user[VPOPMAIL_LIMIT], vpop_domain[VPOPMAIL_LIMIT];
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen struct vqpasswd *vpw;
f7539a17ea306191b53b8f5e752e228937df9ec3Timo Sirainen struct user_data data;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen uid_t uid;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen gid_t gid;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen pool_t pool;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen vpw = vpopmail_lookup_vqp(auth_request, vpop_user, vpop_domain);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen if (vpw == NULL) {
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen callback(NULL, context);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen return;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen }
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen /* we have to get uid/gid separately, because the gid field in
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen struct vqpasswd isn't really gid at all but just some flags... */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen if (vget_assign(vpop_domain, NULL, 0, &uid, &gid) == NULL) {
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen auth_request_log_info(auth_request, "vpopmail",
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen "vget_assign(%s) failed", vpop_domain);
fea7b8b3fc182e415b1875d79587c0aa1adb09d7Timo Sirainen callback(NULL, context);
fea7b8b3fc182e415b1875d79587c0aa1adb09d7Timo Sirainen return;
fea7b8b3fc182e415b1875d79587c0aa1adb09d7Timo Sirainen }
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
fea7b8b3fc182e415b1875d79587c0aa1adb09d7Timo Sirainen if (vpw->pw_dir == NULL || vpw->pw_dir[0] == '\0') {
fea7b8b3fc182e415b1875d79587c0aa1adb09d7Timo Sirainen /* user's homedir doesn't exist yet, create it */
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen auth_request_log_info(auth_request, "vpopmail",
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen "pw_dir isn't set, creating");
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen if (make_user_dir(vpop_user, vpop_domain, uid, gid) == NULL) {
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen auth_request_log_error(auth_request, "vpopmail",
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen "make_user_dir(%s, %s) failed",
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen vpop_user, vpop_domain);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen callback(NULL, context);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen return;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen }
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen /* get the user again so pw_dir is visible */
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen vpw = vauth_getpw(vpop_user, vpop_domain);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen if (vpw == NULL) {
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen callback(NULL, context);
c72d5f1aa95544dbfbc66d72bfa3205f8876d91cTimo Sirainen return;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen }
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen }
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen memset(&data, 0, sizeof(data));
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen data.uid = uid;
ab8cf8fb93579981a687942e53bdb361b3b61bafTimo Sirainen data.gid = gid;
ab8cf8fb93579981a687942e53bdb361b3b61bafTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen data.virtual_user = vpw->pw_name;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen data.home = vpw->pw_dir;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen callback(&data, context);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen}
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainenstruct userdb_module userdb_vpopmail = {
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen "vpopmail",
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen NULL, NULL, NULL,
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen vpopmail_lookup
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen};
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen#endif
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen#endif
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen