LOOK AT ME I AM COMMITTING AGAIN
[citadel.git] / citadel / user_ops.c
index 3e0791f4a159c187d4ba2141808b2e662df99742..1e8792d71d6a0cb9681c9bad800650a4da28e427 100644 (file)
  */
 
 #include "sysdep.h"
-#include <errno.h>
-#include <stdlib.h>
-#include <unistd.h>
 #include <stdio.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <pwd.h>
-#include <ctype.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <syslog.h>
-#ifdef HAVE_SYS_STAT_H
-#include <sys/stat.h>
-#endif
-
-#if TIME_WITH_SYS_TIME
-# include <sys/time.h>
-# include <time.h>
-#else
-# if HAVE_SYS_TIME_H
-#  include <sys/time.h>
-# else
-#  include <time.h>
-# endif
-#endif
-
-#include <string.h>
-#include <limits.h>
 #include <libcitadel.h>
-#include "auth.h"
-#include "citadel.h"
-#include "server.h"
-#include "database.h"
-#include "sysdep_decls.h"
-#include "support.h"
-#include "room_ops.h"
-#include "file_ops.h"
+
 #include "control.h"
-#include "msgbase.h"
-#include "config.h"
+#include "support.h"
 #include "citserver.h"
-#include "citadel_dirs.h"
-#include "genstamp.h"
-#include "threads.h"
+#include "config.h"
 #include "citadel_ldap.h"
-#include "context.h"
 #include "ctdl_module.h"
 #include "user_ops.h"
 #include "internet_addressing.h"
@@ -68,17 +30,6 @@ int chkpwd_write_pipe[2];
 int chkpwd_read_pipe[2];
 
 
-
-/*
- * getuser()  -  retrieve named user into supplied buffer.
- *            returns 0 on success
- */
-int getuser(struct ctdluser *usbuf, char name[])
-{
-       return CtdlGetUser(usbuf, name);
-}
-
-
 /*
  * CtdlGetUser()  -  retrieve named user into supplied buffer.
  *            returns 0 on success
@@ -115,6 +66,12 @@ int CtdlGetUser(struct ctdluser *usbuf, char *name)
        return CtdlGetUserLen(usbuf, name, cutuserkey(name));
 }
 
+int CtdlLockGetCurrentUser(void)
+{
+       CitContext *CCC = CC;
+
+       return CtdlGetUserLen(&CCC->user, CCC->curr_user, cutuserkey(CCC->curr_user));
+}
 
 /*
  * CtdlGetUserLock()  -  same as getuser() but locks the record
@@ -131,15 +88,6 @@ int CtdlGetUserLock(struct ctdluser *usbuf, char *name)
 }
 
 
-/*
- * lgetuser()  -  same as getuser() but locks the record
- */
-int lgetuser(struct ctdluser *usbuf, char *name)
-{
-       return CtdlGetUserLock(usbuf, name);
-}
-
-
 /*
  * CtdlPutUser()  -  write user buffer into the correct place on disk
  */
@@ -158,13 +106,9 @@ void CtdlPutUser(struct ctdluser *usbuf)
 
 }
 
-
-/*
- * putuser()  -  write user buffer into the correct place on disk
- */
-void putuser(struct ctdluser *usbuf)
+void CtdlPutCurrentUserLock()
 {
-       CtdlPutUser(usbuf);
+       CtdlPutUser(&CC->user);
 }
 
 
@@ -178,14 +122,6 @@ void CtdlPutUserLock(struct ctdluser *usbuf)
 }
 
 
-/*
- * lputuser()  -  same as putuser() but locks the record
- */
-void lputuser(struct ctdluser *usbuf)
-{
-       CtdlPutUserLock(usbuf);
-}
-
 
 /*
  * rename_user()  -  this is tricky because the user's display name is the database key
@@ -379,7 +315,7 @@ int CtdlCheckInternetMailPermission(struct ctdluser *who) {
        if (who->axlevel <= AxProbU) return(0);
 
        /* Globally enabled? */
-       if (config.c_restrict == 0) return(1);
+       if (CtdlGetConfigInt("c_restrict") == 0) return(1);
 
        /* User flagged ok? */
        if (who->flags & US_INTERNET) return(2);
@@ -391,6 +327,47 @@ int CtdlCheckInternetMailPermission(struct ctdluser *who) {
        return(0);
 }
 
+/*
+ * Convenience function.
+ */
+int CtdlAccessCheck(int required_level)
+{
+       if (CC->internal_pgm) return(0);
+       if (required_level >= ac_internal) {
+               cprintf("%d This is not a user-level command.\n",
+                       ERROR + HIGHER_ACCESS_REQUIRED);
+               return(-1);
+       }
+
+       if ((required_level >= ac_logged_in_or_guest) && (CC->logged_in == 0) && (CtdlGetConfigInt("c_guest_logins") == 0)) {
+               cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
+               return(-1);
+       }
+
+       if ((required_level >= ac_logged_in) && (CC->logged_in == 0)) {
+               cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
+               return(-1);
+       }
+
+       if (CC->user.axlevel >= AxAideU) return(0);
+       if (required_level >= ac_aide) {
+               cprintf("%d This command requires Admin access.\n",
+                       ERROR + HIGHER_ACCESS_REQUIRED);
+               return(-1);
+       }
+
+       if (is_room_aide()) return(0);
+       if (required_level >= ac_room_aide) {
+               cprintf("%d This command requires Admin or Room Admin access.\n",
+                       ERROR + HIGHER_ACCESS_REQUIRED);
+               return(-1);
+       }
+
+       /* shhh ... succeed quietly */
+       return(0);
+}
+
+
 
 /*
  * Is the user currently logged in an Admin?
@@ -445,19 +422,6 @@ int CtdlGetUserByNumber(struct ctdluser *usbuf, long number)
        return(r);
 }
 
-/*
- * getuserbynumber() - get user by number
- *                     returns 0 if user was found
- *
- * Note: fetching a user this way requires one additional database operation.
- */
-int getuserbynumber(struct ctdluser *usbuf, long number)
-{
-       return CtdlGetUserByNumber(usbuf, number);
-}
-
-
-
 /*
  * Helper function for rebuild_usersbynumber()
  */
@@ -565,10 +529,12 @@ int CtdlLoginExistingUser(char *authname, const char *trythisname)
 
        /* If a "master user" is defined, handle its authentication if specified */
        CC->is_master = 0;
-       if (strlen(config.c_master_user) > 0) if (strlen(config.c_master_pass) > 0) if (authname) {
-               if (!strcasecmp(authname, config.c_master_user)) {
-                       CC->is_master = 1;
-               }
+       if (    (!IsEmptyStr(CtdlGetConfigStr("c_master_user"))) && 
+               (!IsEmptyStr(CtdlGetConfigStr("c_master_pass"))) &&
+               (authname != NULL) &&
+               (!strcasecmp(authname, CtdlGetConfigStr("c_master_user"))) )
+       {
+               CC->is_master = 1;
        }
 
        /* Continue attempting user validation... */
@@ -580,7 +546,7 @@ int CtdlLoginExistingUser(char *authname, const char *trythisname)
                return login_not_found;
        }
 
-       if (config.c_auth_mode == AUTHMODE_HOST) {
+       if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
 
                /* host auth mode */
 
@@ -621,7 +587,7 @@ int CtdlLoginExistingUser(char *authname, const char *trythisname)
        }
 
 #ifdef HAVE_LDAP
-       else if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
+       else if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
        
                /* LDAP auth mode */
 
@@ -629,7 +595,7 @@ int CtdlLoginExistingUser(char *authname, const char *trythisname)
                char ldap_cn[256];
                char ldap_dn[256];
 
-               found_user = CtdlTryUserLDAP(username, ldap_dn, sizeof ldap_dn, ldap_cn, sizeof ldap_cn, &ldap_uid);
+               found_user = CtdlTryUserLDAP(username, ldap_dn, sizeof ldap_dn, ldap_cn, sizeof ldap_cn, &ldap_uid, 0);
                if (found_user != 0) {
                        return login_not_found;
                }
@@ -651,7 +617,7 @@ int CtdlLoginExistingUser(char *authname, const char *trythisname)
        else {
                /* native auth mode */
 
-               struct recptypes *valid = NULL;
+               recptypes *valid = NULL;
        
                /* First, try to log in as if the supplied name is a display name */
                found_user = CtdlGetUser(&CC->user, username);
@@ -705,14 +671,14 @@ void do_login(void)
        /* If this user's name is the name of the system administrator
         * (as specified in setup), automatically assign access level 6.
         */
-       if (!strcasecmp(CCC->user.fullname, config.c_sysadm)) {
+       if (!strcasecmp(CCC->user.fullname, CtdlGetConfigStr("c_sysadm"))) {
                CCC->user.axlevel = AxAideU;
        }
 
        /* If we're authenticating off the host system, automatically give
         * root the highest level of access.
         */
-       if (config.c_auth_mode == AUTHMODE_HOST) {
+       if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
                if (CCC->user.uid == 0) {
                        CCC->user.axlevel = AxAideU;
                }
@@ -726,7 +692,7 @@ void do_login(void)
         * the vCard module's login hook runs.
         */
        snprintf(CCC->cs_inet_email, sizeof CCC->cs_inet_email, "%s@%s",
-               CCC->user.fullname, config.c_fqdn);
+               CCC->user.fullname, CtdlGetConfigStr("c_fqdn"));
        convert_spaces_to_underscores(CCC->cs_inet_email);
 
        /* Create any personal rooms required by the system.
@@ -741,7 +707,7 @@ void do_login(void)
        PerformSessionHooks(EVT_LOGIN);
 
        /* Enter the lobby */
-       CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
+       CtdlUserGoto(CtdlGetConfigStr("c_baseroom"), 0, 0, NULL, NULL, NULL, NULL);
 }
 
 
@@ -764,21 +730,6 @@ void CtdlUserLogout(void)
                   CCC->curr_user, CCC->cs_pid
        );
 
-       /*
-        * If there is a download in progress, abort it.
-        */
-       if (CCC->download_fp != NULL) {
-               fclose(CCC->download_fp);
-               CCC->download_fp = NULL;
-       }
-
-       /*
-        * If there is an upload in progress, abort it.
-        */
-       if (CCC->upload_fp != NULL) {
-               abort_upl(CCC);
-       }
-
        /* Run any hooks registered by modules... */
        PerformSessionHooks(EVT_LOGOUT);
        
@@ -927,10 +878,10 @@ int CtdlTryPassword(const char *password, long len)
        }
 
        if (CCC->is_master) {
-               code = strcmp(password, config.c_master_pass);
+               code = strcmp(password, CtdlGetConfigStr("c_master_pass"));
        }
 
-       else if (config.c_auth_mode == AUTHMODE_HOST) {
+       else if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
 
                /* host auth mode */
 
@@ -960,7 +911,7 @@ int CtdlTryPassword(const char *password, long len)
        }
 
 #ifdef HAVE_LDAP
-       else if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
+       else if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
 
                /* LDAP auth mode */
 
@@ -1102,7 +1053,7 @@ int internal_create_user (const char *username, long len, struct ctdluser *usbuf
 
        usbuf->timescalled = 0;
        usbuf->posted = 0;
-       usbuf->axlevel = config.c_initax;
+       usbuf->axlevel = CtdlGetConfigInt("c_initax");
        usbuf->lastcall = time(NULL);
 
        /* fetch a new user number */
@@ -1139,7 +1090,7 @@ int create_user(const char *newusername, long len, int become_user)
        strproc(username);
 
        
-       if (config.c_auth_mode == AUTHMODE_HOST) {
+       if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
 
                /* host auth mode */
 
@@ -1171,8 +1122,8 @@ int create_user(const char *newusername, long len, int become_user)
        }
 
 #ifdef HAVE_LDAP
-       if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
-               if (CtdlTryUserLDAP(username, NULL, 0, username, sizeof username, &uid) != 0) {
+       if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
+               if (CtdlTryUserLDAP(username, NULL, 0, username, sizeof username, &uid, 0) != 0) {
                        return(ERROR + NO_SUCH_USER);
                }
        }
@@ -1289,7 +1240,7 @@ int CtdlForgetThisRoom(void) {
        visit vbuf;
 
        /* On some systems, Admins are not allowed to forget rooms */
-       if (is_aide() && (config.c_aide_zap == 0)
+       if (is_aide() && (CtdlGetConfigInt("c_aide_zap") == 0)
           && ((CC->room.QRflags & QR_MAILBOX) == 0)  ) {
                return(1);
        }
@@ -1304,7 +1255,7 @@ int CtdlForgetThisRoom(void) {
        CtdlPutUserLock(&CC->user);
 
        /* Return to the Lobby, so we don't end up in an undefined room */
-       CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
+       CtdlUserGoto(CtdlGetConfigStr("c_baseroom"), 0, 0, NULL, NULL, NULL, NULL);
        return(0);
 
 }