chiark / gitweb /
selinux: init once in the daemon, not in every event process
[elogind.git] / udev_sysdeps.c
1 /*
2  * udev_sysdeps.c - wrapping of libc features and kernel defines
3  *
4  * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
5  * Copyright (C) 2005-2006 Kay Sievers <kay.sievers@vrfy.org>
6  *
7  *      This program is free software; you can redistribute it and/or modify it
8  *      under the terms of the GNU General Public License as published by the
9  *      Free Software Foundation version 2 of the License.
10  * 
11  *      This program is distributed in the hope that it will be useful, but
12  *      WITHOUT ANY WARRANTY; without even the implied warranty of
13  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *      General Public License for more details.
15  * 
16  *      You should have received a copy of the GNU General Public License along
17  *      with this program; if not, write to the Free Software Foundation, Inc.,
18  *      675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <sys/types.h>
29
30 #include "udev.h"
31
32 #ifndef __GLIBC__
33 #define __OWN_USERDB_PARSER__
34 #endif
35
36 #ifdef __GLIBC__
37 #define __OWN_STRLCPYCAT__
38 #endif
39
40 #ifdef USE_STATIC
41 #define __OWN_USERDB_PARSER__
42 #endif
43
44 #ifdef __OWN_STRLCPYCAT__
45 size_t strlcpy(char *dst, const char *src, size_t size)
46 {
47         size_t bytes = 0;
48         char *q = dst;
49         const char *p = src;
50         char ch;
51
52         while ((ch = *p++)) {
53                 if (bytes+1 < size)
54                         *q++ = ch;
55                 bytes++;
56         }
57
58         /* If size == 0 there is no space for a final null... */
59         if (size)
60                 *q = '\0';
61
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                 if (errno == 0)
103                         err("specified user unknown '%s'", user);
104                 else
105                         err("error resolving user '%s': %s", user, strerror(errno));
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                 if (errno == 0)
120                         err("specified group unknown '%s'", group);
121                 else
122                         err("error resolving group '%s': %s", group, strerror(errno));
123         } else
124                 gid = gr->gr_gid;
125
126         return gid;
127 }
128
129 #else /* __OWN_USERDB_PARSER__ */
130
131 #define PASSWD_FILE             "/etc/passwd"
132 #define GROUP_FILE              "/etc/group"
133
134 /* return the id of a passwd style line, selected by the users name */
135 static unsigned long get_id_by_name(const char *uname, const char *dbfile)
136 {
137         unsigned long id = 0;
138         char line[LINE_SIZE];
139         char *buf;
140         char *bufline;
141         size_t bufsize;
142         size_t cur;
143         size_t count;
144         char *pos;
145         char *name;
146         char *idstr;
147         char *tail;
148
149         if (file_map(dbfile, &buf, &bufsize) != 0) {
150                 err("can't open '%s' as db file: %s", dbfile, strerror(errno));
151                 return 0;
152         }
153         dbg("search '%s' in '%s'", uname, dbfile);
154
155         /* loop through the whole file */
156         cur = 0;
157         while (cur < bufsize) {
158                 count = buf_get_line(buf, bufsize, cur);
159                 bufline = &buf[cur];
160                 cur += count+1;
161
162                 if (count >= sizeof(line))
163                         continue;
164
165                 memcpy(line, bufline, count-1);
166                 line[count-1] = '\0';
167                 pos = line;
168
169                 /* get name */
170                 name = strsep(&pos, ":");
171                 if (name == NULL)
172                         continue;
173
174                 /* skip pass */
175                 if (strsep(&pos, ":") == NULL)
176                         continue;
177
178                 /* get id */
179                 idstr = strsep(&pos, ":");
180                 if (idstr == NULL)
181                         continue;
182
183                 if (strcmp(uname, name) == 0) {
184                         id = strtoul(idstr, &tail, 10);
185                         if (tail[0] != '\0') {
186                                 id = 0;
187                                 dbg("no id found for '%s'",  name);
188                         } else
189                                 dbg("id for '%s' is '%li'", name, id);
190                         break;
191                 }
192         }
193
194         file_unmap(buf, bufsize);
195         return id;
196 }
197
198 uid_t lookup_user(const char *user)
199 {
200         unsigned long id;
201
202         id = get_id_by_name(user, PASSWD_FILE);
203         return (uid_t) id;
204 }
205
206 gid_t lookup_group(const char *group)
207 {
208         unsigned long id;
209
210         id = get_id_by_name(group, GROUP_FILE);
211         return (gid_t) id;
212 }
213 #endif /* __OWN_USERDB_PARSER__ */