1 /* Mail alias file parser in nss_db module.
2 Copyright (C) 1996 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. */
27 #include <bits/libc-lock.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. */
40 static unsigned int entidx; /* Index for `getaliasent_r'. */
42 /* Open database file if not already opened. */
43 static enum nss_status
44 internal_setent (int stayopen)
46 enum nss_status status = NSS_STATUS_SUCCESS;
50 db = __dbopen (_PATH_VARDB "aliases.db", O_RDONLY, 0, DB_BTREE, NULL);
53 status = NSS_STATUS_UNAVAIL;
56 /* Remember STAYOPEN flag. */
64 /* Thread-safe, exported version of that. */
66 _nss_db_setaliasent (int stayopen)
68 enum nss_status status;
70 __libc_lock_lock (lock);
72 status = internal_setent (stayopen);
74 /* Reset the sequential index. */
77 __libc_lock_unlock (lock);
83 /* Close the database file. */
85 internal_endent (void)
95 /* Thread-safe, exported version of that. */
97 _nss_db_endaliasent (void)
99 __libc_lock_lock (lock);
103 /* Reset STAYOPEN flag. */
106 __libc_lock_unlock (lock);
108 return NSS_STATUS_SUCCESS;
111 /* We provide the parse function here. The parser in libnss_files
112 cannot be used. The generation of the db file already resolved all
113 :include: statements so we simply have to parse the list and store
115 static enum nss_status
116 lookup (const DBT *key, struct aliasent *result, char *buffer,
119 enum nss_status status;
122 /* Open the database. */
123 status = internal_setent (keep_db);
124 if (status != NSS_STATUS_SUCCESS)
127 if ((*db->get) (db, key, &value, 0) == 0)
129 const char *src = value.data;
131 result->alias_members_len = 0;
133 /* We now have to fill the BUFFER with all the information. */
134 if (buflen < key->size + 1)
137 __set_errno (ERANGE);
138 return NSS_STATUS_TRYAGAIN;
141 if (status == NSS_STATUS_SUCCESS)
146 buffer = stpncpy (buffer, key->data, key->size) + 1;
147 buflen -= key->size + 1;
151 const char *end, *upto;
152 while (isspace (*src))
155 end = strchr (src, ',');
157 end = strchr (src, '\0');
158 for (upto = end; upto > src && isspace (upto[-1]); --upto);
162 if ((upto - src) + __alignof__ (char *) > buflen)
164 buffer = stpncpy (buffer, src, upto - src) + 1;
165 buflen -= (upto - src) + __alignof (char *);
166 ++result->alias_members_len;
168 src = end + (*end != '\0');
171 /* Now prepare the return. Provide string pointers for the
172 currently selected aliases. */
174 /* Adjust the pointer so it is aligned for storing pointers. */
175 buffer += __alignof__ (char *) - 1;
176 buffer -= ((buffer - (char *) 0) % __alignof__ (char *));
177 result->alias_members = (char **) buffer;
179 /* Compute addresses of alias entry strings. */
180 cp = result->alias_name;
181 for (cnt = 0; cnt < result->alias_members_len; ++cnt)
183 cp = strchr (cp, '\0') + 1;
184 result->alias_members[cnt] = cp;
187 status = (result->alias_members_len == 0
188 ? NSS_STATUS_RETURN : NSS_STATUS_SUCCESS);
192 status = NSS_STATUS_NOTFOUND;
201 _nss_db_getaliasent_r (struct aliasent *result, char *buffer, size_t buflen)
203 /* Return next entry in host file. */
204 enum nss_status status;
208 __libc_lock_lock (lock);
209 key.size = 1 + snprintf (key.data = buf, sizeof buf, "0%u", entidx++);
210 status = lookup (&key, result, buffer, buflen);
211 __libc_lock_unlock (lock);
218 _nss_db_getaliasbyname_r (const char *name, struct aliasent *result,
219 char *buffer, size_t buflen)
222 enum nss_status status;
224 key.size = 1 + strlen (name);
226 key.data = __alloca (key.size);
227 ((char *) key.data)[0] = '.';
228 memcpy (&((char *) key.data)[1], name, key.size - 1);
230 __libc_lock_lock (lock);
231 status = lookup (&key, result, buffer, buflen);
232 __libc_lock_unlock (lock);