File indexing completed on 2025-05-11 08:24:34
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 #ifdef HAVE_CONFIG_H
0029 #include "config.h"
0030 #endif
0031
0032 #include <sys/stat.h>
0033 #include <sys/types.h>
0034 #include <errno.h>
0035 #include <fcntl.h>
0036 #include <grp.h>
0037 #include <pwd.h>
0038 #include <stdio.h>
0039 #include <unistd.h>
0040
0041 #include "tmacros.h"
0042
0043 const char rtems_test_name[] = "PWDGRP 2";
0044
0045 static void assert_pwd(struct passwd *pwd)
0046 {
0047 rtems_test_assert(strcmp(pwd->pw_name, "root") == 0);
0048 rtems_test_assert(strcmp(pwd->pw_passwd, "") == 0);
0049 rtems_test_assert(pwd->pw_uid == 0);
0050 rtems_test_assert(pwd->pw_gid == 0);
0051 rtems_test_assert(strcmp(pwd->pw_comment, "") == 0);
0052 rtems_test_assert(strcmp(pwd->pw_gecos, "") == 0);
0053 rtems_test_assert(strcmp(pwd->pw_dir, "") == 0);
0054 rtems_test_assert(strcmp(pwd->pw_shell, "") == 0);
0055 }
0056
0057 static void assert_grp(struct group *grp)
0058 {
0059 rtems_test_assert(strcmp(grp->gr_name, "root") == 0);
0060 rtems_test_assert(strcmp(grp->gr_passwd, "") == 0);
0061 rtems_test_assert(grp->gr_gid == 0);
0062 rtems_test_assert(grp->gr_mem[0] == NULL);
0063 }
0064
0065 static void assert_dir(const char *name)
0066 {
0067 int rv;
0068 struct stat st;
0069
0070 rv = lstat(name, &st);
0071 rtems_test_assert(rv == 0);
0072 rtems_test_assert(st.st_uid == 0);
0073 rtems_test_assert(st.st_gid == 0);
0074 rtems_test_assert(
0075 st.st_mode == (S_IFDIR | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
0076 );
0077 }
0078
0079 static void test(void)
0080 {
0081 int rv;
0082 struct passwd pwd;
0083 struct group grp;
0084 struct passwd *pwd_res;
0085 struct group *grp_res;
0086 char buf[256];
0087
0088 rtems_test_assert(getuid() == 0);
0089 rtems_test_assert(geteuid() == 0);
0090
0091 rtems_test_assert(getgid() == 0);
0092 rtems_test_assert(getegid() == 0);
0093
0094 memset(&pwd, 0xff, sizeof(pwd));
0095 rv = getpwnam_r("root", &pwd, &buf[0], sizeof(buf), &pwd_res);
0096 rtems_test_assert(rv == 0);
0097 rtems_test_assert(&pwd == pwd_res);
0098 assert_pwd(pwd_res);
0099
0100 memset(&pwd, 0xff, sizeof(pwd));
0101 rv = getpwuid_r(0, &pwd, &buf[0], sizeof(buf), &pwd_res);
0102 rtems_test_assert(rv == 0);
0103 rtems_test_assert(&pwd == pwd_res);
0104 assert_pwd(pwd_res);
0105
0106 memset(&grp, 0xff, sizeof(grp));
0107 rv = getgrnam_r("root", &grp, &buf[0], sizeof(buf), &grp_res);
0108 rtems_test_assert(rv == 0);
0109 rtems_test_assert(&grp == grp_res);
0110 assert_grp(grp_res);
0111
0112 memset(&grp, 0xff, sizeof(grp));
0113 rv = getgrgid_r(0, &grp, &buf[0], sizeof(buf), &grp_res);
0114 rtems_test_assert(rv == 0);
0115 rtems_test_assert(&grp == grp_res);
0116 assert_grp(grp_res);
0117
0118 assert_dir("/dev");
0119 assert_dir("/etc");
0120
0121 rv = setuid(1);
0122 rtems_test_assert(rv == 0);
0123
0124 rv = seteuid(1);
0125 rtems_test_assert(rv == 0);
0126
0127 errno = 0;
0128 rv = unlink("/etc/passwd");
0129 rtems_test_assert(rv == -1);
0130 rtems_test_assert(errno == EACCES);
0131
0132 errno = 0;
0133 rv = unlink("/etc/group");
0134 rtems_test_assert(rv == -1);
0135 rtems_test_assert(errno == EACCES);
0136
0137 errno = 0;
0138 rv = open("/etc/passwd", O_RDONLY);
0139 rtems_test_assert(rv == -1);
0140 rtems_test_assert(errno == EACCES);
0141
0142 errno = 0;
0143 rv = open("/etc/group", O_RDONLY);
0144 rtems_test_assert(rv == -1);
0145 rtems_test_assert(errno == EACCES);
0146
0147 errno = 0;
0148 rv = open("/etc/passwd", O_WRONLY);
0149 rtems_test_assert(rv == -1);
0150 rtems_test_assert(errno == EACCES);
0151
0152 errno = 0;
0153 rv = open("/etc/group", O_WRONLY);
0154 rtems_test_assert(rv == -1);
0155 rtems_test_assert(errno == EACCES);
0156
0157 errno = 0;
0158 rv = open("/etc/passwd", 0);
0159 rtems_test_assert(rv == -1);
0160 rtems_test_assert(errno == EACCES);
0161
0162 errno = 0;
0163 rv = open("/etc/group", 0);
0164 rtems_test_assert(rv == -1);
0165 rtems_test_assert(errno == EACCES);
0166 }
0167
0168 static void Init(rtems_task_argument arg)
0169 {
0170 TEST_BEGIN();
0171
0172 test();
0173
0174 TEST_END();
0175 rtems_test_exit(0);
0176 }
0177
0178 #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
0179 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0180
0181 #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 4
0182
0183 #define CONFIGURE_MAXIMUM_TASKS 1
0184
0185 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0186
0187 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0188
0189 #define CONFIGURE_INIT
0190
0191 #include <rtems/confdefs.h>