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