OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rtems/] [c/] [src/] [lib/] [libc/] [getpwent.c] - Blame information for rev 1767

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.