chiark / gitweb /
[PATCH] klibc: use klcc wrapper instead of our own Makefile
[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 #ifdef __KLIBC__
36 #define __OWN_USERDB_PARSER__
37 #endif
38
39 #ifdef USE_STATIC
40 #define __OWN_USERDB_PARSER__
41 #endif
42
43 #ifndef strlcpy
44 size_t strlcpy(char *dst, const char *src, size_t size)
45 {
46         size_t bytes = 0;
47         char *q = dst;
48         const char *p = src;
49         char ch;
50
51         while ((ch = *p++)) {
52                 if (bytes < size)
53                         *q++ = ch;
54                 bytes++;
55         }
56
57         *q = '\0';
58         return bytes;
59 }
60 #endif
61
62 #ifndef strlcat
63 size_t strlcat(char *dst, const char *src, size_t size)
64 {
65         size_t bytes = 0;
66         char *q = dst;
67         const char *p = src;
68         char ch;
69
70         while (bytes < size && *q) {
71                 q++;
72                 bytes++;
73         }
74
75         while ((ch = *p++)) {
76                 if (bytes < size)
77                 *q++ = ch;
78                 bytes++;
79         }
80
81         *q = '\0';
82         return bytes;
83 }
84 #endif
85
86 #ifndef __OWN_USERDB_PARSER__
87 #include <sys/types.h>
88 #include <pwd.h>
89 #include <grp.h>
90
91 uid_t lookup_user(const char *user)
92 {
93         struct passwd *pw;
94         uid_t uid = 0;
95
96         pw = getpwnam(user);
97         if (pw == NULL)
98                 dbg("specified user unknown '%s'", user);
99         else
100                 uid = pw->pw_uid;
101
102         return uid;
103 }
104
105 gid_t lookup_group(const char *group)
106 {
107         struct group *gr;
108         gid_t gid = 0;
109
110         gr = getgrnam(group);
111         if (gr == NULL)
112                 dbg("specified group unknown '%s'", group);
113         else
114                 gid = gr->gr_gid;
115
116         return gid;
117 }
118
119 #else /* __OWN_USERDB_PARSER__ */
120
121 #define PASSWD_FILE             "/etc/passwd"
122 #define GROUP_FILE              "/etc/group"
123
124 /* return the id of a passwd style line, selected by the users name */
125 static unsigned long get_id_by_name(const char *uname, const char *dbfile)
126 {
127         unsigned long id = 0;
128         char line[LINE_SIZE];
129         char *buf;
130         char *bufline;
131         size_t bufsize;
132         size_t cur;
133         size_t count;
134         char *pos;
135         char *name;
136         char *idstr;
137         char *tail;
138
139         if (file_map(dbfile, &buf, &bufsize) != 0) {
140                 dbg("can't open '%s' as db file", dbfile);
141                 return 0;
142         }
143         dbg("reading '%s' as db file", dbfile);
144
145         /* loop through the whole file */
146         cur = 0;
147         while (cur < bufsize) {
148                 count = buf_get_line(buf, bufsize, cur);
149                 bufline = &buf[cur];
150                 cur += count+1;
151
152                 if (count >= sizeof(line))
153                         continue;
154
155                 strlcpy(line, bufline, count);
156                 pos = line;
157
158                 /* get name */
159                 name = strsep(&pos, ":");
160                 if (name == NULL)
161                         continue;
162
163                 /* skip pass */
164                 if (strsep(&pos, ":") == NULL)
165                         continue;
166
167                 /* get id */
168                 idstr = strsep(&pos, ":");
169                 if (idstr == NULL)
170                         continue;
171
172                 if (strcmp(uname, name) == 0) {
173                         id = strtoul(idstr, &tail, 10);
174                         if (tail[0] != '\0') {
175                                 id = 0;
176                                 dbg("no id found for '%s'",  name);
177                         } else
178                                 dbg("id for '%s' is '%li'", name, id);
179                         break;
180                 }
181         }
182
183         file_unmap(buf, bufsize);
184         return id;
185 }
186
187 uid_t lookup_user(const char *user)
188 {
189         unsigned long id;
190
191         id = get_id_by_name(user, PASSWD_FILE);
192         return (uid_t) id;
193 }
194
195 gid_t lookup_group(const char *group)
196 {
197         unsigned long id;
198
199         id = get_id_by_name(group, GROUP_FILE);
200         return (gid_t) id;
201 }
202 #endif /* __OWN_USERDB_PARSER__ */