chiark / gitweb /
update RELEASE-NOTES
[elogind.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 <sys/types.h>
29
30 #include "udev_libc_wrapper.h"
31 #include "udev.h"
32 #include "udev_utils.h"
33 #include "logging.h"
34
35 #ifdef __KLIBC__
36 #define __OWN_USERDB_PARSER__
37 #endif
38
39 #ifdef __GLIBC__
40 #define __OWN_STRLCPYCAT__
41 #endif
42
43 #ifdef USE_STATIC
44 #define __OWN_USERDB_PARSER__
45 #endif
46
47 #ifdef __OWN_STRLCPYCAT__
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++)) {
56                 if (bytes+1 < size)
57                         *q++ = ch;
58                 bytes++;
59         }
60
61         /* If size == 0 there is no space for a final null... */
62         if (size)
63                 *q = '\0';
64
65         return bytes;
66 }
67
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         }
79         if (bytes == size)
80                 return (bytes + strlen(src));
81
82         while ((ch = *p++)) {
83                 if (bytes+1 < size)
84                 *q++ = ch;
85                 bytes++;
86         }
87
88         *q = '\0';
89         return bytes;
90 }
91 #endif /* __OWN_STRLCPYCAT__ */
92
93 #ifndef __OWN_USERDB_PARSER__
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                 info("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                 info("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"
130
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 {
134         unsigned long id = 0;
135         char line[LINE_SIZE];
136         char *buf;
137         char *bufline;
138         size_t bufsize;
139         size_t cur;
140         size_t count;
141         char *pos;
142         char *name;
143         char *idstr;
144         char *tail;
145
146         if (file_map(dbfile, &buf, &bufsize) != 0) {
147                 err("can't open '%s' as db file: %s", dbfile, strerror(errno));
148                 return 0;
149         }
150         dbg("search '%s' in '%s'", uname, dbfile);
151
152         /* loop through the whole file */
153         cur = 0;
154         while (cur < bufsize) {
155                 count = buf_get_line(buf, bufsize, cur);
156                 bufline = &buf[cur];
157                 cur += count+1;
158
159                 if (count >= sizeof(line))
160                         continue;
161
162                 memcpy(line, bufline, count-1);
163                 line[count-1] = '\0';
164                 pos = line;
165
166                 /* get name */
167                 name = strsep(&pos, ":");
168                 if (name == NULL)
169                         continue;
170
171                 /* skip pass */
172                 if (strsep(&pos, ":") == NULL)
173                         continue;
174
175                 /* get id */
176                 idstr = strsep(&pos, ":");
177                 if (idstr == NULL)
178                         continue;
179
180                 if (strcmp(uname, name) == 0) {
181                         id = strtoul(idstr, &tail, 10);
182                         if (tail[0] != '\0') {
183                                 id = 0;
184                                 dbg("no id found for '%s'",  name);
185                         } else
186                                 dbg("id for '%s' is '%li'", name, id);
187                         break;
188                 }
189         }
190
191         file_unmap(buf, bufsize);
192         return id;
193 }
194
195 uid_t lookup_user(const char *user)
196 {
197         unsigned long id;
198
199         id = get_id_by_name(user, PASSWD_FILE);
200         return (uid_t) id;
201 }
202
203 gid_t lookup_group(const char *group)
204 {
205         unsigned long id;
206
207         id = get_id_by_name(group, GROUP_FILE);
208         return (gid_t) id;
209 }
210 #endif /* __OWN_USERDB_PARSER__ */