1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
/*
* jabberd - Jabber Open Source Server
* Copyright (c) 2002 Jeremie Miller, Thomas Muldowney,
* Ryan Eatmon, Robert Norris
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
*/
#include "sm.h"
/** @file sm/user.c
* @brief user management
* @author Robert Norris
* $Date: 2005/06/02 04:48:25 $
* $Revision: 1.23 $
*/
/** make a new one */
static user_t _user_alloc(sm_t sm, jid_t jid) {
pool_t p;
user_t user;
p = pool_new();
user = (user_t) pmalloco(p, sizeof(struct user_st));
user->p = p;
user->sm = sm;
user->jid = jid_dup(jid);
pool_cleanup(p, (void (*)(void *)) jid_free, user->jid);
/* a place for modules to store stuff */
user->module_data = (void **) pmalloco(p, sizeof(void *) * sm->mm->nindex);
return user;
}
/** fetch user data */
user_t user_load(sm_t sm, jid_t jid) {
user_t user;
/* already loaded */
user = xhash_get(sm->users, jid_user(jid));
if(user != NULL) {
log_debug(ZONE, "returning previously-created user data for %s", jid_user(jid));
return user;
}
/* make a new one */
user = _user_alloc(sm, jid);
/* get modules to setup */
if(mm_user_load(sm->mm, user) != 0) {
log_debug(ZONE, "modules failed user load for %s", jid_user(jid));
pool_free(user->p);
return NULL;
}
/* save them for later */
xhash_put(sm->users, jid_user(user->jid), (void *) user);
log_debug(ZONE, "loaded user data for %s", jid_user(jid));
return user;
}
void user_free(user_t user) {
log_debug(ZONE, "freeing user %s", jid_user(user->jid));
xhash_zap(user->sm->users, jid_user(user->jid));
pool_free(user->p);
}
/** initialise a user */
int user_create(sm_t sm, jid_t jid) {
user_t user;
log_debug(ZONE, "create user request for %s", jid_user(jid));
/* check whether it is to serviced domain */
if(xhash_get(sm->hosts, jid->domain) == NULL) {
log_write(sm->log, LOG_ERR, "request to create user for non-serviced domain: jid=%s", jid_user(jid));
log_debug(ZONE, "no such domain, not creating");
return 1;
}
user = user_load(sm, jid);
if(user != NULL) {
log_write(sm->log, LOG_ERR, "request to create already-active user: jid=%s", jid_user(jid));
log_debug(ZONE, "user already active, not creating");
return 1;
}
/* modules create */
if(mm_user_create(sm->mm, jid) != 0) {
log_write(sm->log, LOG_ERR, "user creation failed: jid=%s", jid_user(jid));
log_debug(ZONE, "user create failed, forcing deletion for cleanup");
mm_user_delete(sm->mm, jid);
return 1;
}
log_write(sm->log, LOG_NOTICE, "created user: jid=%s", jid_user(jid));
return 0;
}
/** trash a user */
void user_delete(sm_t sm, jid_t jid) {
user_t user;
sess_t scan, next;
log_debug(ZONE, "delete user request for %s", jid_user(jid));
user = user_load(sm, jid);
if(user == NULL) {
log_debug(ZONE, "user doesn't exist, can't delete");
return;
}
/* close their sessions first (this will free user, after the last session ends) */
scan = user->sessions;
while(scan != NULL) {
next = scan->next;
sm_c2s_action(scan, "ended", NULL);
sess_end(scan);
scan = next;
}
mm_user_delete(sm->mm, jid);
log_write(sm->log, LOG_NOTICE, "deleted user: jid=%s", jid_user(jid));
}
|