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