Updated to fedora-glibc-20050627T0850
[glibc.git] / glibc-compat / nss_db / db-alias.c
blobb9b94899896c5df69f09aa6215b1c8c65fa8616b
1 /* Mail alias file parser in nss_db module.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include <glibc-compat/include/aliases.h>
22 #include <alloca.h>
23 #include <ctype.h>
24 #include <dlfcn.h>
25 #include <errno.h>
26 #include <bits/libc-lock.h>
27 #include <paths.h>
28 #include <string.h>
30 #include "nsswitch.h"
31 #include "nss_db.h"
33 /* Locks the static variables in this file. */
34 __libc_lock_define_initialized (static, lock)
36 /* Maintenance of the shared handle open on the database. */
38 static NSS_DB *db;
39 static int keep_db;
40 static unsigned int entidx; /* Index for `getaliasent_r'. */
43 /* Open database. */
44 enum nss_status
45 _nss_db_setaliasent (int stayopen)
47 enum nss_status status;
49 __libc_lock_lock (lock);
51 status = internal_setent (_PATH_VARDB "aliases.db", &db);
53 /* Remember STAYOPEN flag. */
54 if (db != NULL)
55 keep_db |= stayopen;
57 /* Reset the sequential index. */
58 entidx = 0;
60 __libc_lock_unlock (lock);
62 return status;
66 /* Close it again. */
67 enum nss_status
68 _nss_db_endaliasent (void)
70 __libc_lock_lock (lock);
72 internal_endent (&db);
74 /* Reset STAYOPEN flag. */
75 keep_db = 0;
77 __libc_lock_unlock (lock);
79 return NSS_STATUS_SUCCESS;
82 /* We provide the parse function here. The parser in libnss_files
83 cannot be used. The generation of the db file already resolved all
84 :include: statements so we simply have to parse the list and store
85 the result. */
86 static enum nss_status
87 lookup (DBT *key, struct aliasent *result, char *buffer,
88 size_t buflen)
90 enum nss_status status;
91 DBT value;
93 /* Open the database. */
94 if (db == NULL)
96 status = internal_setent (_PATH_VARDB "aliases.db", &db);
97 if (status != NSS_STATUS_SUCCESS)
98 return status;
101 value.flags = 0;
102 if (DL_CALL_FCT (db->get, (db->db, NULL, key, &value, 0)) == 0)
104 const char *src = value.data;
105 char *cp;
106 size_t cnt;
108 result->alias_members_len = 0;
110 /* We now have to fill the BUFFER with all the information. */
111 if (buflen < key->size + 1)
113 no_more_room:
114 __set_errno (ERANGE);
115 return NSS_STATUS_TRYAGAIN;
118 buffer = stpncpy (buffer, key->data, key->size) + 1;
119 buflen -= key->size + 1;
121 while (*src != '\0')
123 const char *end, *upto;
124 while (isspace (*src))
125 ++src;
127 end = strchr (src, ',');
128 if (end == NULL)
129 end = strchr (src, '\0');
130 for (upto = end; upto > src && isspace (upto[-1]); --upto);
132 if (upto != src)
134 if ((upto - src) + __alignof__ (char *) > buflen)
135 goto no_more_room;
136 buffer = stpncpy (buffer, src, upto - src) + 1;
137 buflen -= (upto - src) + __alignof (char *);
138 ++result->alias_members_len;
140 src = end + (*end != '\0');
143 /* Now prepare the return. Provide string pointers for the
144 currently selected aliases. */
146 /* Adjust the pointer so it is aligned for storing pointers. */
147 buffer += __alignof__ (char *) - 1;
148 buffer -= ((buffer - (char *) 0) % __alignof__ (char *));
149 result->alias_members = (char **) buffer;
151 /* Compute addresses of alias entry strings. */
152 cp = result->alias_name;
153 for (cnt = 0; cnt < result->alias_members_len; ++cnt)
155 cp = strchr (cp, '\0') + 1;
156 result->alias_members[cnt] = cp;
159 status = (result->alias_members_len == 0
160 ? NSS_STATUS_RETURN : NSS_STATUS_SUCCESS);
162 else
163 status = NSS_STATUS_NOTFOUND;
165 if (! keep_db)
166 internal_endent (&db);
168 return status;
171 enum nss_status
172 _nss_db_getaliasent_r (struct aliasent *result, char *buffer, size_t buflen)
174 /* Return next entry in alias file. */
175 enum nss_status status;
176 char buf[20];
177 DBT key;
179 __libc_lock_lock (lock);
180 key.size = snprintf (key.data = buf, sizeof buf, "0%u", entidx++);
181 key.flags = 0;
182 status = lookup (&key, result, buffer, buflen);
183 __libc_lock_unlock (lock);
185 return status;
189 enum nss_status
190 _nss_db_getaliasbyname_r (const char *name, struct aliasent *result,
191 char *buffer, size_t buflen)
193 DBT key;
194 enum nss_status status;
196 key.size = 1 + strlen (name);
198 key.data = __alloca (key.size);
199 ((char *) key.data)[0] = '.';
200 memcpy (&((char *) key.data)[1], name, key.size - 1);
201 key.flags = 0;
203 __libc_lock_lock (lock);
204 status = lookup (&key, result, buffer, buflen);
205 __libc_lock_unlock (lock);
207 return status;