2004-02-24 06:29:32 +03:00
/*
2005-03-06 08:16:52 +03:00
* udev_libc_wrapper - wrapping of functions missing in a specific libc
* or not working in a statically compiled binary
2004-02-24 06:29:32 +03:00
*
* Copyright ( C ) 2003 Greg Kroah - Hartman < greg @ kroah . com >
2005-03-06 08:16:52 +03:00
* Copyright ( C ) 2005 Kay Sievers < kay @ vrfy . org >
2004-02-24 06:29:32 +03:00
*
* This program is free software ; you can redistribute it and / or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation version 2 of the License .
*
* This program is distributed in the hope that it will be useful , but
* WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
* General Public License for more details .
*
* You should have received a copy of the GNU General Public License along
* with this program ; if not , write to the Free Software Foundation , Inc . ,
* 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*
*/
2003-10-23 10:48:55 +04:00
# include <stdlib.h>
2004-02-24 06:29:32 +03:00
# include <stdio.h>
2003-10-23 10:48:55 +04:00
# include <string.h>
# include <ctype.h>
2004-03-02 09:42:30 +03:00
# include <fcntl.h>
2006-02-15 23:12:49 +03:00
# include <errno.h>
2003-11-24 07:44:41 +03:00
# include <sys/types.h>
2004-02-24 06:29:32 +03:00
2005-03-18 11:27:31 +03:00
# include "udev.h"
2004-02-24 06:29:32 +03:00
2005-03-06 08:16:52 +03:00
# ifdef __KLIBC__
# define __OWN_USERDB_PARSER__
# endif
2005-03-07 06:29:43 +03:00
2005-03-27 14:39:12 +04:00
# ifdef __GLIBC__
# define __OWN_STRLCPYCAT__
# endif
2005-03-06 08:16:52 +03:00
# ifdef USE_STATIC
# define __OWN_USERDB_PARSER__
# endif
2005-03-27 14:39:12 +04:00
# ifdef __OWN_STRLCPYCAT__
2005-03-07 06:29:43 +03:00
size_t strlcpy ( char * dst , const char * src , size_t size )
{
size_t bytes = 0 ;
char * q = dst ;
const char * p = src ;
char ch ;
while ( ( ch = * p + + ) ) {
2005-03-27 14:39:12 +04:00
if ( bytes + 1 < size )
2005-03-07 06:29:43 +03:00
* q + + = ch ;
bytes + + ;
}
2005-03-29 05:53:45 +04:00
/* If size == 0 there is no space for a final null... */
2005-03-28 02:18:49 +04:00
if ( size )
* q = ' \0 ' ;
2005-03-29 05:53:45 +04:00
2005-03-07 06:29:43 +03:00
return bytes ;
}
size_t strlcat ( char * dst , const char * src , size_t size )
{
size_t bytes = 0 ;
char * q = dst ;
const char * p = src ;
char ch ;
while ( bytes < size & & * q ) {
q + + ;
bytes + + ;
}
2005-03-27 14:39:12 +04:00
if ( bytes = = size )
return ( bytes + strlen ( src ) ) ;
2005-03-06 08:16:52 +03:00
2005-03-07 06:29:43 +03:00
while ( ( ch = * p + + ) ) {
2005-03-27 14:39:12 +04:00
if ( bytes + 1 < size )
2005-03-07 06:29:43 +03:00
* q + + = ch ;
bytes + + ;
}
2005-03-29 05:53:45 +04:00
* q = ' \0 ' ;
2005-03-07 06:29:43 +03:00
return bytes ;
}
2005-03-27 14:39:12 +04:00
# endif /* __OWN_STRLCPYCAT__ */
2005-03-07 06:29:43 +03:00
# ifndef __OWN_USERDB_PARSER__
2005-03-06 08:16:52 +03:00
# include <sys/types.h>
# include <pwd.h>
# include <grp.h>
uid_t lookup_user ( const char * user )
{
struct passwd * pw ;
uid_t uid = 0 ;
pw = getpwnam ( user ) ;
if ( pw = = NULL )
2005-11-07 20:52:03 +03:00
info ( " specified user unknown '%s' " , user ) ;
2005-03-06 08:16:52 +03:00
else
uid = pw - > pw_uid ;
return uid ;
}
gid_t lookup_group ( const char * group )
{
struct group * gr ;
gid_t gid = 0 ;
gr = getgrnam ( group ) ;
if ( gr = = NULL )
2005-11-07 20:52:03 +03:00
info ( " specified group unknown '%s' " , group ) ;
2005-03-06 08:16:52 +03:00
else
gid = gr - > gr_gid ;
return gid ;
}
# else /* __OWN_USERDB_PARSER__ */
# define PASSWD_FILE " / etc / passwd"
# define GROUP_FILE " / etc / group"
2004-03-02 09:42:30 +03:00
2004-02-24 06:29:32 +03:00
/* return the id of a passwd style line, selected by the users name */
static unsigned long get_id_by_name ( const char * uname , const char * dbfile )
{
2005-03-06 08:16:52 +03:00
unsigned long id = 0 ;
2004-09-11 07:54:04 +04:00
char line [ LINE_SIZE ] ;
2004-03-23 09:22:20 +03:00
char * buf ;
2004-09-11 07:54:04 +04:00
char * bufline ;
2004-03-23 09:22:20 +03:00
size_t bufsize ;
size_t cur ;
size_t count ;
2004-02-24 06:29:32 +03:00
char * pos ;
char * name ;
char * idstr ;
char * tail ;
2005-03-06 08:16:52 +03:00
if ( file_map ( dbfile , & buf , & bufsize ) ! = 0 ) {
2005-11-07 20:52:03 +03:00
err ( " can't open '%s' as db file: %s " , dbfile , strerror ( errno ) ) ;
2005-03-06 08:16:52 +03:00
return 0 ;
2004-02-24 06:29:32 +03:00
}
2005-03-18 12:00:25 +03:00
dbg ( " search '%s' in '%s' " , uname , dbfile ) ;
2004-02-24 06:29:32 +03:00
2004-03-23 09:22:20 +03:00
/* loop through the whole file */
cur = 0 ;
2004-09-11 07:54:04 +04:00
while ( cur < bufsize ) {
2004-03-23 09:22:20 +03:00
count = buf_get_line ( buf , bufsize , cur ) ;
2004-09-11 07:54:04 +04:00
bufline = & buf [ cur ] ;
cur + = count + 1 ;
2005-03-07 06:29:43 +03:00
if ( count > = sizeof ( line ) )
2004-09-11 07:54:04 +04:00
continue ;
2004-03-23 09:22:20 +03:00
2005-08-08 04:21:55 +04:00
memcpy ( line , bufline , count - 1 ) ;
line [ count - 1 ] = ' \0 ' ;
2004-03-23 09:22:20 +03:00
pos = line ;
2004-02-24 06:29:32 +03:00
/* get name */
name = strsep ( & pos , " : " ) ;
if ( name = = NULL )
continue ;
/* skip pass */
if ( strsep ( & pos , " : " ) = = NULL )
continue ;
/* get id */
idstr = strsep ( & pos , " : " ) ;
if ( idstr = = NULL )
continue ;
if ( strcmp ( uname , name ) = = 0 ) {
id = strtoul ( idstr , & tail , 10 ) ;
2005-03-06 08:16:52 +03:00
if ( tail [ 0 ] ! = ' \0 ' ) {
id = 0 ;
dbg ( " no id found for '%s' " , name ) ;
} else
2004-02-24 06:29:32 +03:00
dbg ( " id for '%s' is '%li' " , name , id ) ;
break ;
}
}
2004-03-23 09:22:20 +03:00
file_unmap ( buf , bufsize ) ;
2004-02-24 06:29:32 +03:00
return id ;
}
2005-03-06 08:16:52 +03:00
uid_t lookup_user ( const char * user )
2004-02-24 06:29:32 +03:00
{
2005-03-06 08:16:52 +03:00
unsigned long id ;
2004-02-24 06:29:32 +03:00
2005-03-06 08:16:52 +03:00
id = get_id_by_name ( user , PASSWD_FILE ) ;
return ( uid_t ) id ;
2004-02-24 06:29:32 +03:00
}
2005-03-06 08:16:52 +03:00
gid_t lookup_group ( const char * group )
2004-02-24 06:29:32 +03:00
{
2005-03-06 08:16:52 +03:00
unsigned long id ;
2004-02-24 06:29:32 +03:00
2005-03-06 08:16:52 +03:00
id = get_id_by_name ( group , GROUP_FILE ) ;
return ( gid_t ) id ;
2004-02-24 06:29:32 +03:00
}
2005-03-06 08:16:52 +03:00
# endif /* __OWN_USERDB_PARSER__ */