source: rtems/c/src/lib/libc/getpwent.c @ 9c0bd65

4.104.114.84.95
Last change on this file since 9c0bd65 was 9c49db4, checked in by Joel Sherrill <joel.sherrill@…>, on 01/08/01 at 18:26:44

2001-01-08 Ralf Corsepius <corsepiu@…>

  • configure.in: Add libc/config.h
  • libc/Makefile.am: Add INCLUDES += -I. to pickup config.h
  • libc/.cvsignore: Add config.h and stamp-h
  • libc/*.c: Add config.h support.
  • Property mode set to 100644
File size: 4.3 KB
Line 
1/*
2 *  POSIX 1003.1b - 9.2.2 - User Database Access Routines
3 *
4 *  The license and distribution terms for this file may be
5 *  found in the file LICENSE in this distribution or at
6 *  http://www.OARcorp.com/rtems/license.html.
7 *
8 *  $Id$
9 */
10
11#if HAVE_CONFIG_H
12#include "config.h"
13#endif
14
15#include <stdio.h>
16#include <sys/types.h>
17#include <pwd.h>
18#include <errno.h>
19#include <unistd.h>
20#include <stdlib.h>
21#include <string.h>
22#include <limits.h>
23#include <fcntl.h>
24#include <unistd.h>
25
26static struct passwd pw_passwd;  /* password structure */
27static FILE *passwd_fp;
28
29/*
30 *  The size of these buffers is arbitrary and there is no provision
31 *  to protect any of them from overflowing.  The scanf patterns
32 *  need to be changed to prevent overflowing.  In addition,
33 *  the limits on these needs to be examined.
34 */
35
36static char logname[8];
37static char password[1024];
38static char comment[1024];
39static char gecos[1024];
40static char dir[1024];
41static char shell[1024];
42
43/*
44 *  Initialize a useable but dummy /etc/passwd
45 *
46 *  NOTE: Ignore all errors.
47 *
48 */
49
50static char etc_passwd_initted = 0;
51
52void init_etc_passwd_group(void)
53{
54  FILE *fp;
55
56  if ( etc_passwd_initted )
57    return;
58  etc_passwd_initted = 1;
59 
60  (void) mkdir( "/etc", S_IRWXU | S_IRWXG | S_IRWXO );
61
62  /*
63   *  Initialize /etc/passwd
64   */
65
66  if ((fp = fopen ("/etc/passwd", "w")) == NULL)
67    return;
68
69  fprintf( fp, "root:*:0:0:root,,,,:/tmp:/bin/false\n"
70               "rtems:*:1:1:RTEMS Application,,,,:/tmp:/bin/false\n" );
71
72  fclose( fp );
73
74  /*
75   *  Initialize /etc/group
76   */
77
78  if ((fp = fopen ("/etc/group", "w")) == NULL)
79    return;
80
81  fprintf( fp, "root::0:root\n"
82               "rtems::0:rtems\n" );
83
84  fclose( fp );
85}
86
87int getpwnam_r(
88  const char     *name,
89  struct passwd  *pwd,
90  char           *buffer,
91  size_t          bufsize,
92  struct passwd **result
93)
94{
95  FILE *fp;
96
97  init_etc_passwd_group();
98
99  if ((fp = fopen ("/etc/passwd", "r")) == NULL) {
100    errno = EINVAL;
101    return -1;
102  }
103
104  while (fgets (buffer, bufsize, fp)) {
105    sscanf (buffer, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%[^:]:%s\n",
106        logname, password, &pwd->pw_uid,
107        &pwd->pw_gid, comment, gecos,
108        dir, shell);
109    pwd->pw_name = logname;
110    pwd->pw_passwd = password;
111    pwd->pw_comment = comment;
112    pwd->pw_gecos = gecos;
113    pwd->pw_dir = dir;
114    pwd->pw_shell = shell;
115
116    if (!strcmp (logname, name)) {
117      fclose (fp);
118      *result = pwd;
119      return 0;
120    }
121  }
122  fclose (fp);
123  errno = EINVAL;
124  return -1;
125}
126
127struct passwd *getpwnam(
128  const char *name
129)
130{
131  char   buf[1024];
132  struct passwd *p;
133
134  if ( getpwnam_r( name, &pw_passwd, buf, 1024, &p ) )
135    return NULL;
136
137  return p;
138}
139
140int getpwuid_r(
141  uid_t           uid,
142  struct passwd  *pwd,
143  char           *buffer,
144  size_t          bufsize,
145  struct passwd **result
146)
147{
148  FILE *fp;
149
150  init_etc_passwd_group();
151
152  if ((fp = fopen ("/etc/passwd", "r")) == NULL) {
153    errno = EINVAL;
154    return -1;
155  }
156
157  while (fgets (buffer, bufsize, fp)) {
158    sscanf (buffer, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%[^:]:%s\n",
159     logname, password, &pw_passwd.pw_uid,
160     &pw_passwd.pw_gid, comment, gecos,
161     dir, shell);
162    pwd->pw_name = logname;
163    pwd->pw_passwd = password;
164    pwd->pw_comment = comment;
165    pwd->pw_gecos = gecos;
166    pwd->pw_dir = dir;
167    pwd->pw_shell = shell;
168
169    if (uid == pwd->pw_uid) {
170      fclose (fp);
171      *result = pwd;
172      return 0;
173    }
174  }
175  fclose (fp);
176  errno = EINVAL;
177  return -1;
178}
179
180struct passwd *getpwuid(
181  uid_t uid
182)
183{
184  char   buf[1024];
185  struct passwd *p;
186
187  if ( getpwuid_r( uid, &pw_passwd, buf, 1024, &p ) )
188    return NULL;
189
190  return p;
191}
192
193struct passwd *getpwent()
194{
195  char buf[1024];
196
197  if (passwd_fp == NULL)
198    return NULL;
199
200  if (fgets (buf, sizeof (buf), passwd_fp) == NULL)
201    return NULL;
202
203  sscanf (buf, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%[^:]:%s\n",
204    logname, password, &pw_passwd.pw_uid,
205    &pw_passwd.pw_gid, comment, gecos,
206    dir, shell);
207  pw_passwd.pw_name = logname;
208  pw_passwd.pw_passwd = password;
209  pw_passwd.pw_comment = comment;
210  pw_passwd.pw_gecos = gecos;
211  pw_passwd.pw_dir = dir;
212  pw_passwd.pw_shell = shell;
213
214  return &pw_passwd;
215}
216
217void setpwent( void )
218{
219  init_etc_passwd_group();
220
221  if (passwd_fp != NULL)
222    fclose (passwd_fp);
223
224  passwd_fp = fopen ("/etc/passwd", "r");
225}
226
227void endpwent( void )
228{
229  if (passwd_fp != NULL)
230    fclose (passwd_fp);
231}
Note: See TracBrowser for help on using the repository browser.