jabberd2  2.2.16
router/user.c
Go to the documentation of this file.
00001 /*
00002  * jabberd - Jabber Open Source Server
00003  * Copyright (c) 2002-2003 Jeremie Miller, Thomas Muldowney,
00004  *                         Ryan Eatmon, Robert Norris
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
00019  */
00020 
00021 #include "router.h"
00022 
00025 int user_table_load(router_t r) {
00026     char *userfile;
00027     FILE *f;
00028     long size;
00029     char *buf;
00030     nad_t nad;
00031     int nusers, user, name, secret;
00032 
00033     log_debug(ZONE, "loading user table");
00034     
00035     if(r->users != NULL)
00036         xhash_free(r->users);
00037 
00038     r->users = xhash_new(51);
00039 
00040     userfile = config_get_one(r->config, "local.users", 0);
00041     if(userfile == NULL)
00042         userfile = CONFIG_DIR "/router-users.xml";
00043 
00044     f = fopen(userfile, "rb");
00045     if(f == NULL) {
00046         log_write(r->log, LOG_ERR, "couldn't open user table file %s: %s", userfile, strerror(errno));
00047         return 1;
00048     }
00049 
00050     fseek(f, 0, SEEK_END);
00051     size = ftell(f);
00052     fseek(f, 0, SEEK_SET);
00053 
00054     buf = (char *) malloc(sizeof(char) * size);
00055 
00056     if (fread(buf, 1, size, f) != size || ferror(f)) {
00057         log_write(r->log, LOG_ERR, "couldn't read from user table file: %s", strerror(errno));
00058         free(buf);
00059         fclose(f);
00060         return 1;
00061     }
00062 
00063     fclose(f);
00064 
00065     nad = nad_parse(buf, size);
00066     if(nad == NULL) {
00067         log_write(r->log, LOG_ERR, "couldn't parse user table");
00068         free(buf);
00069         return 1;
00070     }
00071 
00072     free(buf);
00073 
00074     nusers = 0;
00075     user = nad_find_elem(nad, 0, -1, "user", 1);
00076     while(user >= 0) {
00077         name = nad_find_elem(nad, user, -1, "name", 1);
00078         secret = nad_find_elem(nad, user, -1, "secret", 1);
00079 
00080         if(name < 0 || secret < 0 || NAD_CDATA_L(nad, name) <= 0 || NAD_CDATA_L(nad, secret) <= 0) {
00081             log_write(r->log, LOG_ERR, "malformed user entry in user table file, skipping");
00082             continue;
00083         }
00084 
00085         log_debug(ZONE, "remembering user '%.*s'", NAD_CDATA_L(nad, name), NAD_CDATA(nad, name));
00086 
00087         xhash_put(r->users, pstrdupx(xhash_pool(r->users), NAD_CDATA(nad, name), NAD_CDATA_L(nad, name)), pstrdupx(xhash_pool(r->users), NAD_CDATA(nad, secret), NAD_CDATA_L(nad, secret)));
00088 
00089         nusers++;
00090         
00091         user = nad_find_elem(nad, user, -1, "user", 0);
00092     }
00093 
00094     nad_free(nad);
00095 
00096     log_write(r->log, LOG_NOTICE, "loaded user table (%d users)", nusers);
00097 
00098     r->users_load = time(NULL);
00099 
00100     return 0;
00101 }
00102 
00103 void user_table_unload(router_t r) {
00104 
00105     if(r->users != NULL)
00106         xhash_free(r->users);
00107     r->users = NULL;
00108 
00109     return;
00110 }