2 * Copyright (C) 2013-2014 Intel Corporation.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * José Bollo <jose.bollo@open.eurogiciel.org>
20 * Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
21 * Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
32 #include <sys/types.h>
38 #ifndef NOT_PASSWD_ONLY
43 enum { iname, ipasswd, iuid, igid, icmt, idir, ishell };
45 static const char pwfile[] = "/etc/passwd";
46 static struct buffer buffer;
47 static size_t pos, lengths[7];
48 static const char *starts[7];
50 #define is(s,i) (!strncmp(s,starts[i],lengths[i]) && !s[lengths[i]])
52 /* open the passwd file */
56 return buffer_create( &buffer, pwfile);
59 /* close the passwd file */
62 buffer_destroy( &buffer);
65 /* read the passwd file */
69 const char *head = buffer.buffer + pos;
70 const char *end = buffer.buffer + buffer.length;
73 while(head!=end && *head!=':' && *head!='\n') head++;
74 lengths[col] = head - starts[col];
78 pos = head - buffer.buffer;
83 pos = head - buffer.buffer;
86 while (head!=end && *head!='\n') head++;
87 if (head!=end) head++;
97 pos = head - buffer.buffer;
101 int pw_get( struct heap *heap, struct pwget **items)
107 for( n = 0 ; items[n] != NULL ; n++ )
113 while (s && rdpw()) {
115 for( i = 0 ; i < n ; i++ ) {
116 if (!items[i]->set && is(items[i]->id,iuid)) {
118 user = heap_strndup( heap,
119 starts[iname], lengths[iname]);
120 home = heap_strndup( heap,
121 starts[idir], lengths[idir]);
124 items[i]->user = user;
125 items[i]->home = home;
135 int pw_has_uid( uid_t uid)
139 if (lengths[iuid] && (int)uid == atoi(starts[iuid])) {
149 int pw_get_uid( const char *name, uid_t *uid)
154 if (is(name,iname)) {
155 *uid = (uid_t)atoi(starts[iuid]);
167 int pw_get_gid( const char *name, gid_t *gid)
172 if (is(name,iname)) {
173 *gid = (gid_t)atoi(starts[igid]);
191 int pw_get( struct heap *heap, struct pwget **items)
193 char buffer[BUFSIZE];
194 struct passwd entry, *pe;
199 for( n = 0 ; items[n] != NULL ; n++ ) {
200 id = (uid_t)atoi(items[n]->id);
201 result = getpwuid_r( id, &entry, buffer, sizeof buffer, &pe);
203 while(items[n] != NULL) items[n++]->set = 0;
210 items[n]->user = heap_strdup( heap, pe->pw_name);
211 items[n]->home = heap_strdup( heap, pe->pw_dir);
217 int pw_has_uid( uid_t uid)
219 char buffer[BUFSIZE];
220 struct passwd entry, *pe;
223 result = getpwuid_r( uid, &entry, buffer, sizeof buffer, &pe);
224 return !result && pe;
227 int pw_get_uid( const char *name, uid_t *uid)
229 char buffer[BUFSIZE];
230 struct passwd entry, *pe;
231 int result = getpwnam_r( name, &entry, buffer, sizeof buffer, &pe);
244 int pw_get_gid( const char *name, gid_t *gid)
246 char buffer[BUFSIZE];
247 struct passwd entry, *pe;
248 int result = getpwnam_r( name, &entry, buffer, sizeof buffer, &pe);
265 int main(int argc,char**argv) {
267 enum { uid, gid, names } action = uid;
268 heap_create(&heap,1000);
270 if (!strcmp(*argv,"-u"))
272 else if (!strcmp(*argv,"-g"))
274 else if (!strcmp(*argv,"-n"))
276 else if (action == uid) {
278 int sts = pw_get_uid( *argv, &u);
279 printf("uid(%s)=%d [%d]\n",*argv,(int)u,sts);
281 else if (action == gid) {
283 int sts = pw_get_gid( *argv, &g);
284 printf("gid(%s)=%d [%d]\n",*argv,(int)g,sts);
286 else if (action == names) {
287 struct pwget pw = { .id=*argv }, *ppw[2] = { &pw, NULL };
288 int sts = pw_get( &heap, ppw);
289 printf("names(%s) set=%d",*argv,pw.set);
291 printf(" name=%s home=%s",
292 pw.user==HNULL ? "(null)"
293 : (char*)heap_address( &heap, pw.user),
294 pw.home==HNULL ? "(null)"
295 : (char*)heap_address( &heap, pw.home));
297 printf(" [%d]\n",sts);