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