]> jfr.im git - irc/quakenet/newserv.git/blob - authext/authext.c
A4STATS: remove E style escapes and switch to createtable for indices
[irc/quakenet/newserv.git] / authext / authext.c
1 #include "authext.h"
2 #include "../core/nsmalloc.h"
3 #include "../core/error.h"
4 #include "../lib/sstring.h"
5 #include "../lib/irc_string.h"
6 #include "../nick/nick.h"
7 #include "../core/hooks.h"
8 #include "../lib/strlfunc.h"
9 #include "../lib/version.h"
10 #include "../lib/ccassert.h"
11
12 #include <string.h>
13 #include <stdio.h>
14
15 MODULE_VERSION("")
16
17 /* checking to see that u_int64_t == unsigned long long for strtoull */
18 CCASSERT(sizeof(unsigned long long) == sizeof(u_int64_t))
19
20 #define authnamehash(x) ((x)%AUTHNAMEHASHSIZE)
21 #define authnamehashbyname(x) (irc_crc32i(x)%AUTHNAMEHASHSIZE)
22
23 authname *authnametable[AUTHNAMEHASHSIZE];
24
25 /* internal access only */
26 static authname *authnametablebyname[AUTHNAMEHASHSIZE];
27
28 static struct {
29 sstring *name;
30 int persistent;
31 } authnameexts[MAXAUTHNAMEEXTS];
32
33 static void authextstats(int hooknum, void *arg);
34
35 void _init(void) {
36 memset(authnametable,0,sizeof(authnametable));
37 memset(authnametablebyname,0,sizeof(authnametablebyname));
38 registerhook(HOOK_CORE_STATSREQUEST, &authextstats);
39 }
40
41 void _fini(void) {
42 deregisterhook(HOOK_CORE_STATSREQUEST, &authextstats);
43 nsfreeall(POOL_AUTHEXT);
44 }
45
46 authname *newauthname(void) {
47 return nsmalloc(POOL_AUTHEXT, sizeof(authname));
48 }
49
50 void freeauthname (authname *anp) {
51 nsfree(POOL_AUTHEXT, anp);
52 }
53
54 int registerauthnameext(const char *name, int persistent) {
55 int i;
56
57 if (findauthnameext(name)!=-1) {
58 Error("nick",ERR_WARNING,"Tried to register duplicate authname extension %s",name);
59 return -1;
60 }
61
62 for (i=0;i<MAXAUTHNAMEEXTS;i++) {
63 if (authnameexts[i].name==NULL) {
64 authnameexts[i].name=getsstring(name,100);
65 authnameexts[i].persistent=persistent;
66 return i;
67 }
68 }
69
70 Error("nick",ERR_WARNING,"Tried to register too many authname extensions: %s",name);
71 return -1;
72 }
73
74 int findauthnameext(const char *name) {
75 int i;
76
77 for (i=0;i<MAXAUTHNAMEEXTS;i++) {
78 if (authnameexts[i].name!=NULL && !ircd_strcmp(name,authnameexts[i].name->content)) {
79 return i;
80 }
81 }
82
83 return -1;
84 }
85
86 void releaseauthnameext(int index) {
87 int i;
88 authname *anp;
89
90 freesstring(authnameexts[index].name);
91 authnameexts[index].name=NULL;
92
93 for (i=0;i<AUTHNAMEHASHSIZE;i++) {
94 for (anp=authnametable[i];anp;anp=anp->next) {
95 anp->exts[index]=NULL;
96 }
97 }
98
99 /* the contents of authnametablebyname should be identical */
100 }
101
102 authname *findauthname(unsigned long userid) {
103 authname *anp;
104
105 if(!userid)
106 return NULL;
107
108 for (anp=authnametable[authnamehash(userid)];anp;anp=(authname *)anp->next)
109 if (userid==anp->userid)
110 return anp;
111
112 return NULL;
113 }
114
115 authname *findauthnamebyname(const char *name) {
116 authname *anp;
117
118 if(!name)
119 return NULL;
120
121 for (anp=authnametablebyname[authnamehashbyname(name)];anp;anp=(authname *)anp->nextbyname)
122 if (!ircd_strcmp(anp->name, name))
123 return anp;
124
125 return NULL;
126 }
127
128 authname *findorcreateauthname(unsigned long userid, const char *name) {
129 authname *anp;
130 unsigned int thehash=authnamehash(userid), secondhash = authnamehashbyname(name);
131
132 if(!userid || !name)
133 return NULL;
134
135 for (anp=authnametable[thehash];anp;anp=(authname *)anp->next)
136 if (userid==anp->userid)
137 return anp;
138
139 anp=newauthname();
140 anp->userid=userid;
141 strlcpy(anp->name, name, sizeof(anp->name));
142 anp->usercount=0;
143 anp->marker=0;
144 anp->flags=0;
145 anp->nicks=NULL;
146 memset(anp->exts, 0, MAXAUTHNAMEEXTS * sizeof(void *));
147 anp->next=(struct authname *)authnametable[thehash];
148 authnametable[thehash]=anp;
149
150 anp->namebucket=secondhash;
151 anp->nextbyname=(struct authname *)authnametablebyname[secondhash];
152 authnametablebyname[secondhash]=anp;
153
154 return anp;
155 }
156
157 void releaseauthname(authname *anp) {
158 authname **manp;
159 int i, found;
160 if (anp->usercount==0) {
161 anp->nicks = NULL;
162
163 for(i=0;i<MAXAUTHNAMEEXTS;i++)
164 if(authnameexts[i].persistent && anp->exts[i]!=NULL)
165 return;
166
167 triggerhook(HOOK_AUTH_LOSTAUTHNAME, (void *)anp);
168 found = 0;
169 for(manp=&(authnametable[authnamehash(anp->userid)]);*manp;manp=(authname **)&((*manp)->next)) {
170 if ((*manp)==anp) {
171 (*manp)=(authname *)anp->next;
172 found = 1;
173 break;
174 }
175 }
176 if(!found) {
177 Error("nick",ERR_ERROR,"Unable to remove authname %lu from hashtable",anp->userid);
178 return;
179 }
180
181 for(manp=&(authnametablebyname[anp->namebucket]);*manp;manp=(authname **)&((*manp)->nextbyname)) {
182 if ((*manp)==anp) {
183 (*manp)=(authname *)anp->nextbyname;
184 freeauthname(anp);
185 return;
186 }
187 }
188
189 Error("nick",ERR_STOP,"Unable to remove authname %lu from byname hashtable, TABLES ARE INCONSISTENT -- DYING",anp->userid);
190 }
191 }
192
193 unsigned int nextauthnamemarker(void) {
194 int i;
195 authname *anp;
196 static unsigned int authnamemarker=0;
197
198 authnamemarker++;
199 if (!authnamemarker) {
200 /* If we wrapped to zero, zap the marker on all records */
201 for (i=0;i<AUTHNAMEHASHSIZE;i++)
202 for (anp=authnametable[i];anp;anp=anp->next)
203 anp->marker=0;
204 authnamemarker++;
205 }
206
207 return authnamemarker;
208 }
209
210 authname *getauthbyname(const char *name) {
211 authname *a = findauthnamebyname(name);
212 if(!a || !a->nicks)
213 return NULL;
214
215 return a;
216 }
217
218 static char *genstats(authname **hashtable, authname *(nextfn)(authname *)) {
219 int i,curchain,maxchain=0,total=0,buckets=0;
220 authname *ap;
221 static char buf[100];
222
223 for (i=0;i<AUTHNAMEHASHSIZE;i++) {
224 if (hashtable[i]!=NULL) {
225 buckets++;
226 curchain=0;
227 for (ap=hashtable[i];ap;ap=nextfn(ap)) {
228 total++;
229 curchain++;
230 }
231 if (curchain>maxchain) {
232 maxchain=curchain;
233 }
234 }
235 }
236
237 snprintf(buf, sizeof(buf), "%6d authexts (HASH: %6d/%6d, chain %3d)",total,buckets,AUTHNAMEHASHSIZE,maxchain);
238 return buf;
239 }
240
241 static authname *nextbynext(authname *in) {
242 return in->next;
243 }
244
245 static authname *nextbyname(authname *in) {
246 return in->nextbyname;
247 }
248
249 static void authextstats(int hooknum, void *arg) {
250 long level=(long)arg;
251 char buf[100];
252
253 if (level>5) {
254 /* Full stats */
255 snprintf(buf,sizeof(buf),"Authext : by id: %s", genstats(authnametable, nextbynext));
256 triggerhook(HOOK_CORE_STATSREPLY,buf);
257
258 snprintf(buf,sizeof(buf),"Authext : by name: %s", genstats(authnametablebyname, nextbyname));
259 triggerhook(HOOK_CORE_STATSREPLY,buf);
260 }
261 }
262