chiark / gitweb /
23cb7b43168bc9ff7c3619bcf1440627f67f8b42
[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         *q = '\0';
62         return bytes;
63 }
64
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         }
76         if (bytes == size)
77                 return (bytes + strlen(src));
78
79         while ((ch = *p++)) {
80                 if (bytes+1 < size)
81                 *q++ = ch;
82                 bytes++;
83         }
84
85         *q = '\0';
86         return bytes;
87 }
88 #endif /* __OWN_STRLCPYCAT__ */
89
90 #ifndef __OWN_USERDB_PARSER__
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"
127
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 {
131         unsigned long id = 0;
132         char line[LINE_SIZE];
133         char *buf;
134         char *bufline;
135         size_t bufsize;
136         size_t cur;
137         size_t count;
138         char *pos;
139         char *name;
140         char *idstr;
141         char *tail;
142
143         if (file_map(dbfile, &buf, &bufsize) != 0) {
144                 dbg("can't open '%s' as db file", dbfile);
145                 return 0;
146         }
147         dbg("search '%s' in '%s'", uname, dbfile);
148
149         /* loop through the whole file */
150         cur = 0;
151         while (cur < bufsize) {
152                 count = buf_get_line(buf, bufsize, cur);
153                 bufline = &buf[cur];
154                 cur += count+1;
155
156                 if (count >= sizeof(line))
157                         continue;
158
159                 strlcpy(line, bufline, count);
160                 pos = line;
161
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);
178                         if (tail[0] != '\0') {
179                                 id = 0;
180                                 dbg("no id found for '%s'",  name);
181                         } else
182                                 dbg("id for '%s' is '%li'", name, id);
183                         break;
184                 }
185         }
186
187         file_unmap(buf, bufsize);
188         return id;
189 }
190
191 uid_t lookup_user(const char *user)
192 {
193         unsigned long id;
194
195         id = get_id_by_name(user, PASSWD_FILE);
196         return (uid_t) id;
197 }
198
199 gid_t lookup_group(const char *group)
200 {
201         unsigned long id;
202
203         id = get_id_by_name(group, GROUP_FILE);
204         return (gid_t) id;
205 }
206 #endif /* __OWN_USERDB_PARSER__ */