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