1 |
/* |
2 |
* authcookie.C: Remote authentication ticket management |
3 |
* Rights to this code are documented in doc/pod/license.pod. |
4 |
* |
5 |
* Copyright © 2005-2007 Atheme Project (http://www.atheme.org) |
6 |
*/ |
7 |
|
8 |
static char const rcsid[] = "$Id"; |
9 |
|
10 |
#include "atheme.h" |
11 |
#include "authcookie.h" |
12 |
|
13 |
list_t authcookie_list; |
14 |
static BlockHeap *authcookie_heap; |
15 |
|
16 |
void |
17 |
authcookie_init (void) |
18 |
{ |
19 |
authcookie_heap = BlockHeapCreate (sizeof (authcookie_t), 1024); |
20 |
|
21 |
if (!authcookie_heap) |
22 |
{ |
23 |
slog (LG_ERROR, "authcookie_init(): cannot initialize block allocator."); |
24 |
exit (EXIT_FAILURE); |
25 |
} |
26 |
} |
27 |
|
28 |
/* |
29 |
* authcookie_create() |
30 |
* |
31 |
* Inputs: |
32 |
* account associated with the authcookie |
33 |
* |
34 |
* Outputs: |
35 |
* pointer to new authcookie |
36 |
* |
37 |
* Side Effects: |
38 |
* an authcookie ticket is created, and validated. |
39 |
*/ |
40 |
authcookie_t * |
41 |
authcookie_create (myuser_t *mu) |
42 |
{ |
43 |
authcookie_t *au = static_cast<authcookie_t *> (BlockHeapAlloc (authcookie_heap)); |
44 |
|
45 |
au->ticket = gen_pw (20); |
46 |
au->myuser = mu; |
47 |
au->expire = NOW + 3600; |
48 |
|
49 |
node_add (au, &au->node, &authcookie_list); |
50 |
|
51 |
return au; |
52 |
} |
53 |
|
54 |
/* |
55 |
* authcookie_find() |
56 |
* |
57 |
* Inputs: |
58 |
* either the ticket string, the myuser_t it is associated with, or both |
59 |
* |
60 |
* Outputs: |
61 |
* the authcookie ticket for this object, if any |
62 |
* |
63 |
* Side Effects: |
64 |
* none |
65 |
*/ |
66 |
authcookie_t * |
67 |
authcookie_find (char *ticket, myuser_t *myuser) |
68 |
{ |
69 |
node_t *n; |
70 |
authcookie_t *ac; |
71 |
|
72 |
/* at least one must be specified */ |
73 |
return_val_if_fail (ticket != NULL || myuser != NULL, NULL); |
74 |
|
75 |
if (!myuser) /* must have ticket */ |
76 |
{ |
77 |
LIST_FOREACH (n, authcookie_list.head) |
78 |
{ |
79 |
ac = static_cast<authcookie_t *> (n->data); |
80 |
|
81 |
if (!strcmp (ac->ticket, ticket)) |
82 |
return ac; |
83 |
} |
84 |
} |
85 |
else if (!ticket) /* must have myuser */ |
86 |
{ |
87 |
LIST_FOREACH (n, authcookie_list.head) |
88 |
{ |
89 |
ac = static_cast<authcookie_t *> (n->data); |
90 |
|
91 |
if (ac->myuser == myuser) |
92 |
return ac; |
93 |
} |
94 |
} |
95 |
else /* must have both */ |
96 |
{ |
97 |
LIST_FOREACH (n, authcookie_list.head) |
98 |
{ |
99 |
ac = static_cast<authcookie_t *> (n->data); |
100 |
|
101 |
if (ac->myuser == myuser && !strcmp (ac->ticket, ticket)) |
102 |
return ac; |
103 |
} |
104 |
} |
105 |
|
106 |
return NULL; |
107 |
} |
108 |
|
109 |
/* |
110 |
* authcookie_destroy() |
111 |
* |
112 |
* Inputs: |
113 |
* an authcookie to destroy |
114 |
* |
115 |
* Outputs: |
116 |
* none |
117 |
* |
118 |
* Side Effects: |
119 |
* an authcookie is destroyed |
120 |
*/ |
121 |
void |
122 |
authcookie_destroy (authcookie_t *ac) |
123 |
{ |
124 |
return_if_fail (ac != NULL); |
125 |
|
126 |
node_del (&ac->node, &authcookie_list); |
127 |
free (ac->ticket); |
128 |
BlockHeapFree (authcookie_heap, ac); |
129 |
} |
130 |
|
131 |
/* |
132 |
* authcookie_destroy_all() |
133 |
* |
134 |
* Inputs: |
135 |
* a myuser_t pointer |
136 |
* |
137 |
* Outputs: |
138 |
* none |
139 |
* |
140 |
* Side Effects: |
141 |
* all authcookies for the user are destroyed |
142 |
*/ |
143 |
void |
144 |
authcookie_destroy_all (myuser_t *mu) |
145 |
{ |
146 |
node_t *n, *tn; |
147 |
authcookie_t *ac; |
148 |
|
149 |
LIST_FOREACH_SAFE (n, tn, authcookie_list.head) |
150 |
{ |
151 |
ac = static_cast<authcookie_t *> (n->data); |
152 |
|
153 |
if (ac->myuser == mu) |
154 |
authcookie_destroy (ac); |
155 |
} |
156 |
} |
157 |
|
158 |
/* |
159 |
* authcookie_expire() |
160 |
* |
161 |
* Inputs: |
162 |
* unused arg because this is an event function |
163 |
* |
164 |
* Outputs: |
165 |
* none |
166 |
* |
167 |
* Side Effects: |
168 |
* expired authcookies are destroyed |
169 |
*/ |
170 |
void |
171 |
authcookie_expire (void *arg) |
172 |
{ |
173 |
authcookie_t *ac; |
174 |
node_t *n, *tn; |
175 |
|
176 |
(void) arg; |
177 |
LIST_FOREACH_SAFE (n, tn, authcookie_list.head) |
178 |
{ |
179 |
ac = static_cast<authcookie_t *> (n->data); |
180 |
|
181 |
if (ac->expire <= NOW) |
182 |
authcookie_destroy (ac); |
183 |
} |
184 |
} |
185 |
|
186 |
/* |
187 |
* authcookie_validate() |
188 |
* |
189 |
* Inputs: |
190 |
* a ticket and myuser pair that needs to be validated |
191 |
* |
192 |
* Outputs: |
193 |
* true if the authcookie is valid, |
194 |
* otherwise false |
195 |
* |
196 |
* Side Effects: |
197 |
* expired authcookies are destroyed here |
198 |
*/ |
199 |
bool |
200 |
authcookie_validate (char *ticket, myuser_t *myuser) |
201 |
{ |
202 |
authcookie_t *ac = authcookie_find (ticket, myuser); |
203 |
|
204 |
if (ac == NULL) |
205 |
return false; |
206 |
|
207 |
if (ac->expire <= NOW) |
208 |
{ |
209 |
authcookie_destroy (ac); |
210 |
return false; |
211 |
} |
212 |
|
213 |
return true; |
214 |
} |
215 |
|
216 |
/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs |
217 |
* vim:ts=8 |
218 |
* vim:sw=8 |
219 |
* vim:noexpandtab |
220 |
*/ |