Debian Codegraph @

src/pam/auth-method-localdb/usersdb.c

raw symbols SCIP
1 /* usersdb.c - PAM authentication via OpenPGP smartcards.
2 Copyright (C) 2004, 2005, 2007, 2008 g10 Code GmbH
3
4 This file is part of Poldi.
5
6 Poldi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 Poldi is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20 #include <poldi.h>
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <assert.h>
25 #include <string.h>
26
27 #include <gcrypt.h>
28
29 #include "usersdb.h"
30 #include "defs-localdb.h"
31
32
33
34 /* This is the type for callbacks functions, which need to be passed
35 to usersdb_process(). The callback function receives one
36 (SERIALNO, USERNAME) pair per invocation. OPAQUE is the opaque
37 arugment passed to usersdb_process(). The return code of such a
38 callback functions has the following meanings:
39
40 0: Continue processing the users database.
41 1: Stop processing. */
42 typedef int (*usersdb_cb_t) (const char *serialno, const char *username,
43 void *opaque);
44
45 /* This functions processes the users database. For each read pair of
46 a card serial number and a account, the callback function specified
47 as (CB, OPAQUE) is called. Depending on CB's return code,
48 processing is continued or aborted. */
49 static gpg_error_t
50 usersdb_process (usersdb_cb_t cb, void *opaque)
51 {
52 const char *delimiters = "\t\n ";
53 gpg_error_t err;
54 FILE *usersdb;
55 char *line;
56 char *line_serialno;
57 char *line_username;
58 size_t line_n;
59 ssize_t ret;
60 int cb_ret;
61 char *save_ptr; /* For strtok_r. */
62
63 line_serialno = NULL;
64 line_username = NULL;
65 line = NULL;
66 err = 0;
67
68 /* Open users database. */
69 usersdb = fopen (POLDI_USERS_DB_FILE, "r");
70 if (! usersdb)
71 {
72 err = gpg_error_from_syserror ();
73 goto out;
74 }
75
76 /* Process lines. */
77 while (1)
78 {
79 /* Get next line. */
80 save_ptr = NULL;
81 line = NULL;
82 line_n = 0;
83 ret = getline (&line, &line_n, usersdb);
84 if (ret == -1)
85 {
86 if (ferror (usersdb))
87 err = gpg_error_from_syserror ();
88 /* else EOF. */
89 break;
90 }
91
92 {
93 /* Cut off comments. */
94 char *comment = strchr (line, '#');
95 if (comment)
96 *comment = '\0';
97 }
98
99 /* Extract first token: the card serial number. */
100 line_serialno = strtok_r (line, delimiters, &save_ptr);
101 if (!line_serialno)
102 /* Ignore this incomplete entry. */
103 goto skip;
104
105 /* Extract second token: the username. */
106 line_username = strtok_r (NULL, delimiters, &save_ptr);
107 if (!line_username)
108 /* Ignore this incomplete entry. */
109 goto skip;
110
111 /* Looks like a valid entry, pass to callback function. */
112 cb_ret = (*cb) (line_serialno, line_username, opaque);
113 if (cb_ret)
114 /* Callback functions wants us to stop. */
115 break;
116
117 skip:
118 free (line);
119 }
120 if (err)
121 goto out;
122
123 /* Finalize. */
124 (*cb) (NULL, NULL, opaque);
125
126 out:
127
128 if (usersdb)
129 fclose (usersdb);
130 free (line); /* Allocated by getline, thus standard
131 free. */
132 return err;
133 }
134
135
136
137 /*
138 * Implementation of "usersdb_check" function. usersdb_check()
139 * figures out wether a given serial number is assocated with a given
140 * username or not.
141 */
142
143 /* Type for opaque callback argument. */
144 typedef struct check_cb_s
145 {
146 /* This is the pair we are looking for. */
147 const char *serialno;
148 const char *username;
149
150 /* If found, this is set to TRUE by the callback function. */
151 int match;
152 } *check_cb_t;
153
154 /* Callback function. */
155 static int
156 usersdb_check_cb (const char *serialno, const char *username, void *opaque)
157 {
158 check_cb_t ctx = opaque;
159
160 if (! (serialno || username))
161 /* Finalizing */;
162 else
163 {
164 /* Regular entry. */
165
166 if ((! strcmp (ctx->serialno, serialno))
167 && (! strcmp (ctx->username, username)))
168 {
169 /* The current entry is exactly the one we were looking
170 for. */
171 ctx->match = 1;
172 return 1;
173 }
174 }
175
176 return 0;
177 }
178
179 /* This functions figures out wether the provided (SERIALNO, USERNAME)
180 pair is contained in the users database. */
181 gpg_error_t
182 usersdb_check (const char *serialno, const char *username)
183 {
184 struct check_cb_s ctx = { serialno, username, 0 };
185 gpg_error_t err;
186
187 err = usersdb_process (usersdb_check_cb, &ctx);
188 if (! err)
189 {
190 /* Now we have a result in CTX. */
191
192 if (! ctx.match)
193 err = gpg_error (GPG_ERR_NOT_FOUND); /* FIXME: not the best
194 return code... */
195 }
196
197 return err;
198 }
199
200
201
202 /*
203 *
204 */
205
206 /* Looking up a username for a given serial number. */
207
208 typedef struct lookup_cb_s
209 {
210 const char *serialno;
211 const char *username;
212 int matches;
213 char *found;
214 gpg_error_t err;
215 } *lookup_cb_t;
216
217 static int
218 usersdb_lookup_cb (const char *serialno, const char *username, void *opaque)
219 {
220 lookup_cb_t ctx = opaque;
221 char *str;
222
223 if (! (serialno || username))
224 goto out;
225
226 /* FIXME: this function is not nice. */
227
228 if (ctx->serialno)
229 {
230 if (! strcmp (ctx->serialno, serialno))
231 {
232 if (! ctx->matches)
233 {
234 str = xtrystrdup (username);
235 if (! str)
236 {
237 ctx->err = gpg_error_from_syserror ();
238 return 1;
239 }
240
241 ctx->found = str;
242 ctx->matches++;
243 }
244 else
245 {
246 if (strcmp (ctx->found, username))
247 ctx->matches++;
248 }
249 }
250 }
251 else
252 {
253 if (! strcmp (ctx->username, username))
254 {
255 if (! ctx->matches)
256 {
257 str = xtrystrdup (serialno);
258 if (! str)
259 {
260 ctx->err = gpg_error_from_syserror ();
261 return 1;
262 }
263
264 ctx->found = str;
265 ctx->matches++;
266 }
267 else
268 {
269 if (strcmp (ctx->found, serialno))
270 ctx->matches++;
271 }
272 }
273 }
274
275 out:
276
277 return 0;
278 }
279
280 /* This function tries to lookup a username by it's serial number;
281 this is only possible in case the specified serial number SERIALNO
282 is associated with exactly one username. The username will be
283 stored in newly allocated memory in *USERNAME. Returns proper
284 error code. */
285 gpg_error_t
286 usersdb_lookup_by_serialno (const char *serialno, char **username)
287 {
288 struct lookup_cb_s ctx = { serialno, NULL, 0, NULL, 0 };
289 gpg_error_t err;
290
291 assert (serialno);
292 assert (username);
293
294 err = usersdb_process (usersdb_lookup_cb, &ctx);
295 if (err)
296 goto out;
297
298 /* Now we have a result in CTX. */
299
300 if (ctx.err)
301 {
302 err = ctx.err;
303 goto out;
304 }
305
306 if (! ctx.matches)
307 {
308 err = gpg_error (GPG_ERR_NOT_FOUND);
309 goto out;
310 }
311 else if (ctx.matches > 1)
312 {
313 err = gpg_error (GPG_ERR_AMBIGUOUS_NAME);
314 goto out;
315 }
316 else if (ctx.matches == 1)
317 {
318 *username = ctx.found;
319 ctx.found = NULL;
320 }
321
322 out:
323
324 xfree (ctx.found);
325
326 return err;
327 }
328
329 /* This function tries to lookup a serial number by it's username;
330 this is only possible in case the specified username USERNAME is
331 associated with exactly one serial number. The serial number will
332 be stored in newly allocated memory in *SERIALNO. Returns proper
333 error code. */
334 gpg_error_t
335 usersdb_lookup_by_username (const char *username, char **serialno)
336 {
337 struct lookup_cb_s ctx = { NULL, username, 0, NULL, 0 };
338 gpg_error_t err;
339
340 assert (username);
341 assert (serialno);
342
343 err = usersdb_process (usersdb_lookup_cb, &ctx);
344 if (err)
345 goto out;
346
347 /* Now we have a result in CTX. */
348
349 if (ctx.err)
350 {
351 err = ctx.err;
352 goto out;
353 }
354
355 if (! ctx.matches)
356 {
357 err = gpg_error (GPG_ERR_NOT_FOUND);
358 goto out;
359 }
360 else if (ctx.matches > 1)
361 {
362 err = gpg_error (GPG_ERR_AMBIGUOUS_NAME);
363 goto out;
364 }
365 else if (ctx.matches == 1)
366 {
367 *serialno = ctx.found;
368 ctx.found = NULL;
369 }
370
371 out:
372
373 xfree (ctx.found);
374
375 return err;
376 }
377
378 /* END */