From a0a225f4aaa36461554db637edf24291acb17da6 Mon Sep 17 00:00:00 2001 From: Joel Sherrill Date: Fri, 2 Jul 1999 18:50:40 +0000 Subject: Added code to initialize the /etc/group and /etc/passwd files. --- c/src/exec/libcsupport/src/getgrent.c | 22 +++++++++++ c/src/exec/libcsupport/src/getpwent.c | 69 +++++++++++++++++++++++++++++++++++ c/src/lib/libc/getgrent.c | 22 +++++++++++ c/src/lib/libc/getpwent.c | 69 +++++++++++++++++++++++++++++++++++ 4 files changed, 182 insertions(+) (limited to 'c') diff --git a/c/src/exec/libcsupport/src/getgrent.c b/c/src/exec/libcsupport/src/getgrent.c index b47ece8283..da339718a0 100644 --- a/c/src/exec/libcsupport/src/getgrent.c +++ b/c/src/exec/libcsupport/src/getgrent.c @@ -1,7 +1,14 @@ /* + * POSIX 1003.1b - 9.2.1 - Group Database Access Routines + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.OARcorp.com/rtems/license.html. + * * $Id$ */ + #include #include #include @@ -14,11 +21,20 @@ static struct group gr_group; /* password structure */ static FILE *group_fp; +/* + * The size of these buffers is arbitrary and there is no provision + * to protect any of them from overflowing. The scanf patterns + * need to be changed to prevent overflowing. In addition, + * the limits on these needs to be examined. + */ + static char groupname[8]; static char password[1024]; static char groups[1024]; static char *gr_mem[16] = { } ; +extern void init_etc_passwd_group(void); + int getgrnam_r( const char *name, struct group *grp, @@ -29,6 +45,8 @@ int getgrnam_r( { FILE *fp; + init_etc_passwd_group(); + if ((fp = fopen ("/etc/group", "r")) == NULL) { errno = EINVAL; return -1; @@ -76,6 +94,8 @@ int getgrgid_r( { FILE *fp; + init_etc_passwd_group(); + if ((fp = fopen ("/etc/group", "r")) == NULL) { errno = EINVAL; return -1; @@ -137,6 +157,8 @@ struct group *getgrent( void ) void setgrent () { + init_etc_passwd_group(); + if (group_fp != NULL) fclose (group_fp); diff --git a/c/src/exec/libcsupport/src/getpwent.c b/c/src/exec/libcsupport/src/getpwent.c index 5a9b0f96c6..06f7318d27 100644 --- a/c/src/exec/libcsupport/src/getpwent.c +++ b/c/src/exec/libcsupport/src/getpwent.c @@ -1,3 +1,13 @@ +/* + * POSIX 1003.1b - 9.2.2 - User Database Access Routines + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.OARcorp.com/rtems/license.html. + * + * $Id$ + */ + #include #include #include @@ -6,10 +16,19 @@ #include #include #include +#include +#include static struct passwd pw_passwd; /* password structure */ static FILE *passwd_fp; +/* + * The size of these buffers is arbitrary and there is no provision + * to protect any of them from overflowing. The scanf patterns + * need to be changed to prevent overflowing. In addition, + * the limits on these needs to be examined. + */ + static char logname[8]; static char password[1024]; static char comment[1024]; @@ -17,6 +36,50 @@ static char gecos[1024]; static char dir[1024]; static char shell[1024]; +/* + * Initialize a useable but dummy /etc/passwd + * + * NOTE: Ignore all errors. + * + */ + +static char etc_passwd_initted = 0; + +void init_etc_passwd_group(void) +{ + FILE *fp; + + if ( etc_passwd_initted ) + return; + etc_passwd_initted = 1; + + (void) mkdir( "/etc", S_IRWXU | S_IRWXG | S_IRWXO ); + + /* + * Initialize /etc/passwd + */ + + if ((fp = fopen ("/etc/passwd", "w")) == NULL) + return; + + fprintf( fp, "root:*:0:0:root,,,,:/tmp:/bin/false\n" + "rtems:*:1:1:RTEMS Application,,,,:/tmp:/bin/false\n" ); + + fclose( fp ); + + /* + * Initialize /etc/group + */ + + if ((fp = fopen ("/etc/group", "w")) == NULL) + return; + + fprintf( fp, "root::0:root\n" + "rtems::0:rtems\n" ); + + fclose( fp ); +} + int getpwnam_r( const char *name, struct passwd *pwd, @@ -27,6 +90,8 @@ int getpwnam_r( { FILE *fp; + init_etc_passwd_group(); + if ((fp = fopen ("/etc/passwd", "r")) == NULL) { errno = EINVAL; return -1; @@ -78,6 +143,8 @@ int getpwuid_r( { FILE *fp; + init_etc_passwd_group(); + if ((fp = fopen ("/etc/passwd", "r")) == NULL) { errno = EINVAL; return -1; @@ -145,6 +212,8 @@ struct passwd *getpwent() void setpwent( void ) { + init_etc_passwd_group(); + if (passwd_fp != NULL) fclose (passwd_fp); diff --git a/c/src/lib/libc/getgrent.c b/c/src/lib/libc/getgrent.c index b47ece8283..da339718a0 100644 --- a/c/src/lib/libc/getgrent.c +++ b/c/src/lib/libc/getgrent.c @@ -1,7 +1,14 @@ /* + * POSIX 1003.1b - 9.2.1 - Group Database Access Routines + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.OARcorp.com/rtems/license.html. + * * $Id$ */ + #include #include #include @@ -14,11 +21,20 @@ static struct group gr_group; /* password structure */ static FILE *group_fp; +/* + * The size of these buffers is arbitrary and there is no provision + * to protect any of them from overflowing. The scanf patterns + * need to be changed to prevent overflowing. In addition, + * the limits on these needs to be examined. + */ + static char groupname[8]; static char password[1024]; static char groups[1024]; static char *gr_mem[16] = { } ; +extern void init_etc_passwd_group(void); + int getgrnam_r( const char *name, struct group *grp, @@ -29,6 +45,8 @@ int getgrnam_r( { FILE *fp; + init_etc_passwd_group(); + if ((fp = fopen ("/etc/group", "r")) == NULL) { errno = EINVAL; return -1; @@ -76,6 +94,8 @@ int getgrgid_r( { FILE *fp; + init_etc_passwd_group(); + if ((fp = fopen ("/etc/group", "r")) == NULL) { errno = EINVAL; return -1; @@ -137,6 +157,8 @@ struct group *getgrent( void ) void setgrent () { + init_etc_passwd_group(); + if (group_fp != NULL) fclose (group_fp); diff --git a/c/src/lib/libc/getpwent.c b/c/src/lib/libc/getpwent.c index 5a9b0f96c6..06f7318d27 100644 --- a/c/src/lib/libc/getpwent.c +++ b/c/src/lib/libc/getpwent.c @@ -1,3 +1,13 @@ +/* + * POSIX 1003.1b - 9.2.2 - User Database Access Routines + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.OARcorp.com/rtems/license.html. + * + * $Id$ + */ + #include #include #include @@ -6,10 +16,19 @@ #include #include #include +#include +#include static struct passwd pw_passwd; /* password structure */ static FILE *passwd_fp; +/* + * The size of these buffers is arbitrary and there is no provision + * to protect any of them from overflowing. The scanf patterns + * need to be changed to prevent overflowing. In addition, + * the limits on these needs to be examined. + */ + static char logname[8]; static char password[1024]; static char comment[1024]; @@ -17,6 +36,50 @@ static char gecos[1024]; static char dir[1024]; static char shell[1024]; +/* + * Initialize a useable but dummy /etc/passwd + * + * NOTE: Ignore all errors. + * + */ + +static char etc_passwd_initted = 0; + +void init_etc_passwd_group(void) +{ + FILE *fp; + + if ( etc_passwd_initted ) + return; + etc_passwd_initted = 1; + + (void) mkdir( "/etc", S_IRWXU | S_IRWXG | S_IRWXO ); + + /* + * Initialize /etc/passwd + */ + + if ((fp = fopen ("/etc/passwd", "w")) == NULL) + return; + + fprintf( fp, "root:*:0:0:root,,,,:/tmp:/bin/false\n" + "rtems:*:1:1:RTEMS Application,,,,:/tmp:/bin/false\n" ); + + fclose( fp ); + + /* + * Initialize /etc/group + */ + + if ((fp = fopen ("/etc/group", "w")) == NULL) + return; + + fprintf( fp, "root::0:root\n" + "rtems::0:rtems\n" ); + + fclose( fp ); +} + int getpwnam_r( const char *name, struct passwd *pwd, @@ -27,6 +90,8 @@ int getpwnam_r( { FILE *fp; + init_etc_passwd_group(); + if ((fp = fopen ("/etc/passwd", "r")) == NULL) { errno = EINVAL; return -1; @@ -78,6 +143,8 @@ int getpwuid_r( { FILE *fp; + init_etc_passwd_group(); + if ((fp = fopen ("/etc/passwd", "r")) == NULL) { errno = EINVAL; return -1; @@ -145,6 +212,8 @@ struct passwd *getpwent() void setpwent( void ) { + init_etc_passwd_group(); + if (passwd_fp != NULL) fclose (passwd_fp); -- cgit v1.2.3