gsasl  1.8.0
md5pwd.c
Go to the documentation of this file.
00001 /* md5pwd.c --- Find passwords in UoW imapd MD5 type password files.
00002  * Copyright (C) 2002-2012 Simon Josefsson
00003  *
00004  * This file is part of GNU SASL Library.
00005  *
00006  * GNU SASL Library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public License
00008  * as published by the Free Software Foundation; either version 2.1 of
00009  * the License, or (at your option) any later version.
00010  *
00011  * GNU SASL Library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License License along with GNU SASL Library; if not, write to the
00018  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "internal.h"
00024 
00046 int
00047 gsasl_simple_getpass (const char *filename, const char *username, char **key)
00048 {
00049   size_t userlen = strlen (username);
00050   char *line = NULL;
00051   size_t n = 0;
00052   FILE *fh;
00053 
00054   fh = fopen (filename, "r");
00055   if (fh)
00056     {
00057       while (!feof (fh))
00058         {
00059           if (getline (&line, &n, fh) < 0)
00060             break;
00061 
00062           if (line[0] == '#')
00063             continue;
00064 
00065           if (line[strlen (line) - 1] == '\r')
00066             line[strlen (line) - 1] = '\0';
00067           if (line[strlen (line) - 1] == '\n')
00068             line[strlen (line) - 1] = '\0';
00069 
00070           if (strncmp (line, username, userlen) == 0 && line[userlen] == '\t')
00071             {
00072               *key = malloc (strlen (line) - userlen);
00073               if (!*key)
00074                 {
00075                   free (line);
00076                   return GSASL_MALLOC_ERROR;
00077                 }
00078 
00079               strcpy (*key, line + userlen + 1);
00080 
00081               free (line);
00082 
00083               fclose (fh);
00084 
00085               return GSASL_OK;
00086             }
00087         }
00088 
00089       fclose (fh);
00090     }
00091 
00092   free (line);
00093 
00094   return GSASL_AUTHENTICATION_ERROR;
00095 }