1 | #include <stdio.h> |
---|
2 | #include <sys/types.h> |
---|
3 | #include <pwd.h> |
---|
4 | #include <errno.h> |
---|
5 | #include <unistd.h> |
---|
6 | #include <stdlib.h> |
---|
7 | #include <string.h> |
---|
8 | #include <limits.h> |
---|
9 | |
---|
10 | static struct passwd pw_passwd; /* password structure */ |
---|
11 | static FILE *passwd_fp; |
---|
12 | |
---|
13 | static char logname[8]; |
---|
14 | static char password[1024]; |
---|
15 | static char comment[1024]; |
---|
16 | static char gecos[1024]; |
---|
17 | static char dir[1024]; |
---|
18 | static char shell[1024]; |
---|
19 | |
---|
20 | int getpwnam_r( |
---|
21 | const char *name, |
---|
22 | struct passwd *pwd, |
---|
23 | char *buffer, |
---|
24 | size_t bufsize, |
---|
25 | struct passwd **result |
---|
26 | ) |
---|
27 | { |
---|
28 | FILE *fp; |
---|
29 | |
---|
30 | if ((fp = fopen ("/etc/passwd", "r")) == NULL) { |
---|
31 | errno = EINVAL; |
---|
32 | return -1; |
---|
33 | } |
---|
34 | |
---|
35 | while (fgets (buffer, bufsize, fp)) { |
---|
36 | sscanf (buffer, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%[^:]:%s\n", |
---|
37 | logname, password, &pwd->pw_uid, |
---|
38 | &pwd->pw_gid, comment, gecos, |
---|
39 | dir, shell); |
---|
40 | pwd->pw_name = logname; |
---|
41 | pwd->pw_passwd = password; |
---|
42 | pwd->pw_comment = comment; |
---|
43 | pwd->pw_gecos = gecos; |
---|
44 | pwd->pw_dir = dir; |
---|
45 | pwd->pw_shell = shell; |
---|
46 | |
---|
47 | if (!strcmp (logname, name)) { |
---|
48 | fclose (fp); |
---|
49 | *result = pwd; |
---|
50 | return 0; |
---|
51 | } |
---|
52 | } |
---|
53 | fclose (fp); |
---|
54 | errno = EINVAL; |
---|
55 | return -1; |
---|
56 | } |
---|
57 | |
---|
58 | struct passwd *getpwnam( |
---|
59 | const char *name |
---|
60 | ) |
---|
61 | { |
---|
62 | char buf[1024]; |
---|
63 | struct passwd *p; |
---|
64 | |
---|
65 | if ( getpwnam_r( name, &pw_passwd, buf, 1024, &p ) ) |
---|
66 | return NULL; |
---|
67 | |
---|
68 | return p; |
---|
69 | } |
---|
70 | |
---|
71 | int getpwuid_r( |
---|
72 | uid_t uid, |
---|
73 | struct passwd *pwd, |
---|
74 | char *buffer, |
---|
75 | size_t bufsize, |
---|
76 | struct passwd **result |
---|
77 | ) |
---|
78 | { |
---|
79 | FILE *fp; |
---|
80 | |
---|
81 | if ((fp = fopen ("/etc/passwd", "r")) == NULL) { |
---|
82 | errno = EINVAL; |
---|
83 | return -1; |
---|
84 | } |
---|
85 | |
---|
86 | while (fgets (buffer, bufsize, fp)) { |
---|
87 | sscanf (buffer, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%[^:]:%s\n", |
---|
88 | logname, password, &pw_passwd.pw_uid, |
---|
89 | &pw_passwd.pw_gid, comment, gecos, |
---|
90 | dir, shell); |
---|
91 | pwd->pw_name = logname; |
---|
92 | pwd->pw_passwd = password; |
---|
93 | pwd->pw_comment = comment; |
---|
94 | pwd->pw_gecos = gecos; |
---|
95 | pwd->pw_dir = dir; |
---|
96 | pwd->pw_shell = shell; |
---|
97 | |
---|
98 | if (uid == pwd->pw_uid) { |
---|
99 | fclose (fp); |
---|
100 | *result = pwd; |
---|
101 | return 0; |
---|
102 | } |
---|
103 | } |
---|
104 | fclose (fp); |
---|
105 | errno = EINVAL; |
---|
106 | return -1; |
---|
107 | } |
---|
108 | |
---|
109 | struct passwd *getpwuid( |
---|
110 | uid_t uid |
---|
111 | ) |
---|
112 | { |
---|
113 | char buf[1024]; |
---|
114 | struct passwd *p; |
---|
115 | |
---|
116 | if ( getpwuid_r( uid, &pw_passwd, buf, 1024, &p ) ) |
---|
117 | return NULL; |
---|
118 | |
---|
119 | return p; |
---|
120 | } |
---|
121 | |
---|
122 | struct passwd *getpwent() |
---|
123 | { |
---|
124 | char buf[1024]; |
---|
125 | |
---|
126 | if (passwd_fp == NULL) |
---|
127 | return NULL; |
---|
128 | |
---|
129 | if (fgets (buf, sizeof (buf), passwd_fp) == NULL) |
---|
130 | return NULL; |
---|
131 | |
---|
132 | sscanf (buf, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%[^:]:%s\n", |
---|
133 | logname, password, &pw_passwd.pw_uid, |
---|
134 | &pw_passwd.pw_gid, comment, gecos, |
---|
135 | dir, shell); |
---|
136 | pw_passwd.pw_name = logname; |
---|
137 | pw_passwd.pw_passwd = password; |
---|
138 | pw_passwd.pw_comment = comment; |
---|
139 | pw_passwd.pw_gecos = gecos; |
---|
140 | pw_passwd.pw_dir = dir; |
---|
141 | pw_passwd.pw_shell = shell; |
---|
142 | |
---|
143 | return &pw_passwd; |
---|
144 | } |
---|
145 | |
---|
146 | void setpwent( void ) |
---|
147 | { |
---|
148 | if (passwd_fp != NULL) |
---|
149 | fclose (passwd_fp); |
---|
150 | |
---|
151 | passwd_fp = fopen ("/etc/passwd", "r"); |
---|
152 | } |
---|
153 | |
---|
154 | void endpwent( void ) |
---|
155 | { |
---|
156 | if (passwd_fp != NULL) |
---|
157 | fclose (passwd_fp); |
---|
158 | } |
---|