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