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