diff options
Diffstat (limited to 'src/udb.c')
-rw-r--r-- | src/udb.c | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/udb.c b/src/udb.c new file mode 100644 index 0000000..aad5982 --- /dev/null +++ b/src/udb.c | |||
@@ -0,0 +1,90 @@ | |||
1 | /* This file is part of tagr. | ||
2 | Copyright (C) 2009 Sergey Poznyakoff | ||
3 | |||
4 | This program is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 3, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | ||
16 | |||
17 | #ifdef HAVE_CONFIG_H | ||
18 | # include <config.h> | ||
19 | #endif | ||
20 | #include <unistd.h> | ||
21 | #include <stdio.h> | ||
22 | #include <stdlib.h> | ||
23 | #include <errno.h> | ||
24 | #include <string.h> | ||
25 | #include <wordsplit.h> | ||
26 | #include <tagr.h> | ||
27 | |||
28 | char *tagr_udb_name = SYSCONFDIR "/tagr.udb"; | ||
29 | |||
30 | int | ||
31 | udb_get_password (const char *username, char **password) | ||
32 | { | ||
33 | FILE *fp; | ||
34 | size_t bufsize = 0; | ||
35 | char *buf = NULL; | ||
36 | unsigned line = 0; | ||
37 | int rc = 1; | ||
38 | |||
39 | fp = fopen (tagr_udb_name, "r"); | ||
40 | if (!fp) | ||
41 | { | ||
42 | logmsg (L_ERR, _("cannot open file %s: %s"), | ||
43 | tagr_udb_name, strerror (errno)); | ||
44 | return -1; | ||
45 | } | ||
46 | |||
47 | while (rc == 1 && getline (&buf, &bufsize, fp) > 0) | ||
48 | { | ||
49 | struct wordsplit ws; | ||
50 | |||
51 | line++; | ||
52 | trim_crlf (buf); | ||
53 | if (wordsplit (buf, &ws, WRDSF_DEFFLAGS)) | ||
54 | { | ||
55 | logmsg (L_ERR, _("%s:%u: failed to parse input line"), | ||
56 | tagr_udb_name, line); | ||
57 | } | ||
58 | if (ws.ws_wordc == 0 || ws.ws_wordv[0][0] == '#') | ||
59 | /* next */; | ||
60 | else if (ws.ws_wordc < 2) | ||
61 | { | ||
62 | logmsg (L_WARNING, _("%s:%u: too few words on line"), | ||
63 | tagr_udb_name, line); | ||
64 | } | ||
65 | else if (ws.ws_wordc > 2) | ||
66 | { | ||
67 | logmsg (L_WARNING, _("%s:%u: too many words on line"), | ||
68 | tagr_udb_name, line); | ||
69 | } | ||
70 | else if (strcmp (ws.ws_wordv[0], username) == 0) | ||
71 | { | ||
72 | *password = xstrdup (ws.ws_wordv[1]); | ||
73 | rc = 0; | ||
74 | } | ||
75 | wordsplit_free (&ws); | ||
76 | } | ||
77 | |||
78 | free (buf); | ||
79 | fclose (fp); | ||
80 | return rc; | ||
81 | } | ||
82 | |||
83 | void | ||
84 | udb_free_password (char *pass) | ||
85 | { | ||
86 | char *p; | ||
87 | for (p = pass; *p; p++) | ||
88 | *p = 0; | ||
89 | free (pass); | ||
90 | } | ||