chiark / gitweb /
[PATCH] remove Makefile magic for klibc integration
[elogind.git] / udev_libc_wrapper.c
1 /*
2  * udev_libc_wrapper - wrapping of functions missing in a specific libc
3  *                     or not working in a statically compiled binary
4  *
5  * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
6  * Copyright (C) 2005 Kay Sievers <kay@vrfy.org>
7  *
8  *      This program is free software; you can redistribute it and/or modify it
9  *      under the terms of the GNU General Public License as published by the
10  *      Free Software Foundation version 2 of the License.
11  * 
12  *      This program is distributed in the hope that it will be useful, but
13  *      WITHOUT ANY WARRANTY; without even the implied warranty of
14  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *      General Public License for more details.
16  * 
17  *      You should have received a copy of the GNU General Public License along
18  *      with this program; if not, write to the Free Software Foundation, Inc.,
19  *      675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <fcntl.h>
28 #include <sys/types.h>
29
30 #include "../udev_libc_wrapper.h"
31 #include "../udev.h"
32 #include "../udev_utils.h"
33 #include "../logging.h"
34
35
36 #ifdef __KLIBC__
37 #define __OWN_USERDB_PARSER__
38 #endif
39 #ifdef USE_STATIC
40 #define __OWN_USERDB_PARSER__
41 #endif
42
43 #ifndef __OWN_USERDB_PARSER__
44
45 #include <sys/types.h>
46 #include <pwd.h>
47 #include <grp.h>
48
49 uid_t lookup_user(const char *user)
50 {
51         struct passwd *pw;
52         uid_t uid = 0;
53
54         pw = getpwnam(user);
55         if (pw == NULL)
56                 dbg("specified user unknown '%s'", user);
57         else
58                 uid = pw->pw_uid;
59
60         return uid;
61 }
62
63 gid_t lookup_group(const char *group)
64 {
65         struct group *gr;
66         gid_t gid = 0;
67
68         gr = getgrnam(group);
69         if (gr == NULL)
70                 dbg("specified group unknown '%s'", group);
71         else
72                 gid = gr->gr_gid;
73
74         return gid;
75 }
76
77 #else /* __OWN_USERDB_PARSER__ */
78
79 #define PASSWD_FILE             "/etc/passwd"
80 #define GROUP_FILE              "/etc/group"
81
82 /* return the id of a passwd style line, selected by the users name */
83 static unsigned long get_id_by_name(const char *uname, const char *dbfile)
84 {
85         unsigned long id = 0;
86         char line[LINE_SIZE];
87         char *buf;
88         char *bufline;
89         size_t bufsize;
90         size_t cur;
91         size_t count;
92         char *pos;
93         char *name;
94         char *idstr;
95         char *tail;
96
97         if (file_map(dbfile, &buf, &bufsize) != 0) {
98                 dbg("can't open '%s' as db file", dbfile);
99                 return 0;
100         }
101         dbg("reading '%s' as db file", dbfile);
102
103         /* loop through the whole file */
104         cur = 0;
105         while (cur < bufsize) {
106                 count = buf_get_line(buf, bufsize, cur);
107                 bufline = &buf[cur];
108                 cur += count+1;
109
110                 if (count >= LINE_SIZE)
111                         continue;
112
113                 strncpy(line, bufline, count);
114                 line[count] = '\0';
115                 pos = line;
116
117                 /* get name */
118                 name = strsep(&pos, ":");
119                 if (name == NULL)
120                         continue;
121
122                 /* skip pass */
123                 if (strsep(&pos, ":") == NULL)
124                         continue;
125
126                 /* get id */
127                 idstr = strsep(&pos, ":");
128                 if (idstr == NULL)
129                         continue;
130
131                 if (strcmp(uname, name) == 0) {
132                         id = strtoul(idstr, &tail, 10);
133                         if (tail[0] != '\0') {
134                                 id = 0;
135                                 dbg("no id found for '%s'",  name);
136                         } else
137                                 dbg("id for '%s' is '%li'", name, id);
138                         break;
139                 }
140         }
141
142         file_unmap(buf, bufsize);
143         return id;
144 }
145
146 uid_t lookup_user(const char *user)
147 {
148         unsigned long id;
149
150         id = get_id_by_name(user, PASSWD_FILE);
151         return (uid_t) id;
152 }
153
154 gid_t lookup_group(const char *group)
155 {
156         unsigned long id;
157
158         id = get_id_by_name(group, GROUP_FILE);
159         return (gid_t) id;
160 }
161
162 #endif /* __OWN_USERDB_PARSER__ */