nss-shared-helper/db.c
changeset 1 98d01e6fd2bd
parent 0 262e1fb001a8
child 3 1d5c74914855
equal deleted inserted replaced
0:262e1fb001a8 1:98d01e6fd2bd
     1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 
     2 
     3 /* db.c - Helper funcs for shared NSS database
     3 /* db.c - Helper funcs for shared NSS database
     4  *
     4  *
     5  * Copyright (C) 2008 Hans Petter Jansson
     5  * Copyright (C) 2008 Hans Petter Jansson
     6  *               2008-2010 Wolfgang Rosenauer
     6  *               2008-2011 Wolfgang Rosenauer
     7  *
     7  *
     8  * This library is free software; you can redistribute it and/or
     8  * This library is free software; you can redistribute it and/or
     9  * modify it under the terms of the GNU Lesser General Public
     9  * modify it under the terms of the GNU Lesser General Public
    10  * License as published by the Free Software Foundation; either
    10  * License as published by the Free Software Foundation; either
    11  * version 2 of the License, or (at your option) any later version.
    11  * version 2 of the License, or (at your option) any later version.
    18  * You should have received a copy of the GNU Lesser General Public
    18  * You should have received a copy of the GNU Lesser General Public
    19  * License along with this library; if not, write to the
    19  * License along with this library; if not, write to the
    20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    21  * Boston, MA 02111-1307, USA.
    21  * Boston, MA 02111-1307, USA.
    22  *
    22  *
    23  * Authors: Hans Petter Jansson <hpj@copyleft.no> 
    23  * Authors: Hans Petter Jansson <hpj@copyleft.no>
    24  *          Wolfgang Rosenauer <wr@rosenauer.org> */
    24  *          Wolfgang Rosenauer <wr@rosenauer.org> */
    25 
    25 
    26 #include <sys/types.h>
    26 #include <sys/types.h>
    27 #include <sys/stat.h>
    27 #include <sys/stat.h>
    28 #include <unistd.h>
    28 #include <unistd.h>
    54 char *
    54 char *
    55 nsshelp_get_user_db_path (void)
    55 nsshelp_get_user_db_path (void)
    56 {
    56 {
    57   const char  *env;
    57   const char  *env;
    58   char        *path;
    58   char        *path;
       
    59   char         system_path[] = DEFAULT_PATH;
    59   struct stat  sbuf;
    60   struct stat  sbuf;
    60   int          result;
    61   int          result;
    61 
    62 
    62   env = getenv ("NSS_SHARED_DB_PATH");
    63   env = getenv ("NSS_SHARED_DB_PATH");
    63   
    64 
       
    65   /*
       
    66    * 1. use env NSS_SHARED_DB_PATH
       
    67    * 2. try /etc/pki/nssdb
       
    68    * 3. use $HOME/.pki/nssdb
       
    69    */
    64   if (env && *env) {
    70   if (env && *env) {
    65     path = strdup (env);
    71     path = strdup (env);
    66   } else {
    72   } else {
    67     /*
       
    68     env = getenv ("HOME");
    73     env = getenv ("HOME");
    69     if (!env || !*env)
    74     if (env && *env)
    70     {
    75     {
    71       fprintf (stderr, "*** nss-shared-helper: Could not determine shared NSS DB path!\n");
    76       path = malloc (strlen (env) + 1 + strlen (DEFAULT_RELATIVE_PATH) + 1);
    72       return NULL;
    77       strcpy (path, env);
    73     }
    78       strcat (path, "/");
    74 
    79       strcat (path, DEFAULT_RELATIVE_PATH);
    75     path = malloc (strlen (env) + 1 + strlen (DEFAULT_RELATIVE_PATH) + 1);
    80     }
    76     strcpy (path, env);
       
    77     strcat (path, "/");
       
    78     strcat (path, DEFAULT_RELATIVE_PATH);
       
    79     */
       
    80     path = strdup(DEFAULT_PATH);
       
    81   }
    81   }
    82 
    82 
    83   /* Create path if it doesn't exist */
    83   /* Create path if it doesn't exist */
    84 
       
    85   result = stat (path, &sbuf);
    84   result = stat (path, &sbuf);
    86 
       
    87   if (result != 0)
    85   if (result != 0)
    88   {
    86   {
    89     const char *p0 = path;
    87     const char *p0 = path;
    90     const char *p1;
    88     const char *p1;
    91 
    89 
   110       }
   108       }
   111 
   109 
   112       p0 = p1 + 1;
   110       p0 = p1 + 1;
   113     }
   111     }
   114     while (*p1 != '\0');
   112     while (*p1 != '\0');
   115 
   113   }
   116     result = stat (path, &sbuf);
   114 
   117   }
   115   /* /etc/pki/nssdb */
   118 
   116   if (stat(system_path, &sbuf) == 0 && S_ISDIR (sbuf.st_mode))
   119   if (result == 0 && S_ISDIR (sbuf.st_mode))
   117     return system_path;
       
   118 
       
   119   /* $HOME/.pki/nssdb */
       
   120   if (stat(path, &sbuf) == 0 && S_ISDIR (sbuf.st_mode))
   120     return path;
   121     return path;
   121 
   122 
   122   free (path);
   123   free (path);
   123   return NULL;
   124   return NULL;
   124 }
   125 }
   134   PK11SlotInfo *slot;
   135   PK11SlotInfo *slot;
   135   secuPWData pwdata = { PW_NONE, 0 };
   136   secuPWData pwdata = { PW_NONE, 0 };
   136 
   137 
   137   if (!getenv ("NSS_USE_SHARED_DB"))
   138   if (!getenv ("NSS_USE_SHARED_DB"))
   138   {
   139   {
   139     fprintf (stderr, "*** nss-shared-helper: Shared database disabled (set NSS_USE_SHARED_DB to enable).\n");
   140     //fprintf (stderr, "*** nss-shared-helper: Shared database disabled (set NSS_USE_SHARED_DB to enable).\n");
   140 
   141 
   141     rv = NSS_Initialize (old_path,
   142     rv = NSS_Initialize (old_path,
   142                          "", "", "secmod.db",
   143                          "", "", "secmod.db",
   143                          flags);
   144                          flags);
   144     return rv;
   145     return rv;