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