System users (SYS_*) now have proper user numbers.
[citadel.git] / citadel / sysdep.c
index 14e9e3e9d18b454c9e4967d1c549a3691e5a75b4..c09fd2a06292edca76dae0ebbad3997440b76818 100644 (file)
@@ -72,6 +72,7 @@
 #include "ctdl_module.h"
 #include "threads.h"
 #include "user_ops.h"
+#include "control.h"
 
 
 #ifdef DEBUG_MEMORY_LEAKS
@@ -422,8 +423,12 @@ struct CitContext *CreateNewContext(void) {
                CtdlLogPrintf(CTDL_ALERT, "citserver: can't allocate memory!!\n");
                return NULL;
        }
-       memset(me, 0, sizeof(struct CitContext));
 
+       memset(me, 0, sizeof(struct CitContext));
+       
+       /* Give the contaxt a name. Hopefully makes it easier to track */
+       strcpy (me->user.fullname, "SYS_notauth");
+       
        /* The new context will be created already in the CON_EXECUTING state
         * in order to prevent another thread from grabbing it while it's
         * being set up.
@@ -468,9 +473,10 @@ struct CitContext *CtdlGetContextArray(int *count)
 
 
 /**
- * This function returns a private context with the user filled in correctly
+ * This function fills in a context and its user field correctly
+ * Then creates/loads that user
  */
-void CtdlFillPrivateContext(struct CitContext *context, char *name)
+void CtdlFillSystemContext(struct CitContext *context, char *name)
 {
        char sysname[USERNAME_SIZE];
 
@@ -479,10 +485,19 @@ void CtdlFillPrivateContext(struct CitContext *context, char *name)
        context->cs_pid = 0;
        strcpy (sysname, "SYS_");
        strcat (sysname, name);
-       if (getuser(&(context->user), sysname))
-       {
-               strcpy(context->user.fullname, sysname);
+       /* internal_create_user has the side effect of loading the user regardless of wether they
+        * already existed or needed to be created
+        */
+       internal_create_user (sysname, &(context->user), -1) ;
+       
+       /* Check to see if the system user needs upgrading */
+       if (context->user.usernum == 0)
+       {       /* old system user with number 0, upgrade it */
+               context->user.usernum = get_new_user_number();
+               CtdlLogPrintf(CTDL_DEBUG, "Upgrading system user \"%s\" from user number 0 to user number %d\n", context->user.fullname, context->user.usernum);
+               /* add user to the database */
                putuser(&(context->user));
+               cdb_store(CDB_USERSBYNUMBER, &(context->user.usernum), sizeof(long), context->user.fullname, strlen(context->user.fullname)+1);
        }
 }