Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/tests/testpw6.c
blob: 6d776caaf0bfc93ed2d3d63e13cee08845391aa9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187

/* $Id$ */

/* This program just tests getpwent/getpwnam. You want to have nss_ldap
 * plugged in, so to speak, to test anything useful.
 */

#include "config.h"

#ifdef _REENTRANT
#ifdef HAVE_PTHREAD_H
#include <pthread.h>
#else
#include <thread.h>
#endif /* _REENTRANT */

#endif /* HAVE_PTHREAD_H */
#include <stdio.h>
#include <pwd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

void test_passwd (void);
void scan_passwd (void);

int ARGC;
char **ARGV;

#define MAX_THREADS 8

void
main (int argc, char **argv)
{
#ifdef _REENTRANT
  int i;
#ifdef HAVE_PTHREAD_H
  pthread_t tids[MAX_THREADS];
#endif
#endif
  pid_t pid;
  ARGC = argc;
  ARGV = argv;

  /* PRIME */
  scan_passwd();

   pid = fork();
   if (pid == 0) {
	printf("IN CHILD\n");
   } else {
	printf("IN PARENT\n");
   }

#ifdef _REENTRANT
  for (i = 0; i < MAX_THREADS; i++)
    {
#ifdef HAVE_PTHREAD_H
      pthread_create(&tids[i], NULL, test_passwd, NULL);
#else
      thread_t tid;
      thr_create (NULL, 0, test_passwd, NULL, 0, &tid);
      thr_continue (tid);
#endif /* HAVE_PTHREAD_H */
    }
#ifdef HAVE_PTHREAD_H
  for (i = 0; i < MAX_THREADS; i++) pthread_join(tids[i], NULL);
#else
  while (thr_join (NULL, NULL, NULL) == 0);
#endif
#else
  test_passwd ();
#endif
  exit (0);
}

#ifdef _REENTRANT
static void
ret (int status)
{
#ifdef HAVE_PTHREAD_H
  pthread_exit(&status);
#else
  thr_exit (&status);
#endif
}
#else
#define ret exit
#endif

void
test_passwd (void)
{
  struct passwd *pw;
  uid_t uid;
#ifdef _REENTRANT
  char buf[1024];
  struct passwd pbuf;
#endif
  int pid;

  printf (">>>>>> getpwnam(\"%s\")\n", ARGC > 1 ? ARGV[1] : "testuser");
#ifdef _REENTRANT
#if GETHOSTBYNAME_R_ARGS == 6
  if (getpwnam_r (ARGC > 1 ? ARGV[1] : "testuser", &pbuf, buf, sizeof (buf), &pw) != 0) pw = NULL;
#else
  pw = getpwnam_r (ARGC > 1 ? ARGV[1] : "testuser", &pbuf, buf, sizeof (buf));
#endif
#else
  pw = getpwnam (ARGC > 1 ? ARGV[1] : "testuser");
#endif

  if (!pw)
    ret (1);

  printf ("%s:%s:%d:%d:%s:%s:%s\n", pw->pw_name, pw->pw_passwd, pw->pw_uid,
	  pw->pw_gid, pw->pw_gecos, pw->pw_dir, pw->pw_shell);
  uid = pw->pw_uid;


  printf (">>>>>> getpwuid(%d)\n", uid);

#ifdef _REENTRANT
#if GETHOSTBYNAME_R_ARGS == 6
  if (getpwuid_r (uid, &pbuf, buf, sizeof(buf), &pw) != 0) pw = NULL;
#else
  pw = getpwuid_r (uid, &pbuf, buf, sizeof (buf));
#endif
#else
  pw = getpwuid (uid);
#endif

  if (!pw)
    ret (1);


  printf ("%s:%s:%d:%d:%s:%s:%s\n", pw->pw_name, pw->pw_passwd, pw->pw_uid,
	  pw->pw_gid, pw->pw_gecos, pw->pw_dir, pw->pw_shell);

  if (ARGC > 2 && !strcmp (ARGV[2], "no"))
    {
      printf (">>>>>> Enumeration skipped.\n");
    }
  else
    {
      printf (">>>>>> setpwent()\n");
      setpwent ();

      printf (">>>>>> getpwent()\n");
      scan_passwd ();

      printf (">>>>>> endpwent()\n");
      endpwent ();
    }

  ret (0);
}

void
scan_passwd (void)
{
  int i = 1;
  struct passwd *p;
#ifdef _REENTRANT
  char buf[1024];
  struct passwd pbuf;
#endif


#ifdef _REENTRANT
#if GETHOSTBYNAME_R_ARGS == 6
  while (getpwent_r (&pbuf, buf, sizeof (buf), &p) == 0 && p != NULL)
#else
  while ((p = getpwent_r (&pbuf, buf, sizeof (buf))) != NULL)
#endif
#else
  while ((p = getpwent ()) != NULL)
#endif
    {
      printf ("%s:%s:%d:%d:%s:%s:%s\n",
	      p->pw_name,
	      p->pw_passwd,
	      p->pw_uid, p->pw_gid, p->pw_gecos, p->pw_dir, p->pw_shell);
      i++;
    }
}