random boilerplate
[citadel.git] / citadel / control.c
index 24b643229edca506347d1dbbebf9c9a0e28b8794..22f6625582926adf20264dac06bfc6e3754b6e06 100644 (file)
@@ -1,16 +1,11 @@
-/*
- * This module handles states which are global to the entire server.
- *
- * Copyright (c) 1987-2015 by the citadel.org team
- *
- * This program is open source software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 3.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- */
+//
+// This module handles states which are global to the entire server.
+//
+// Copyright (c) 1987-2021 by the citadel.org team
+//
+// This program is open source software.  Use, duplication, or disclosure
+// is subject to the terms of the GNU General Public License, version 3.
+// The program is distributed without any warranty, expressed or implied.
 
 #include <stdio.h>
 #include <sys/file.h>
@@ -24,7 +19,9 @@
 long control_highest_user = 0;
 
 /*
- * This is the control record for the message base... 
+ * This is the legacy "control record" format for the message base.  If found
+ * on disk, its contents will be migrated into the system configuration.  Never
+ * change this.
  */
 struct legacy_ctrl_format {
        long MMhighest;                 /* highest message number in file   */
@@ -38,89 +35,88 @@ struct legacy_ctrl_format {
 };
 
 
+/*
+ * data that gets passed back and forth between control_find_highest() and its caller
+ */
+struct cfh {
+       long highest_roomnum_found;
+       long highest_msgnum_found;
+};
+
 
 /*
- * callback to get highest room number when rebuilding control file
+ * Callback to get highest room number when rebuilding message base metadata
+ *
+ * sanity_diag_mode (can be set by -s flag at startup) may be:
+ * 0 = attempt to fix inconsistencies
+ * 1 = show inconsistencies but don't repair them, exit after complete
+ * 2 = show inconsistencies but don't repair them, continue execution
  */
-void control_find_highest(struct ctdlroom *qrbuf, void *data)
-{
-       struct ctdlroom room;
+void control_find_highest(struct ctdlroom *qrbuf, void *data) {
+       struct cfh *cfh = (struct cfh *)data;
        struct cdbdata *cdbfr;
        long *msglist;
        int num_msgs=0;
        int c;
-       int room_fixed = 0;
-       int message_fixed = 0;
-       
-       if (qrbuf->QRnumber > CtdlGetConfigLong("MMnextroom"))
-       {
-               CtdlSetConfigLong("MMnextroom", qrbuf->QRnumber);
-               room_fixed = 1;
+
+       if (qrbuf->QRnumber > cfh->highest_roomnum_found) {
+               cfh->highest_roomnum_found = qrbuf->QRnumber;
        }
-               
-       CtdlGetRoom (&room, qrbuf->QRname);
-       
+
        /* Load the message list */
-       cdbfr = cdb_fetch(CDB_MSGLISTS, &room.QRnumber, sizeof(long));
+       cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
        if (cdbfr != NULL) {
                msglist = (long *) cdbfr->ptr;
                num_msgs = cdbfr->len / sizeof(long);
-       } else {
+       }
+       else {
                return; /* No messages at all?  No further action. */
        }
 
-       if (num_msgs>0)
-       {
-               for (c=0; c<num_msgs; c++)
-               {
-                       if (msglist[c] > CtdlGetConfigLong("MMhighest"))
-                       {
-                               CtdlSetConfigLong("MMhighest", msglist[c]);
-                               message_fixed = 1;
+       if (num_msgs > 0) {
+               for (c=0; c<num_msgs; c++) {
+                       if (msglist[c] > cfh->highest_msgnum_found) {
+                               cfh->highest_msgnum_found = msglist[c];
                        }
                }
        }
+
        cdb_free(cdbfr);
-       if (room_fixed) {
-               syslog(LOG_INFO, "Control record checking....Fixed room counter\n");
-       }
-       if (message_fixed) {
-               syslog(LOG_INFO, "Control record checking....Fixed message count\n");
-       }
-       return;
 }
 
 
 /*
  * Callback to get highest user number.
  */
-void control_find_user (struct ctdluser *EachUser, void *out_data)
-{
-       int user_fixed = 0;
-       
-       if (EachUser->usernum > CtdlGetConfigLong("MMnextuser"))
-       {
-               CtdlSetConfigLong("MMnextuser", EachUser->usernum);
-               user_fixed = 1;
+void control_find_user(char *username, void *out_data) {
+       struct ctdluser EachUser;
+
+       if (CtdlGetUser(&EachUser, username) != 0) {
+               return;
+       }
+
+       if (EachUser.usernum > CtdlGetConfigLong("MMnextuser")) {
+               syslog(LOG_DEBUG, "control: fixing MMnextuser %ld > %ld , found in %s",
+                       EachUser.usernum, CtdlGetConfigLong("MMnextuser"), EachUser.fullname
+               );
+               if (!sanity_diag_mode) {
+                       CtdlSetConfigLong("MMnextuser", EachUser.usernum);
+               }
        }
-       if(user_fixed)
-               syslog(LOG_INFO, "Control record checking....Fixed user count\n");
 }
 
 
 /*
  * If we have a legacy format control record on disk, import it.
  */
-void migrate_legacy_control_record(void)
-{
+void migrate_legacy_control_record(void) {
        FILE *fp = NULL;
        struct legacy_ctrl_format c;
        memset(&c, 0, sizeof(c));
 
-       fp = fopen(file_citadel_control, "rb+");
+       fp = fopen("citadel.control", "rb+");
        if (fp != NULL) {
-               syslog(LOG_INFO, "Legacy format control record found -- importing to db");
+               syslog(LOG_INFO, "control: legacy format record found -- importing to db");
                fread(&c, sizeof(struct legacy_ctrl_format), 1, fp);
                
                CtdlSetConfigLong(      "MMhighest",                    c.MMhighest);
@@ -132,8 +128,8 @@ void migrate_legacy_control_record(void)
                CtdlSetConfigLong(      "MMfulltext",                   c.MMfulltext);
 
                fclose(fp);
-               if (unlink(file_citadel_control) != 0) {
-                       fprintf(stderr, "Unable to remove legacy control record %s after migrating it.\n", file_citadel_control);
+               if (unlink("citadel.control") != 0) {
+                       fprintf(stderr, "Unable to remove legacy control record after migrating it.\n");
                        fprintf(stderr, "Exiting to prevent data corruption.\n");
                        exit(CTDLEXIT_CONFIG);
                }
@@ -144,13 +140,37 @@ void migrate_legacy_control_record(void)
 /*
  * check_control   -  check the control record has sensible values for message, user and room numbers
  */
-void check_control(void)
-{
-       syslog(LOG_INFO, "Sanity checking the recorded highest message, user, and room numbers\n");
-       CtdlForEachRoom(control_find_highest, NULL);
+void check_control(void) {
+
+       syslog(LOG_INFO, "control: sanity checking the recorded highest message and room numbers");
+       struct cfh cfh;
+       memset(&cfh, 0, sizeof(struct cfh));
+       CtdlForEachRoom(control_find_highest, &cfh);
+
+       if (cfh.highest_roomnum_found > CtdlGetConfigLong("MMnextroom")) {
+               syslog(LOG_DEBUG, "control: fixing MMnextroom %ld > %ld", cfh.highest_roomnum_found, CtdlGetConfigLong("MMnextroom"));
+               if (!sanity_diag_mode) {
+                       CtdlSetConfigLong("MMnextroom", cfh.highest_roomnum_found);
+               }
+       }
+
+       if (cfh.highest_msgnum_found > CtdlGetConfigLong("MMhighest")) {
+               syslog(LOG_DEBUG, "control: fixing MMhighest %ld > %ld", cfh.highest_msgnum_found, CtdlGetConfigLong("MMhighest"));
+               if (!sanity_diag_mode) {
+                       CtdlSetConfigLong("MMhighest", cfh.highest_msgnum_found);
+               }
+       }
+
+       syslog(LOG_INFO, "control: sanity checking the recorded highest user number");
        ForEachUser(control_find_user, NULL);
-}
 
+       syslog(LOG_INFO, "control: sanity checks complete");
+
+       if (sanity_diag_mode == 1) {
+               syslog(LOG_INFO, "control: sanity check diagnostic mode is active - exiting now");
+               abort();
+       }
+}
 
 
 /*
@@ -171,7 +191,7 @@ long get_new_message_number(void)
 /*
  * CtdlGetCurrentMessageNumber()  -  Obtain the current highest message number in the system
  * This provides a quick way to initialise a variable that might be used to indicate
- * messages that should not be processed. EG. a new Sieve script will use this
+ * messages that should not be processed.   For example, an inbox rules script will use this
  * to record determine that messages older than this should not be processed.
  *
  * (Why is this function here?  Can't we just go straight to the config variable it fetches?)
@@ -197,7 +217,6 @@ long get_new_user_number(void)
 }
 
 
-
 /*
  * get_new_room_number()  -  Obtain a new, unique ID to be used for a room.
  */
@@ -213,7 +232,6 @@ long get_new_room_number(void)
 }
 
 
-
 /*
  * Helper function for cmd_conf() to handle boolean values
  */
@@ -227,10 +245,6 @@ int confbool(char *v)
 
 /* 
  * Get or set global configuration options
- *
- * IF YOU ADD OR CHANGE FIELDS HERE, YOU *MUST* DOCUMENT YOUR CHANGES AT:
- * http://www.citadel.org/doku.php/documentation:appproto:system_config
- *
  */
 void cmd_conf(char *argbuf)
 {
@@ -245,7 +259,8 @@ void cmd_conf(char *argbuf)
 
        extract_token(cmd, argbuf, 0, '|', sizeof cmd);
 
-       // CONF GET - retrieve system configuration in legacy format (deprecated)
+       // CONF GET - retrieve system configuration in legacy format
+       // This is deprecated; please do not add fields or change their order.
        if (!strcasecmp(cmd, "GET")) {
                cprintf("%d Configuration...\n", LISTING_FOLLOWS);
                cprintf("%s\n",         CtdlGetConfigStr("c_nodename"));
@@ -307,15 +322,15 @@ void cmd_conf(char *argbuf)
                cprintf("%s\n",         CtdlGetConfigStr("c_journal_dest"));
                cprintf("%s\n",         CtdlGetConfigStr("c_default_cal_zone"));
                cprintf("%d\n",         CtdlGetConfigInt("c_pftcpdict_port"));
-               cprintf("%d\n",         CtdlGetConfigInt("c_managesieve_port"));
+               cprintf("0\n");
                cprintf("%d\n",         CtdlGetConfigInt("c_auth_mode"));
-               cprintf("%s\n",         CtdlGetConfigStr("c_funambol_host"));
-               cprintf("%d\n",         CtdlGetConfigInt("c_funambol_port"));
-               cprintf("%s\n",         CtdlGetConfigStr("c_funambol_source"));
-               cprintf("%s\n",         CtdlGetConfigStr("c_funambol_auth"));
+               cprintf("\n");
+               cprintf("\n");
+               cprintf("\n");
+               cprintf("\n");
                cprintf("%d\n",         CtdlGetConfigInt("c_rbl_at_greeting"));
-               cprintf("%s\n",         CtdlGetConfigStr("c_master_user"));
-               cprintf("%s\n",         CtdlGetConfigStr("c_master_pass"));
+               cprintf("\n");
+               cprintf("\n");
                cprintf("%s\n",         CtdlGetConfigStr("c_pager_program"));
                cprintf("%d\n",         CtdlGetConfigInt("c_imap_keep_from"));
                cprintf("%d\n",         CtdlGetConfigInt("c_xmpp_c2s_port"));
@@ -331,7 +346,8 @@ void cmd_conf(char *argbuf)
                cprintf("000\n");
        }
 
-       // CONF SET - set system configuration in legacy format (really deprecated)
+       // CONF SET - set system configuration in legacy format
+       // This is deprecated; please do not add fields or change their order.
        else if (!strcasecmp(cmd, "SET")) {
                unbuffer_output();
                cprintf("%d Send configuration...\n", SEND_LISTING);
@@ -351,7 +367,7 @@ void cmd_conf(char *argbuf)
                                /* placeholder -- field no longer in use */
                                break;
                        case 4:
-                               CtdlSetConfigInt("c_creataide", atoi(buf));
+                               CtdlSetConfigInt("c_creataide", confbool(buf));
                                break;
                        case 5:
                                CtdlSetConfigInt("c_sleeping", atoi(buf));
@@ -378,10 +394,10 @@ void cmd_conf(char *argbuf)
                                CtdlSetConfigInt("c_restrict", confbool(buf));
                                break;
                        case 12:
-                               CtdlSetConfigInt("c_site_location", confbool(buf));
+                               CtdlSetConfigStr("c_site_location", buf);
                                break;
                        case 13:
-                               CtdlSetConfigInt("c_sysadm", confbool(buf));
+                               CtdlSetConfigStr("c_sysadm", buf);
                                break;
                        case 14:
                                i = atoi(buf);
@@ -512,31 +528,31 @@ void cmd_conf(char *argbuf)
                                CtdlSetConfigInt("c_pftcpdict_port", atoi(buf));
                                break;
                        case 51:
-                               CtdlSetConfigInt("c_managesieve_port", atoi(buf));
+                               /* niu */
                                break;
                        case 52:
                                CtdlSetConfigInt("c_auth_mode", atoi(buf));
                                break;
                        case 53:
-                               CtdlSetConfigStr("c_funambol_host", buf);
+                               /* niu */
                                break;
                        case 54:
-                               CtdlSetConfigInt("c_funambol_port", atoi(buf));
+                               /* niu */
                                break;
                        case 55:
-                               CtdlSetConfigStr("c_funambol_source", buf);
+                               /* niu */
                                break;
                        case 56:
-                               CtdlSetConfigStr("c_funambol_auth", buf);
+                               /* niu */
                                break;
                        case 57:
                                CtdlSetConfigInt("c_rbl_at_greeting", confbool(buf));
                                break;
                        case 58:
-                               CtdlSetConfigStr("c_master_user", buf);
+                               /* niu */
                                break;
                        case 59:
-                               CtdlSetConfigStr("c_master_pass", buf);
+                               /* niu */
                                break;
                        case 60:
                                CtdlSetConfigStr("c_pager_program", buf);
@@ -581,7 +597,7 @@ void cmd_conf(char *argbuf)
                        "The global system configuration has been edited by %s.\n",
                         (CC->logged_in ? CC->curr_user : "an administrator")
                );
-               CtdlAideMessage(buf,"Citadel Configuration Manager Message");
+               CtdlAideMessage(buf, "Citadel Configuration Manager Message");
 
                if (!IsEmptyStr(CtdlGetConfigStr("c_logpages")))
                        CtdlCreateRoom(CtdlGetConfigStr("c_logpages"), 3, "", 0, 1, 1, VIEW_BBS);
@@ -619,22 +635,29 @@ void cmd_conf(char *argbuf)
                extract_token(confname, argbuf, 1, '|', sizeof confname);
                unbuffer_output();
                cprintf("%d %s\n", SEND_LISTING, confname);
-               confptr = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0, 0);
+               confptr = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
                CtdlPutSysConfig(confname, confptr);
                free(confptr);
        }
 
-       else if (!strcasecmp(cmd, "GETVAL")) {
+       // CONF GETVAL - retrieve configuration variables from the database
+       // CONF LOADVAL - same thing but can handle variables bigger than 1 KB
+       else if ( (!strcasecmp(cmd, "GETVAL")) || (!strcasecmp(cmd, "LOADVAL")) ) {
                extract_token(confname, argbuf, 1, '|', sizeof confname);
                char *v = CtdlGetConfigStr(confname);
-               if (v) {
-                       cprintf("%d|%s|\n", CIT_OK, v);
+               if ( (v) && (!strcasecmp(cmd, "GETVAL")) ) {
+                       cprintf("%d %s|\n", CIT_OK, v);
+               }
+               else if ( (v) && (!strcasecmp(cmd, "LOADVAL")) ) {
+                       cprintf("%d %d\n", BINARY_FOLLOWS, (int)strlen(v));
+                       client_write(v, strlen(v));
                }
                else {
-                       cprintf("%d||\n", ERROR);
+                       cprintf("%d |\n", ERROR);
                }
        }
 
+       // CONF PUTVAL - store configuration variables in the database
        else if (!strcasecmp(cmd, "PUTVAL")) {
                if (num_tokens(argbuf, '|') < 3) {
                        cprintf("%d name and value required\n", ERROR);
@@ -647,11 +670,50 @@ void cmd_conf(char *argbuf)
                }
        }
 
+       // CONF STOREVAL - store configuration variables in the database bigger than 1 KB
+       else if (!strcasecmp(cmd, "STOREVAL")) {
+               if (num_tokens(argbuf, '|') < 3) {
+                       cprintf("%d name and length required\n", ERROR);
+               }
+               else {
+                       extract_token(confname, argbuf, 1, '|', sizeof confname);
+                       int bytes = extract_int(argbuf, 2);
+                       char *valbuf = malloc(bytes + 1);
+                       cprintf("%d %d\n", SEND_BINARY, bytes);
+                       client_read(valbuf, bytes);
+                       valbuf[bytes+1] = 0;
+                       CtdlSetConfigStr(confname, valbuf);
+                       free(valbuf);
+               }
+       }
+
+       // CONF LISTVAL - list configuration variables in the database and their values
+       else if (!strcasecmp(cmd, "LISTVAL")) {
+               struct cdbdata *cdbcfg;
+               int keylen = 0;
+               char *key = NULL;
+               char *value = NULL;
+       
+               cprintf("%d all configuration variables\n", LISTING_FOLLOWS);
+               cdb_rewind(CDB_CONFIG);
+               while (cdbcfg = cdb_next_item(CDB_CONFIG), cdbcfg != NULL) {
+                       if (cdbcfg->len < 1020) {
+                               keylen = strlen(cdbcfg->ptr);
+                               key = cdbcfg->ptr;
+                               value = cdbcfg->ptr + keylen + 1;
+                               cprintf("%s|%s\n", key, value);
+                       }
+                       cdb_free(cdbcfg);
+               }
+               cprintf("000\n");
+       }
+
        else {
                cprintf("%d Illegal option(s) specified.\n", ERROR + ILLEGAL_VALUE);
        }
 }
 
+
 typedef struct __ConfType {
        ConstStr Name;
        long Type;
@@ -739,10 +801,6 @@ void cmd_gvdn(char *argbuf)
 /*                      MODULE INITIALIZATION STUFF                          */
 /*****************************************************************************/
 
-void control_cleanup(void)
-{
-       DeleteHash(&CfgNameHash);
-}
 CTDL_MODULE_INIT(control)
 {
        if (!threading) {
@@ -754,8 +812,6 @@ CTDL_MODULE_INIT(control)
 
                CtdlRegisterProtoHook(cmd_gvdn, "GVDN", "get valid domain names");
                CtdlRegisterProtoHook(cmd_conf, "CONF", "get/set system configuration");
-               CtdlRegisterCleanupHook(control_cleanup);
-
        }
        /* return our id for the Log */
        return "control";