gsasl  2.2.1
md5pwd.c
Go to the documentation of this file.
1 /* md5pwd.c --- Find passwords in UoW imapd MD5 type password files.
2  * Copyright (C) 2002-2024 Simon Josefsson
3  *
4  * This file is part of GNU SASL Library.
5  *
6  * GNU SASL Library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * GNU SASL Library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License License along with GNU SASL Library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include <config.h>
24 #include "internal.h"
25 
47 int
48 gsasl_simple_getpass (const char *filename, const char *username, char **key)
49 {
50  size_t userlen = strlen (username);
51  char *line = NULL;
52  size_t n = 0;
53  FILE *fh;
54 
55  fh = fopen (filename, "r");
56  if (fh)
57  {
58  while (!feof (fh))
59  {
60  if (getline (&line, &n, fh) < 0)
61  break;
62 
63  if (line[0] == '#')
64  continue;
65 
66  if (line[strlen (line) - 1] == '\r')
67  line[strlen (line) - 1] = '\0';
68  if (line[strlen (line) - 1] == '\n')
69  line[strlen (line) - 1] = '\0';
70 
71  if (strncmp (line, username, userlen) == 0 && line[userlen] == '\t')
72  {
73  *key = malloc (strlen (line) - userlen);
74  if (!*key)
75  {
76  free (line);
77  return GSASL_MALLOC_ERROR;
78  }
79 
80  strcpy (*key, line + userlen + 1);
81 
82  free (line);
83 
84  fclose (fh);
85 
86  return GSASL_OK;
87  }
88  }
89 
90  fclose (fh);
91  }
92 
93  free (line);
94 
96 }
@ GSASL_OK
Definition: gsasl.h:129
@ GSASL_AUTHENTICATION_ERROR
Definition: gsasl.h:138
@ GSASL_MALLOC_ERROR
Definition: gsasl.h:133
int gsasl_simple_getpass(const char *filename, const char *username, char **key)
Definition: md5pwd.c:48