[PATCH] 055 release
[systemd_ALT/systemd_imz.git] / udev_libc_wrapper.c
blob0dafe4d077bb344bdd7dc95adf260f6d822742ca
1 /*
2 * udev_libc_wrapper - wrapping of functions missing in a specific libc
3 * or not working in a statically compiled binary
5 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
6 * Copyright (C) 2005 Kay Sievers <kay@vrfy.org>
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.
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.
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.
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>
30 #include "../udev_libc_wrapper.h"
31 #include "../udev.h"
32 #include "../udev_utils.h"
33 #include "../logging.h"
35 #ifdef __KLIBC__
36 #define __OWN_USERDB_PARSER__
37 #endif
39 #ifdef USE_STATIC
40 #define __OWN_USERDB_PARSER__
41 #endif
43 #ifndef strlcpy
44 size_t strlcpy(char *dst, const char *src, size_t size)
46 size_t bytes = 0;
47 char *q = dst;
48 const char *p = src;
49 char ch;
51 while ((ch = *p++)) {
52 if (bytes < size)
53 *q++ = ch;
54 bytes++;
57 *q = '\0';
58 return bytes;
60 #endif
62 #ifndef strlcat
63 size_t strlcat(char *dst, const char *src, size_t size)
65 size_t bytes = 0;
66 char *q = dst;
67 const char *p = src;
68 char ch;
70 while (bytes < size && *q) {
71 q++;
72 bytes++;
75 while ((ch = *p++)) {
76 if (bytes < size)
77 *q++ = ch;
78 bytes++;
81 *q = '\0';
82 return bytes;
84 #endif
86 #ifndef __OWN_USERDB_PARSER__
87 #include <sys/types.h>
88 #include <pwd.h>
89 #include <grp.h>
91 uid_t lookup_user(const char *user)
93 struct passwd *pw;
94 uid_t uid = 0;
96 pw = getpwnam(user);
97 if (pw == NULL)
98 dbg("specified user unknown '%s'", user);
99 else
100 uid = pw->pw_uid;
102 return uid;
105 gid_t lookup_group(const char *group)
107 struct group *gr;
108 gid_t gid = 0;
110 gr = getgrnam(group);
111 if (gr == NULL)
112 dbg("specified group unknown '%s'", group);
113 else
114 gid = gr->gr_gid;
116 return gid;
119 #else /* __OWN_USERDB_PARSER__ */
121 #define PASSWD_FILE "/etc/passwd"
122 #define GROUP_FILE "/etc/group"
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)
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;
139 if (file_map(dbfile, &buf, &bufsize) != 0) {
140 dbg("can't open '%s' as db file", dbfile);
141 return 0;
143 dbg("reading '%s' as db file", dbfile);
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;
152 if (count >= sizeof(line))
153 continue;
155 strlcpy(line, bufline, count);
156 pos = line;
158 /* get name */
159 name = strsep(&pos, ":");
160 if (name == NULL)
161 continue;
163 /* skip pass */
164 if (strsep(&pos, ":") == NULL)
165 continue;
167 /* get id */
168 idstr = strsep(&pos, ":");
169 if (idstr == NULL)
170 continue;
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;
183 file_unmap(buf, bufsize);
184 return id;
187 uid_t lookup_user(const char *user)
189 unsigned long id;
191 id = get_id_by_name(user, PASSWD_FILE);
192 return (uid_t) id;
195 gid_t lookup_group(const char *group)
197 unsigned long id;
199 id = get_id_by_name(group, GROUP_FILE);
200 return (gid_t) id;
202 #endif /* __OWN_USERDB_PARSER__ */