1#[repr(C)]
9#[derive(Copy, Clone)]
10pub struct Spwd {
11 pub sp_namp: *mut c_char,
12 pub sp_pwdp: *mut c_char,
13 pub sp_lstchg: c_long,
14 pub sp_min: c_long,
15 pub sp_max: c_long,
16 pub sp_warn: c_long,
17 pub sp_inact: c_long,
18 pub sp_expire: c_long,
19 pub sp_flag: c_ulong,
20}
21
22static mut SPWD_ENT: Spwd = Spwd {
23 sp_namp: core::ptr::null_mut(), sp_pwdp: core::ptr::null_mut(),
24 sp_lstchg: 0, sp_min: 0, sp_max: 0, sp_warn: 0, sp_inact: 0,
25 sp_expire: 0, sp_flag: 0,
26};
27static mut SPWD_BUF: *mut c_char = core::ptr::null_mut();
28static mut SPWD_BUF_CAP: usize = 0;
29static mut SPWD_LINE: *mut c_char = core::ptr::null_mut();
30static mut SPWD_LINE_CAP: usize = 0;
31static mut SPWD_FILE: *mut FILE = core::ptr::null_mut();
32
33unsafe fn ensure_spwd_buf(need: usize) -> bool {
34 if SPWD_BUF_CAP >= need {
35 return true;
36 }
37 let mut cap = if SPWD_BUF_CAP == 0 { 1024 } else { SPWD_BUF_CAP };
38 while cap < need {
39 cap = match cap.checked_mul(2) {
40 Some(v) => v,
41 None => return false,
42 };
43 }
44 let grown = crate::realloc(SPWD_BUF as *mut c_void, cap) as *mut c_char;
45 if grown.is_null() {
46 ERRNO!() = ENOMEM;
47 return false;
48 }
49 SPWD_BUF = grown;
50 SPWD_BUF_CAP = cap;
51 true
52}
53
54unsafe fn spwd_next(f: *mut FILE) -> *mut Spwd {
57 loop {
58 let n = crate::getline(&raw mut SPWD_LINE, &raw mut SPWD_LINE_CAP, f);
59 if n < 0 {
60 return core::ptr::null_mut();
61 }
62 let len = n as usize;
63 if len > 0 && *SPWD_LINE.add(len - 1) == b'\n' as c_char {
64 *SPWD_LINE.add(len - 1) = 0;
65 }
66 if !ensure_spwd_buf(len + 1) {
67 return core::ptr::null_mut();
68 }
69 if parse_spwd(SPWD_LINE, &raw mut SPWD_ENT, SPWD_BUF, SPWD_BUF_CAP) {
70 return &raw mut SPWD_ENT;
71 }
72 }
73}
74
75unsafe fn parse_spwd(line: *const c_char, ent: *mut Spwd, buf: *mut c_char, buflen: usize) -> bool {
76 let mut fields: [*mut c_char; 9] = [core::ptr::null_mut(); 9];
77 let n = match split_colon_fields(line, buf, buflen, &mut fields) {
78 Some(n) => n,
79 None => return false,
80 };
81 if n < 9 { return false; }
82 (*ent).sp_namp = fields[0];
83 (*ent).sp_pwdp = fields[1];
84 (*ent).sp_lstchg = crate::strtol(fields[2], core::ptr::null_mut(), 10) as c_long;
85 (*ent).sp_min = crate::strtol(fields[3], core::ptr::null_mut(), 10) as c_long;
86 (*ent).sp_max = crate::strtol(fields[4], core::ptr::null_mut(), 10) as c_long;
87 (*ent).sp_warn = crate::strtol(fields[5], core::ptr::null_mut(), 10) as c_long;
88 (*ent).sp_inact = crate::strtol(fields[6], core::ptr::null_mut(), 10) as c_long;
89 (*ent).sp_expire = crate::strtol(fields[7], core::ptr::null_mut(), 10) as c_long;
90 (*ent).sp_flag = crate::strtoul(fields[8], core::ptr::null_mut(), 10) as c_ulong;
91 true
92}
93
94#[no_mangle]
95pub unsafe extern "C" fn setspent() {
96 endspent();
97 SPWD_FILE = crate::fopen(b"/etc/shadow\0".as_ptr() as *const c_char, b"r\0".as_ptr() as *const c_char);
98}
99
100#[no_mangle]
101pub unsafe extern "C" fn endspent() {
102 if !SPWD_FILE.is_null() { crate::fclose(SPWD_FILE); SPWD_FILE = core::ptr::null_mut(); }
103}
104
105#[no_mangle]
106pub unsafe extern "C" fn getspent() -> *mut Spwd {
107 if SPWD_FILE.is_null() { setspent(); if SPWD_FILE.is_null() { return core::ptr::null_mut(); } }
108 spwd_next(SPWD_FILE)
109}
110
111#[no_mangle]
112pub unsafe extern "C" fn fgetspent(f: *mut FILE) -> *mut Spwd {
113 if f.is_null() { return core::ptr::null_mut(); }
114 spwd_next(f)
115}
116
117#[no_mangle]
118pub unsafe extern "C" fn getspnam(name: *const c_char) -> *mut Spwd {
119 if name.is_null() { return core::ptr::null_mut(); }
120 setspent();
121 loop {
122 let sp = getspent();
123 if sp.is_null() { break; }
124 if crate::strcmp((*sp).sp_namp as *const u8, name as *const u8) == 0 { endspent(); return sp; }
125 }
126 endspent();
127 core::ptr::null_mut()
128}
129
130unsafe fn getsp_r_inner(
131 name: *const c_char,
132 sp: *mut Spwd,
133 buf: *mut c_char,
134 buflen: usize,
135 result: *mut *mut Spwd,
136) -> c_int {
137 if name.is_null() || sp.is_null() || buf.is_null() || result.is_null() {
138 return EINVAL;
139 }
140 *result = core::ptr::null_mut();
141 let f = crate::fopen(b"/etc/shadow\0".as_ptr() as *const c_char, b"r\0".as_ptr() as *const c_char);
142 if f.is_null() { return ERRNO!(); }
143 let mut line: *mut c_char = core::ptr::null_mut();
144 let mut cap: usize = 0;
145 let mut err = 0;
146 loop {
147 let n = crate::getline(&raw mut line, &raw mut cap, f);
148 if n < 0 { break; }
149 let mut len = n as usize;
150 if len > 0 && *line.add(len - 1) == b'\n' as c_char {
151 *line.add(len - 1) = 0;
152 len -= 1;
153 }
154 if len + 1 > buflen {
155 err = ERANGE;
156 break;
157 }
158 if parse_spwd(line, sp, buf, buflen) {
159 if crate::strcmp((*sp).sp_namp as *const u8, name as *const u8) == 0 {
160 *result = sp;
161 break;
162 }
163 }
164 }
165 crate::free(line as *mut c_void);
166 crate::fclose(f);
167 err
168}
169
170#[no_mangle]
171pub unsafe extern "C" fn getspnam_r(name: *const c_char, sp: *mut Spwd, buf: *mut c_char, buflen: usize, result: *mut *mut Spwd) -> c_int {
172 getsp_r_inner(name, sp, buf, buflen, result)
173}
174
175#[no_mangle]
176pub unsafe extern "C" fn putspent(sp: *const Spwd, f: *mut FILE) -> c_int {
177 if sp.is_null() || f.is_null() { return -1; }
178 crate::fprintf(f, b"%s:%s:%ld:%ld:%ld:%ld:%ld:%ld:%lu\\n\0".as_ptr() as *const c_char,
179 (*sp).sp_namp, (*sp).sp_pwdp, (*sp).sp_lstchg, (*sp).sp_min, (*sp).sp_max,
180 (*sp).sp_warn, (*sp).sp_inact, (*sp).sp_expire, (*sp).sp_flag)
181}
182
183#[no_mangle]
184pub unsafe extern "C" fn getgrouplist(user: *const c_char, group: u32, groups: *mut u32, ngroups: *mut i32) -> c_int {
185 if user.is_null() || groups.is_null() || ngroups.is_null() { return -1; }
186 if *ngroups < 0 { return -1; }
187 let max = *ngroups as usize;
188 let mut n = 0usize;
189 if n < max { *groups.add(n) = group; }
190 n += 1;
191 setgrent();
192 loop {
193 let g = getgrent();
194 if g.is_null() { break; }
195 if (*g).gr_gid == group { continue; }
196 let mut found = false;
197 let mut i = 0;
198 while !(*(*g).gr_mem.add(i)).is_null() {
199 if crate::strcmp(*(*g).gr_mem.add(i) as *const u8, user as *const u8) == 0 {
200 found = true;
201 break;
202 }
203 i += 1;
204 }
205 if found {
206 if n < max { *groups.add(n) = (*g).gr_gid; }
207 n += 1;
208 }
209 }
210 endgrent();
211 if n > max { *ngroups = n as i32; return -1; }
212 *ngroups = n as i32;
213 n as c_int
214}