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