jabberd2  2.6.1
user.c
Go to the documentation of this file.
1 /*
2  * jabberd - Jabber Open Source Server
3  * Copyright (c) 2002-2003 Jeremie Miller, Thomas Muldowney,
4  * Ryan Eatmon, Robert Norris
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
19  */
20 
21 #include "router.h"
22 
26  const char *userfile;
27  FILE *f;
28  long size;
29  char *buf;
30  nad_t nad;
31  int nusers, user, name, secret;
32 
33  log_debug(ZONE, "loading user table");
34 
35  if(r->users != NULL)
36  xhash_free(r->users);
37 
38  r->users = xhash_new(51);
39 
40  userfile = config_get_one(r->config, "local.users", 0);
41  if(userfile == NULL)
42  userfile = CONFIG_DIR "/router-users.xml";
43 
44  f = fopen(userfile, "rb");
45  if(f == NULL) {
46  log_write(r->log, LOG_ERR, "couldn't open user table file %s: %s", userfile, strerror(errno));
47  return 1;
48  }
49 
50  fseek(f, 0, SEEK_END);
51  size = ftell(f);
52  if(size < 0) {
53  log_write(r->log, LOG_ERR, "couldn't seek user table file %s: %s", userfile, strerror(errno));
54  fclose(f);
55  return 1;
56  }
57  if(size == 0) {
58  log_write(r->log, LOG_ERR, "empty user table file %s", userfile);
59  fclose(f);
60  return 1;
61  }
62  fseek(f, 0, SEEK_SET);
63 
64  buf = (char *) malloc(sizeof(char) * size);
65 
66  if (fread(buf, 1, size, f) != size || ferror(f)) {
67  log_write(r->log, LOG_ERR, "couldn't read from user table file: %s", strerror(errno));
68  free(buf);
69  fclose(f);
70  return 1;
71  }
72 
73  fclose(f);
74 
75  nad = nad_parse(buf, size);
76  if(nad == NULL) {
77  log_write(r->log, LOG_ERR, "couldn't parse user table");
78  free(buf);
79  return 1;
80  }
81 
82  free(buf);
83 
84  nusers = 0;
85  user = nad_find_elem(nad, 0, -1, "user", 1);
86  while(user >= 0) {
87  name = nad_find_elem(nad, user, -1, "name", 1);
88  secret = nad_find_elem(nad, user, -1, "secret", 1);
89 
90  if(name < 0 || secret < 0 || NAD_CDATA_L(nad, name) <= 0 || NAD_CDATA_L(nad, secret) <= 0) {
91  log_write(r->log, LOG_ERR, "malformed user entry in user table file, skipping");
92  continue;
93  }
94 
95  log_debug(ZONE, "remembering user '%.*s'", NAD_CDATA_L(nad, name), NAD_CDATA(nad, name));
96 
97  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)));
98 
99  nusers++;
100 
101  user = nad_find_elem(nad, user, -1, "user", 0);
102  }
103 
104  nad_free(nad);
105 
106  log_write(r->log, LOG_NOTICE, "loaded user table (%d users)", nusers);
107 
108  r->users_load = time(NULL);
109 
110  return 0;
111 }
112 
114 
115  if(r->users != NULL)
116  xhash_free(r->users);
117  r->users = NULL;
118 
119  return;
120 }
Definition: nad.h:93
#define NAD_CDATA_L(N, E)
Definition: nad.h:186
xht users
user table
Definition: router.h:77
void xhash_free(xht h)
Definition: xhash.c:241
void log_write(log_t log, int level, const char *msgfmt,...)
Definition: log.c:104
int user_table_load(router_t r)
user table manager
Definition: user.c:25
time_t users_load
Definition: router.h:78
void nad_free(nad_t nad)
free that nad
Definition: nad.c:180
void user_table_unload(router_t r)
Definition: user.c:113
nad_t nad_parse(const char *buf, int len)
create a nad from raw xml
Definition: nad.c:1430
char * pstrdupx(pool_t p, const char *src, int len)
use given size
Definition: pool.c:205
void xhash_put(xht h, const char *key, void *val)
Definition: xhash.c:163
log_t log
logging
Definition: router.h:85
#define log_debug(...)
Definition: log.h:65
int nad_find_elem(nad_t nad, unsigned int elem, int ns, const char *name, int depth)
locate the next elem at a given depth with an optional matching name
Definition: nad.c:206
pool_t xhash_pool(xht h)
get our pool
Definition: xhash.c:305
#define NAD_CDATA(N, E)
Definition: nad.h:185
#define ZONE
Definition: mio_impl.h:76
const char * config_get_one(config_t c, const char *key, int num)
get config value n for this key
Definition: config.c:278
xht xhash_new(int prime)
Definition: xhash.c:96
config_t config
config
Definition: router.h:74