]>
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 | |
57e1a277 KS |
39 | #ifdef USE_STATIC |
40 | #define __OWN_USERDB_PARSER__ | |
41 | #endif | |
42 | ||
63f61c5c KS |
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 | } | |
57e1a277 | 74 | |
63f61c5c KS |
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__ | |
57e1a277 KS |
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" | |
534c853d | 123 | |
82962619 KS |
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 | { | |
57e1a277 | 127 | unsigned long id = 0; |
3e441450 | 128 | char line[LINE_SIZE]; |
c81b35c0 | 129 | char *buf; |
3e441450 | 130 | char *bufline; |
c81b35c0 KS |
131 | size_t bufsize; |
132 | size_t cur; | |
133 | size_t count; | |
82962619 KS |
134 | char *pos; |
135 | char *name; | |
136 | char *idstr; | |
137 | char *tail; | |
138 | ||
57e1a277 | 139 | if (file_map(dbfile, &buf, &bufsize) != 0) { |
c81b35c0 | 140 | dbg("can't open '%s' as db file", dbfile); |
57e1a277 | 141 | return 0; |
82962619 | 142 | } |
57e1a277 | 143 | dbg("reading '%s' as db file", dbfile); |
82962619 | 144 | |
c81b35c0 | 145 | /* loop through the whole file */ |
c81b35c0 | 146 | cur = 0; |
3e441450 | 147 | while (cur < bufsize) { |
c81b35c0 | 148 | count = buf_get_line(buf, bufsize, cur); |
3e441450 | 149 | bufline = &buf[cur]; |
150 | cur += count+1; | |
151 | ||
63f61c5c | 152 | if (count >= sizeof(line)) |
3e441450 | 153 | continue; |
c81b35c0 | 154 | |
63f61c5c | 155 | strlcpy(line, bufline, count); |
c81b35c0 KS |
156 | pos = line; |
157 | ||
82962619 KS |
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); | |
57e1a277 KS |
174 | if (tail[0] != '\0') { |
175 | id = 0; | |
176 | dbg("no id found for '%s'", name); | |
177 | } else | |
82962619 KS |
178 | dbg("id for '%s' is '%li'", name, id); |
179 | break; | |
180 | } | |
181 | } | |
182 | ||
c81b35c0 | 183 | file_unmap(buf, bufsize); |
82962619 KS |
184 | return id; |
185 | } | |
186 | ||
57e1a277 | 187 | uid_t lookup_user(const char *user) |
82962619 | 188 | { |
57e1a277 | 189 | unsigned long id; |
82962619 | 190 | |
57e1a277 KS |
191 | id = get_id_by_name(user, PASSWD_FILE); |
192 | return (uid_t) id; | |
82962619 KS |
193 | } |
194 | ||
57e1a277 | 195 | gid_t lookup_group(const char *group) |
82962619 | 196 | { |
57e1a277 | 197 | unsigned long id; |
82962619 | 198 | |
57e1a277 KS |
199 | id = get_id_by_name(group, GROUP_FILE); |
200 | return (gid_t) id; | |
82962619 | 201 | } |
57e1a277 | 202 | #endif /* __OWN_USERDB_PARSER__ */ |