summaryrefslogtreecommitdiffstats
path: root/cpukit/libcsupport/src/pwdgrp.c
diff options
context:
space:
mode:
authorSebastian Huber <sebastian.huber@embedded-brains.de>2014-11-14 11:30:50 +0100
committerSebastian Huber <sebastian.huber@embedded-brains.de>2014-11-20 10:30:22 +0100
commit6935428a29ece41828cb55c6c22710a7c1535010 (patch)
treea7beb5fab51f19bc50013c27bd96d90518d93b71 /cpukit/libcsupport/src/pwdgrp.c
parentlibcsupport: Use pthread_once() (diff)
downloadrtems-6935428a29ece41828cb55c6c22710a7c1535010.tar.bz2
libcsupport: Avoid TOCTOU and format errors
Diffstat (limited to 'cpukit/libcsupport/src/pwdgrp.c')
-rw-r--r--cpukit/libcsupport/src/pwdgrp.c42
1 files changed, 22 insertions, 20 deletions
diff --git a/cpukit/libcsupport/src/pwdgrp.c b/cpukit/libcsupport/src/pwdgrp.c
index c10cbab719..a531b8b2ff 100644
--- a/cpukit/libcsupport/src/pwdgrp.c
+++ b/cpukit/libcsupport/src/pwdgrp.c
@@ -39,40 +39,42 @@
static pthread_once_t pwdgrp_once = PTHREAD_ONCE_INIT;
+static void init_file(const char *name, const char *content)
+{
+ FILE *fp = fopen(name, "wx");
+
+ if (fp != NULL) {
+ fputs(content, fp);
+ fclose(fp);
+ }
+}
+
/**
* Initialize useable but dummy databases
*/
static void pwdgrp_init(void)
{
- FILE *fp;
-
mkdir("/etc", 0777);
/*
* Initialize /etc/passwd
*/
- if ((fp = fopen("/etc/passwd", "r")) != NULL) {
- fclose(fp);
- }
- else if ((fp = fopen("/etc/passwd", "w")) != NULL) {
- fprintf(fp, "root:*:0:0:root::/:/bin/sh\n"
- "rtems:*:1:1:RTEMS Application::/:/bin/sh\n"
- "tty:!:2:2:tty owner::/:/bin/false\n" );
- fclose(fp);
- }
+ init_file(
+ "/etc/passwd",
+ "root:*:0:0:root::/:/bin/sh\n"
+ "rtems:*:1:1:RTEMS Application::/:/bin/sh\n"
+ "tty:!:2:2:tty owner::/:/bin/false\n"
+ );
/*
* Initialize /etc/group
*/
- if ((fp = fopen("/etc/group", "r")) != NULL) {
- fclose(fp);
- }
- else if ((fp = fopen("/etc/group", "w")) != NULL) {
- fprintf( fp, "root:x:0:root\n"
- "rtems:x:1:rtems\n"
- "tty:x:2:tty\n" );
- fclose(fp);
- }
+ init_file(
+ "/etc/group",
+ "root:x:0:root\n"
+ "rtems:x:1:rtems\n"
+ "tty:x:2:tty\n"
+ );
}
void _libcsupport_pwdgrp_init(void)