1 /* Mail alias file parser in nss_db module.
2 Copyright (C) 1996, 1997, 1998 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 /* We have to make sure the file is `closed on exec'. */
59 result = flags = fcntl ((*db->fd) (db), F_GETFD, 0);
63 result = fcntl ((*db->fd) (db), F_SETFD, flags);
67 /* Something went wrong. Close the stream and return a
71 status = NSS_STATUS_UNAVAIL;
76 /* Remember STAYOPEN flag. */
84 /* Thread-safe, exported version of that. */
86 _nss_db_setaliasent (int stayopen)
88 enum nss_status status;
90 __libc_lock_lock (lock);
92 status = internal_setent (stayopen);
94 /* Reset the sequential index. */
97 __libc_lock_unlock (lock);
103 /* Close the database file. */
105 internal_endent (void)
115 /* Thread-safe, exported version of that. */
117 _nss_db_endaliasent (void)
119 __libc_lock_lock (lock);
123 /* Reset STAYOPEN flag. */
126 __libc_lock_unlock (lock);
128 return NSS_STATUS_SUCCESS;
131 /* We provide the parse function here. The parser in libnss_files
132 cannot be used. The generation of the db file already resolved all
133 :include: statements so we simply have to parse the list and store
135 static enum nss_status
136 lookup (const DBT *key, struct aliasent *result, char *buffer,
137 size_t buflen, int *errnop)
139 enum nss_status status;
142 /* Open the database. */
143 status = internal_setent (keep_db);
144 if (status != NSS_STATUS_SUCCESS)
147 if ((*db->get) (db, key, &value, 0) == 0)
149 const char *src = value.data;
151 result->alias_members_len = 0;
153 /* We now have to fill the BUFFER with all the information. */
154 if (buflen < key->size + 1)
158 return NSS_STATUS_TRYAGAIN;
161 if (status == NSS_STATUS_SUCCESS)
166 buffer = stpncpy (buffer, key->data, key->size) + 1;
167 buflen -= key->size + 1;
171 const char *end, *upto;
172 while (isspace (*src))
175 end = strchr (src, ',');
177 end = strchr (src, '\0');
178 for (upto = end; upto > src && isspace (upto[-1]); --upto);
182 if ((upto - src) + __alignof__ (char *) > buflen)
184 buffer = stpncpy (buffer, src, upto - src) + 1;
185 buflen -= (upto - src) + __alignof (char *);
186 ++result->alias_members_len;
188 src = end + (*end != '\0');
191 /* Now prepare the return. Provide string pointers for the
192 currently selected aliases. */
194 /* Adjust the pointer so it is aligned for storing pointers. */
195 buffer += __alignof__ (char *) - 1;
196 buffer -= ((buffer - (char *) 0) % __alignof__ (char *));
197 result->alias_members = (char **) buffer;
199 /* Compute addresses of alias entry strings. */
200 cp = result->alias_name;
201 for (cnt = 0; cnt < result->alias_members_len; ++cnt)
203 cp = strchr (cp, '\0') + 1;
204 result->alias_members[cnt] = cp;
207 status = (result->alias_members_len == 0
208 ? NSS_STATUS_RETURN : NSS_STATUS_SUCCESS);
212 status = NSS_STATUS_NOTFOUND;
221 _nss_db_getaliasent_r (struct aliasent *result, char *buffer, size_t buflen,
224 /* Return next entry in alias file. */
225 enum nss_status status;
229 __libc_lock_lock (lock);
230 key.size = snprintf (key.data = buf, sizeof buf, "0%u", entidx++);
231 status = lookup (&key, result, buffer, buflen, errnop);
232 __libc_lock_unlock (lock);
239 _nss_db_getaliasbyname_r (const char *name, struct aliasent *result,
240 char *buffer, size_t buflen, int *errnop)
243 enum nss_status status;
245 key.size = 1 + strlen (name);
247 key.data = __alloca (key.size);
248 ((char *) key.data)[0] = '.';
249 memcpy (&((char *) key.data)[1], name, key.size - 1);
251 __libc_lock_lock (lock);
252 status = lookup (&key, result, buffer, buflen, errnop);
253 __libc_lock_unlock (lock);