2 * Copyright (C) 2013 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_get_uid( const char *name, uid_t *uid)
140 if (is(name,iname)) {
141 *uid = (uid_t)atoi(starts[iuid]);
153 int pw_get_gid( const char *name, gid_t *gid)
158 if (is(name,iname)) {
159 *gid = (gid_t)atoi(starts[igid]);
177 int pw_get( struct heap *heap, struct pwget **items)
179 char buffer[BUFSIZE];
180 struct passwd entry, *pe;
185 for( n = 0 ; items[n] != NULL ; n++ ) {
186 id = (uid_t)atoi(items[n]->id);
187 result = getpwuid_r( id, &entry, buffer, sizeof buffer, &pe);
189 while(items[n] != NULL) items[n++]->set = 0;
196 items[n]->user = heap_strdup( heap, pe->pw_name);
197 items[n]->home = heap_strdup( heap, pe->pw_dir);
203 int pw_get_uid( const char *name, uid_t *uid)
205 char buffer[BUFSIZE];
206 struct passwd entry, *pe;
207 int result = getpwnam_r( name, &entry, buffer, sizeof buffer, &pe);
220 int pw_get_gid( const char *name, gid_t *gid)
222 char buffer[BUFSIZE];
223 struct passwd entry, *pe;
224 int result = getpwnam_r( name, &entry, buffer, sizeof buffer, &pe);
241 int main(int argc,char**argv) {
243 enum { uid, gid, names } action = uid;
244 heap_create(&heap,1000);
246 if (!strcmp(*argv,"-u"))
248 else if (!strcmp(*argv,"-g"))
250 else if (!strcmp(*argv,"-n"))
252 else if (action == uid) {
254 int sts = pw_get_uid( *argv, &u);
255 printf("uid(%s)=%d [%d]\n",*argv,(int)u,sts);
257 else if (action == gid) {
259 int sts = pw_get_gid( *argv, &g);
260 printf("gid(%s)=%d [%d]\n",*argv,(int)g,sts);
262 else if (action == names) {
263 struct pwget pw = { .id=*argv }, *ppw[2] = { &pw, NULL };
264 int sts = pw_get( &heap, ppw);
265 printf("names(%s) set=%d",*argv,pw.set);
267 printf(" name=%s home=%s",
268 pw.user==HNULL ? "(null)"
269 : (char*)heap_address( &heap, pw.user),
270 pw.home==HNULL ? "(null)"
271 : (char*)heap_address( &heap, pw.home));
273 printf(" [%d]\n",sts);