]>
Commit | Line | Data |
---|---|---|
82962619 | 1 | /* |
57e1a277 KS |
2 | * udev_libc_wrapper - wrapping of functions missing in a specific libc |
3 | * or not working in a statically compiled binary | |
82962619 KS |
4 | * |
5 | * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com> | |
57e1a277 | 6 | * Copyright (C) 2005 Kay Sievers <kay@vrfy.org> |
82962619 KS |
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 | */ | |
1e959a4b | 22 | |
1e959a4b | 23 | #include <stdlib.h> |
82962619 | 24 | #include <stdio.h> |
1e959a4b GKH |
25 | #include <string.h> |
26 | #include <ctype.h> | |
534c853d | 27 | #include <fcntl.h> |
2023350e | 28 | #include <sys/types.h> |
82962619 | 29 | |
a5b9e299 TK |
30 | #include "udev_libc_wrapper.h" |
31 | #include "udev.h" | |
32 | #include "udev_utils.h" | |
33 | #include "logging.h" | |
82962619 | 34 | |
57e1a277 KS |
35 | #ifdef __KLIBC__ |
36 | #define __OWN_USERDB_PARSER__ | |
37 | #endif | |
63f61c5c | 38 | |
8a4c0c32 KS |
39 | #ifdef __GLIBC__ |
40 | #define __OWN_STRLCPYCAT__ | |
41 | #endif | |
42 | ||
57e1a277 KS |
43 | #ifdef USE_STATIC |
44 | #define __OWN_USERDB_PARSER__ | |
45 | #endif | |
46 | ||
8a4c0c32 | 47 | #ifdef __OWN_STRLCPYCAT__ |
63f61c5c KS |
48 | size_t strlcpy(char *dst, const char *src, size_t size) |
49 | { | |
50 | size_t bytes = 0; | |
51 | char *q = dst; | |
52 | const char *p = src; | |
53 | char ch; | |
54 | ||
55 | while ((ch = *p++)) { | |
8a4c0c32 | 56 | if (bytes+1 < size) |
63f61c5c KS |
57 | *q++ = ch; |
58 | bytes++; | |
59 | } | |
60 | ||
12340f41 | 61 | /* If size == 0 there is no space for a final null... */ |
4f8d44c2 KS |
62 | if (size) |
63 | *q = '\0'; | |
12340f41 | 64 | |
63f61c5c KS |
65 | return bytes; |
66 | } | |
63f61c5c | 67 | |
63f61c5c KS |
68 | size_t strlcat(char *dst, const char *src, size_t size) |
69 | { | |
70 | size_t bytes = 0; | |
71 | char *q = dst; | |
72 | const char *p = src; | |
73 | char ch; | |
74 | ||
75 | while (bytes < size && *q) { | |
76 | q++; | |
77 | bytes++; | |
78 | } | |
8a4c0c32 KS |
79 | if (bytes == size) |
80 | return (bytes + strlen(src)); | |
57e1a277 | 81 | |
63f61c5c | 82 | while ((ch = *p++)) { |
8a4c0c32 | 83 | if (bytes+1 < size) |
63f61c5c KS |
84 | *q++ = ch; |
85 | bytes++; | |
86 | } | |
87 | ||
12340f41 | 88 | *q = '\0'; |
63f61c5c KS |
89 | return bytes; |
90 | } | |
8a4c0c32 | 91 | #endif /* __OWN_STRLCPYCAT__ */ |
63f61c5c KS |
92 | |
93 | #ifndef __OWN_USERDB_PARSER__ | |
57e1a277 KS |
94 | #include <sys/types.h> |
95 | #include <pwd.h> | |
96 | #include <grp.h> | |
97 | ||
98 | uid_t lookup_user(const char *user) | |
99 | { | |
100 | struct passwd *pw; | |
101 | uid_t uid = 0; | |
102 | ||
103 | pw = getpwnam(user); | |
104 | if (pw == NULL) | |
105 | dbg("specified user unknown '%s'", user); | |
106 | else | |
107 | uid = pw->pw_uid; | |
108 | ||
109 | return uid; | |
110 | } | |
111 | ||
112 | gid_t lookup_group(const char *group) | |
113 | { | |
114 | struct group *gr; | |
115 | gid_t gid = 0; | |
116 | ||
117 | gr = getgrnam(group); | |
118 | if (gr == NULL) | |
119 | dbg("specified group unknown '%s'", group); | |
120 | else | |
121 | gid = gr->gr_gid; | |
122 | ||
123 | return gid; | |
124 | } | |
125 | ||
126 | #else /* __OWN_USERDB_PARSER__ */ | |
127 | ||
128 | #define PASSWD_FILE "/etc/passwd" | |
129 | #define GROUP_FILE "/etc/group" | |
534c853d | 130 | |
82962619 KS |
131 | /* return the id of a passwd style line, selected by the users name */ |
132 | static unsigned long get_id_by_name(const char *uname, const char *dbfile) | |
133 | { | |
57e1a277 | 134 | unsigned long id = 0; |
3e441450 | 135 | char line[LINE_SIZE]; |
c81b35c0 | 136 | char *buf; |
3e441450 | 137 | char *bufline; |
c81b35c0 KS |
138 | size_t bufsize; |
139 | size_t cur; | |
140 | size_t count; | |
82962619 KS |
141 | char *pos; |
142 | char *name; | |
143 | char *idstr; | |
144 | char *tail; | |
145 | ||
57e1a277 | 146 | if (file_map(dbfile, &buf, &bufsize) != 0) { |
c81b35c0 | 147 | dbg("can't open '%s' as db file", dbfile); |
57e1a277 | 148 | return 0; |
82962619 | 149 | } |
68c2c0b5 | 150 | dbg("search '%s' in '%s'", uname, dbfile); |
82962619 | 151 | |
c81b35c0 | 152 | /* loop through the whole file */ |
c81b35c0 | 153 | cur = 0; |
3e441450 | 154 | while (cur < bufsize) { |
c81b35c0 | 155 | count = buf_get_line(buf, bufsize, cur); |
3e441450 | 156 | bufline = &buf[cur]; |
157 | cur += count+1; | |
158 | ||
63f61c5c | 159 | if (count >= sizeof(line)) |
3e441450 | 160 | continue; |
c81b35c0 | 161 | |
63f61c5c | 162 | strlcpy(line, bufline, count); |
c81b35c0 KS |
163 | pos = line; |
164 | ||
82962619 KS |
165 | /* get name */ |
166 | name = strsep(&pos, ":"); | |
167 | if (name == NULL) | |
168 | continue; | |
169 | ||
170 | /* skip pass */ | |
171 | if (strsep(&pos, ":") == NULL) | |
172 | continue; | |
173 | ||
174 | /* get id */ | |
175 | idstr = strsep(&pos, ":"); | |
176 | if (idstr == NULL) | |
177 | continue; | |
178 | ||
179 | if (strcmp(uname, name) == 0) { | |
180 | id = strtoul(idstr, &tail, 10); | |
57e1a277 KS |
181 | if (tail[0] != '\0') { |
182 | id = 0; | |
183 | dbg("no id found for '%s'", name); | |
184 | } else | |
82962619 KS |
185 | dbg("id for '%s' is '%li'", name, id); |
186 | break; | |
187 | } | |
188 | } | |
189 | ||
c81b35c0 | 190 | file_unmap(buf, bufsize); |
82962619 KS |
191 | return id; |
192 | } | |
193 | ||
57e1a277 | 194 | uid_t lookup_user(const char *user) |
82962619 | 195 | { |
57e1a277 | 196 | unsigned long id; |
82962619 | 197 | |
57e1a277 KS |
198 | id = get_id_by_name(user, PASSWD_FILE); |
199 | return (uid_t) id; | |
82962619 KS |
200 | } |
201 | ||
57e1a277 | 202 | gid_t lookup_group(const char *group) |
82962619 | 203 | { |
57e1a277 | 204 | unsigned long id; |
82962619 | 205 | |
57e1a277 KS |
206 | id = get_id_by_name(group, GROUP_FILE); |
207 | return (gid_t) id; | |
82962619 | 208 | } |
57e1a277 | 209 | #endif /* __OWN_USERDB_PARSER__ */ |