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
;
51 err
= __nss_db_open (_PATH_VARDB
"aliases.db", DB_BTREE
, DB_RDONLY
, 0,
57 status
= NSS_STATUS_UNAVAIL
;
61 /* We have to make sure the file is `closed on exec'. */
65 err
= db
->fd (db
, &fd
);
72 result
= flags
= fcntl (fd
, F_GETFD
, 0);
76 result
= fcntl (fd
, F_SETFD
, flags
);
80 /* Something went wrong. Close the stream and return a
84 status
= NSS_STATUS_UNAVAIL
;
89 /* Remember STAYOPEN flag. */
97 /* Thread-safe, exported version of that. */
99 _nss_db_setaliasent (int stayopen
)
101 enum nss_status status
;
103 __libc_lock_lock (lock
);
105 status
= internal_setent (stayopen
);
107 /* Reset the sequential index. */
110 __libc_lock_unlock (lock
);
116 /* Close the database file. */
118 internal_endent (void)
128 /* Thread-safe, exported version of that. */
130 _nss_db_endaliasent (void)
132 __libc_lock_lock (lock
);
136 /* Reset STAYOPEN flag. */
139 __libc_lock_unlock (lock
);
141 return NSS_STATUS_SUCCESS
;
144 /* We provide the parse function here. The parser in libnss_files
145 cannot be used. The generation of the db file already resolved all
146 :include: statements so we simply have to parse the list and store
148 static enum nss_status
149 lookup (DBT
*key
, struct aliasent
*result
, char *buffer
,
150 size_t buflen
, int *errnop
)
152 enum nss_status status
;
155 /* Open the database. */
156 status
= internal_setent (keep_db
);
157 if (status
!= NSS_STATUS_SUCCESS
)
164 if (db
->get (db
, NULL
, key
, &value
, 0) == 0)
166 const char *src
= value
.data
;
168 result
->alias_members_len
= 0;
170 /* We now have to fill the BUFFER with all the information. */
171 if (buflen
< key
->size
+ 1)
175 return NSS_STATUS_TRYAGAIN
;
178 if (status
== NSS_STATUS_SUCCESS
)
183 buffer
= stpncpy (buffer
, key
->data
, key
->size
) + 1;
184 buflen
-= key
->size
+ 1;
188 const char *end
, *upto
;
189 while (isspace (*src
))
192 end
= strchr (src
, ',');
194 end
= strchr (src
, '\0');
195 for (upto
= end
; upto
> src
&& isspace (upto
[-1]); --upto
);
199 if ((upto
- src
) + __alignof__ (char *) > buflen
)
201 buffer
= stpncpy (buffer
, src
, upto
- src
) + 1;
202 buflen
-= (upto
- src
) + __alignof (char *);
203 ++result
->alias_members_len
;
205 src
= end
+ (*end
!= '\0');
208 /* Now prepare the return. Provide string pointers for the
209 currently selected aliases. */
211 /* Adjust the pointer so it is aligned for storing pointers. */
212 buffer
+= __alignof__ (char *) - 1;
213 buffer
-= ((buffer
- (char *) 0) % __alignof__ (char *));
214 result
->alias_members
= (char **) buffer
;
216 /* Compute addresses of alias entry strings. */
217 cp
= result
->alias_name
;
218 for (cnt
= 0; cnt
< result
->alias_members_len
; ++cnt
)
220 cp
= strchr (cp
, '\0') + 1;
221 result
->alias_members
[cnt
] = cp
;
224 status
= (result
->alias_members_len
== 0
225 ? NSS_STATUS_RETURN
: NSS_STATUS_SUCCESS
);
229 status
= NSS_STATUS_NOTFOUND
;
238 _nss_db_getaliasent_r (struct aliasent
*result
, char *buffer
, size_t buflen
,
241 /* Return next entry in alias file. */
242 enum nss_status status
;
246 __libc_lock_lock (lock
);
247 key
.size
= snprintf (key
.data
= buf
, sizeof buf
, "0%u", entidx
++);
249 status
= lookup (&key
, result
, buffer
, buflen
, errnop
);
250 if (status
== NSS_STATUS_TRYAGAIN
&& *errnop
== ERANGE
)
251 /* Give the user a chance to get the same entry with a larger buffer. */
253 __libc_lock_unlock (lock
);
260 _nss_db_getaliasbyname_r (const char *name
, struct aliasent
*result
,
261 char *buffer
, size_t buflen
, int *errnop
)
264 enum nss_status status
;
266 key
.size
= 1 + strlen (name
);
268 key
.data
= __alloca (key
.size
);
269 ((char *) key
.data
)[0] = '.';
270 memcpy (&((char *) key
.data
)[1], name
, key
.size
- 1);
273 __libc_lock_lock (lock
);
274 status
= lookup (&key
, result
, buffer
, buflen
, errnop
);
275 __libc_lock_unlock (lock
);