jabberd2  2.2.17
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  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  fseek(f, 0, SEEK_SET);
53 
54  buf = (char *) malloc(sizeof(char) * size);
55 
56  if (fread(buf, 1, size, f) != size || ferror(f)) {
57  log_write(r->log, LOG_ERR, "couldn't read from user table file: %s", strerror(errno));
58  free(buf);
59  fclose(f);
60  return 1;
61  }
62 
63  fclose(f);
64 
65  nad = nad_parse(buf, size);
66  if(nad == NULL) {
67  log_write(r->log, LOG_ERR, "couldn't parse user table");
68  free(buf);
69  return 1;
70  }
71 
72  free(buf);
73 
74  nusers = 0;
75  user = nad_find_elem(nad, 0, -1, "user", 1);
76  while(user >= 0) {
77  name = nad_find_elem(nad, user, -1, "name", 1);
78  secret = nad_find_elem(nad, user, -1, "secret", 1);
79 
80  if(name < 0 || secret < 0 || NAD_CDATA_L(nad, name) <= 0 || NAD_CDATA_L(nad, secret) <= 0) {
81  log_write(r->log, LOG_ERR, "malformed user entry in user table file, skipping");
82  continue;
83  }
84 
85  log_debug(ZONE, "remembering user '%.*s'", NAD_CDATA_L(nad, name), NAD_CDATA(nad, name));
86 
87  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)));
88 
89  nusers++;
90 
91  user = nad_find_elem(nad, user, -1, "user", 0);
92  }
93 
94  nad_free(nad);
95 
96  log_write(r->log, LOG_NOTICE, "loaded user table (%d users)", nusers);
97 
98  r->users_load = time(NULL);
99 
100  return 0;
101 }
102 
104 
105  if(r->users != NULL)
106  xhash_free(r->users);
107  r->users = NULL;
108 
109  return;
110 }