]> 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
8 #include <string.h>
9
10 #define ALLOCUNIT 100
11
12 #define authnamehash(x) ((x)%AUTHNAMEHASHSIZE)
13 #define authnamehashbyname(x) (crc32i(x)%AUTHNAMEHASHSIZE)
14
15 authname *freeauthnames;
16 authname *authnametable[AUTHNAMEHASHSIZE];
17
18 /* internal access only */
19 static authname *authnametablebyname[AUTHNAMEHASHSIZE];
20
21 sstring *authnameextnames[MAXAUTHNAMEEXTS];
22
23 void _init(void) {
24 freeauthnames=NULL;
25 memset(authnametable,0,sizeof(authnametable));
26 memset(authnametablebyname,0,sizeof(authnametablebyname));
27 }
28
29 void _fini(void) {
30 nsfreeall(POOL_AUTHEXT);
31 }
32
33 authname *newauthname(void) {
34 authname *anp;
35 int i;
36
37 if (freeauthnames==NULL) {
38 freeauthnames=(authname *)nsmalloc(POOL_AUTHEXT, ALLOCUNIT*sizeof(authname));
39 for (i=0;i<(ALLOCUNIT-1);i++) {
40 freeauthnames[i].next=&(freeauthnames[i+1]);
41 }
42 freeauthnames[ALLOCUNIT-1].next=NULL;
43 }
44
45 anp=freeauthnames;
46 freeauthnames=anp->next;
47
48 return anp;
49 }
50
51 void freeauthname (authname *anp) {
52 anp->next=freeauthnames;
53 freeauthnames=anp;
54 }
55
56 int registerauthnameext(const char *name) {
57 int i;
58
59 if (findauthnameext(name)!=-1) {
60 Error("nick",ERR_WARNING,"Tried to register duplicate authname extension %s",name);
61 return -1;
62 }
63
64 for (i=0;i<MAXAUTHNAMEEXTS;i++) {
65 if (authnameextnames[i]==NULL) {
66 authnameextnames[i]=getsstring(name,100);
67 return i;
68 }
69 }
70
71 Error("nick",ERR_WARNING,"Tried to register too many authname extensions: %s",name);
72 return -1;
73 }
74
75 int findauthnameext(const char *name) {
76 int i;
77
78 for (i=0;i<MAXAUTHNAMEEXTS;i++) {
79 if (authnameextnames[i]!=NULL && !ircd_strcmp(name,authnameextnames[i]->content)) {
80 return i;
81 }
82 }
83
84 return -1;
85 }
86
87 void releaseauthnameext(int index) {
88 int i;
89 authname *anp;
90
91 freesstring(authnameextnames[index]);
92 authnameextnames[index]=NULL;
93
94 for (i=0;i<AUTHNAMEHASHSIZE;i++) {
95 for (anp=authnametable[i];anp;anp=anp->next) {
96 anp->exts[index]=NULL;
97 }
98 }
99
100 /* the contents of authnametablebyname should be identical */
101 }
102
103 authname *findauthname(unsigned long userid) {
104 authname *anp;
105
106 if(!userid)
107 return NULL;
108
109 for (anp=authnametable[authnamehash(userid)];anp;anp=(authname *)anp->next)
110 if (userid==anp->userid)
111 return anp;
112
113 return NULL;
114 }
115
116 authname *findauthnamebyname(const char *name) {
117 authname *anp;
118
119 if(!name)
120 return NULL;
121
122 for (anp=authnametable[authnamehashbyname(name)];anp;anp=(authname *)anp->nextbyname)
123 if (!ircd_strcmp(anp->nicks->authname, name))
124 return anp;
125
126 return NULL;
127 }
128
129 authname *findorcreateauthname(unsigned long userid, const char *name) {
130 authname *anp;
131 unsigned int thehash=authnamehash(userid), secondhash = authnamehashbyname(name);
132
133 if(!userid || !name)
134 return NULL;
135
136 for (anp=authnametable[thehash];anp;anp=(authname *)anp->next)
137 if (userid==anp->userid)
138 return anp;
139
140 anp=newauthname();
141 anp->userid=userid;
142 anp->usercount=0;
143 anp->marker=0;
144 anp->nicks=NULL;
145 memset(anp->exts, 0, MAXAUTHNAMEEXTS * sizeof(void *));
146 anp->next=(struct authname *)authnametable[thehash];
147 authnametable[thehash]=anp;
148
149 anp->namebucket=secondhash;
150 anp->nextbyname=(struct authname *)authnametablebyname[secondhash];
151 authnametablebyname[secondhash]=anp;
152
153 return anp;
154 }
155
156 void releaseauthname(authname *anp) {
157 authname **manp;
158 int i, found;
159 if (anp->usercount==0) {
160 anp->nicks = NULL;
161
162 for(i=0;i<MAXAUTHNAMEEXTS;i++)
163 if(anp->exts[i]!=NULL)
164 return;
165
166 found = 0;
167 for(manp=&(authnametable[authnamehash(anp->userid)]);*manp;manp=(authname **)&((*manp)->next)) {
168 if ((*manp)==anp) {
169 (*manp)=(authname *)anp->next;
170 found = 1;
171 break;
172 }
173 }
174 if(!found) {
175 Error("nick",ERR_ERROR,"Unable to remove authname %lu from hashtable",anp->userid);
176 return;
177 }
178
179 for(manp=&(authnametablebyname[anp->namebucket]);*manp;manp=(authname **)&((*manp)->nextbyname)) {
180 if ((*manp)==anp) {
181 (*manp)=(authname *)anp->nextbyname;
182 freeauthname(anp);
183 return;
184 }
185 }
186
187 Error("nick",ERR_STOP,"Unable to remove authname %lu from byname hashtable, TABLES ARE INCONSISTENT -- DYING",anp->userid);
188 }
189 }
190
191 unsigned int nextauthnamemarker(void) {
192 int i;
193 authname *anp;
194 static unsigned int authnamemarker=0;
195
196 authnamemarker++;
197 if (!authnamemarker) {
198 /* If we wrapped to zero, zap the marker on all records */
199 for (i=0;i<AUTHNAMEHASHSIZE;i++)
200 for (anp=authnametable[i];anp;anp=anp->next)
201 anp->marker=0;
202 authnamemarker++;
203 }
204
205 return authnamemarker;
206 }
207
208 authname *getauthbyname(const char *name) {
209 authname *a = findauthnamebyname(name);
210 if(!a || !a->nicks)
211 return NULL;
212
213 return a;
214 }
215