Back to index

glibc  2.9
files-key.c
Go to the documentation of this file.
00001 /* Public key file parser in nss_files module.
00002    Copyright (C) 1996, 1997, 1998, 2006 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <stdio.h>
00021 #include <errno.h>
00022 #include <string.h>
00023 #include <netdb.h>
00024 #include <rpc/key_prot.h>
00025 #include "nsswitch.h"
00026 
00027 #define DATAFILE "/etc/publickey"
00028 
00029 /* Prototype for function in xcyrpt.c.  */
00030 extern int xdecrypt (char *, char *);
00031 
00032 
00033 static enum nss_status
00034 search (const char *netname, char *result, int *errnop, int secret)
00035 {
00036   FILE *stream;
00037 
00038   stream = fopen (DATAFILE, "r");
00039   if (stream == NULL)
00040     return errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
00041 
00042   for (;;)
00043     {
00044       char buffer[HEXKEYBYTES * 2 + KEYCHECKSUMSIZE + MAXNETNAMELEN + 17];
00045       char *p;
00046       char *save_ptr;
00047 
00048       buffer[sizeof (buffer) - 1] = '\xff';
00049       p = fgets (buffer, sizeof (buffer), stream);
00050       if (p == NULL)
00051        {
00052          /* End of file or read error.  */
00053          *errnop = errno;
00054          fclose (stream);
00055          return NSS_STATUS_NOTFOUND;
00056        }
00057       else if (buffer[sizeof (buffer) - 1] != '\xff')
00058        {
00059          /* Invalid line in file?  Skip remainder of line.  */
00060          if (buffer[sizeof (buffer) - 2] != '\0')
00061            while (getc (stream) != '\n')
00062              continue;
00063          continue;
00064        }
00065 
00066       /* Parse line.  */
00067       p = __strtok_r (buffer, "# \t:\n", &save_ptr);
00068       if (p == NULL) /* Skip empty and comment lines.  */
00069        continue;
00070       if (strcmp (p, netname) != 0)
00071        continue;
00072 
00073       /* A hit!  Find the field we want and return.  */
00074       p = __strtok_r (NULL, ":\n", &save_ptr);
00075       if (p == NULL)  /* malformed line? */
00076        continue;
00077       if (secret)
00078        p = __strtok_r (NULL, ":\n", &save_ptr);
00079       if (p == NULL)  /* malformed line? */
00080        continue;
00081       fclose (stream);
00082       strcpy (result, p);
00083       return NSS_STATUS_SUCCESS;
00084     }
00085 }
00086 
00087 enum nss_status
00088 _nss_files_getpublickey (const char *netname, char *pkey, int *errnop)
00089 {
00090   return search (netname, pkey, errnop, 0);
00091 }
00092 
00093 enum nss_status
00094 _nss_files_getsecretkey (const char *netname, char *skey, char *passwd,
00095                       int *errnop)
00096 {
00097   enum nss_status status;
00098   char buf[HEXKEYBYTES + KEYCHECKSUMSIZE + 16];
00099 
00100   skey[0] = 0;
00101 
00102   status = search (netname, buf, errnop, 1);
00103   if (status != NSS_STATUS_SUCCESS)
00104     return status;
00105 
00106   if (!xdecrypt (buf, passwd))
00107     return NSS_STATUS_SUCCESS;
00108 
00109   if (memcmp (buf, &(buf[HEXKEYBYTES]), KEYCHECKSUMSIZE) != 0)
00110     return NSS_STATUS_SUCCESS;
00111 
00112   buf[HEXKEYBYTES] = 0;
00113   strcpy (skey, buf);
00114 
00115   return NSS_STATUS_SUCCESS;
00116 }