1 |
pippijn |
1.1 |
/* |
2 |
|
|
* authcookie.C: Remote authentication ticket management |
3 |
pippijn |
1.5 |
* |
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 |
pippijn |
1.2 |
* Rights to this code are documented in doc/pod/license.pod. |
10 |
pippijn |
1.4 |
* Copyright © 2005-2007 Atheme Project (http://www.atheme.org) |
11 |
pippijn |
1.1 |
*/ |
12 |
|
|
|
13 |
pippijn |
1.5 |
static char const rcsid[] = "$Id: authcookie.C,v 1.4 2007-08-28 17:08:12 pippijn Exp $"; |
14 |
pippijn |
1.1 |
|
15 |
|
|
#include "atheme.h" |
16 |
|
|
#include "authcookie.h" |
17 |
|
|
|
18 |
|
|
list_t authcookie_list; |
19 |
|
|
|
20 |
|
|
void |
21 |
|
|
authcookie_init (void) |
22 |
|
|
{ |
23 |
pippijn |
1.4 |
#if 0 |
24 |
pippijn |
1.1 |
authcookie_heap = BlockHeapCreate (sizeof (authcookie_t), 1024); |
25 |
pippijn |
1.4 |
#endif |
26 |
pippijn |
1.1 |
} |
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 |
pippijn |
1.4 |
authcookie_t *au = new authcookie_t; |
44 |
pippijn |
1.1 |
|
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 |
pippijn |
1.4 |
sfree (ac->ticket); |
128 |
|
|
delete ac; |
129 |
pippijn |
1.1 |
} |
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 |
|
|
*/ |