summaryrefslogtreecommitdiff
path: root/udev_libc_wrapper.c
blob: 4b39b6f47627826fd09c836af3a7869b1830c58e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * udev_libc_wrapper - wrapping of functions missing in a specific libc
 *		       or not working in a statically compiled binary
 *
 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
 * Copyright (C) 2005 Kay Sievers <kay@vrfy.org>
 *
 *	This program is free software; you can redistribute it and/or modify it
 *	under the terms of the GNU General Public License as published by the
 *	Free Software Foundation version 2 of the License.
 * 
 *	This program is distributed in the hope that it will be useful, but
 *	WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *	General Public License for more details.
 * 
 *	You should have received a copy of the GNU General Public License along
 *	with this program; if not, write to the Free Software Foundation, Inc.,
 *	675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/types.h>

#include "udev_libc_wrapper.h"
#include "udev.h"
#include "udev_utils.h"
#include "logging.h"

#ifdef __KLIBC__
#define __OWN_USERDB_PARSER__
#endif

#ifdef USE_STATIC
#define __OWN_USERDB_PARSER__
#endif

#ifndef strlcpy
size_t strlcpy(char *dst, const char *src, size_t size)
{
	size_t bytes = 0;
	char *q = dst;
	const char *p = src;
	char ch;

	while ((ch = *p++)) {
		if (bytes < size)
			*q++ = ch;
		bytes++;
	}

	*q = '\0';
	return bytes;
}
#endif

#ifndef strlcat
size_t strlcat(char *dst, const char *src, size_t size)
{
	size_t bytes = 0;
	char *q = dst;
	const char *p = src;
	char ch;

	while (bytes < size && *q) {
		q++;
		bytes++;
	}

	while ((ch = *p++)) {
		if (bytes < size)
		*q++ = ch;
		bytes++;
	}

	*q = '\0';
	return bytes;
}
#endif

#ifndef __OWN_USERDB_PARSER__
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>

uid_t lookup_user(const char *user)
{
	struct passwd *pw;
	uid_t uid = 0;

	pw = getpwnam(user);
	if (pw == NULL)
		dbg("specified user unknown '%s'", user);
	else
		uid = pw->pw_uid;

	return uid;
}

gid_t lookup_group(const char *group)
{
	struct group *gr;
	gid_t gid = 0;

	gr = getgrnam(group);
	if (gr == NULL)
		dbg("specified group unknown '%s'", group);
	else
		gid = gr->gr_gid;

	return gid;
}

#else /* __OWN_USERDB_PARSER__ */

#define PASSWD_FILE		"/etc/passwd"
#define GROUP_FILE		"/etc/group"

/* return the id of a passwd style line, selected by the users name */
static unsigned long get_id_by_name(const char *uname, const char *dbfile)
{
	unsigned long id = 0;
	char line[LINE_SIZE];
	char *buf;
	char *bufline;
	size_t bufsize;
	size_t cur;
	size_t count;
	char *pos;
	char *name;
	char *idstr;
	char *tail;

	if (file_map(dbfile, &buf, &bufsize) != 0) {
		dbg("can't open '%s' as db file", dbfile);
		return 0;
	}
	dbg("reading '%s' as db file", dbfile);

	/* loop through the whole file */
	cur = 0;
	while (cur < bufsize) {
		count = buf_get_line(buf, bufsize, cur);
		bufline = &buf[cur];
		cur += count+1;

		if (count >= sizeof(line))
			continue;

		strlcpy(line, bufline, count);
		pos = line;

		/* get name */
		name = strsep(&pos, ":");
		if (name == NULL)
			continue;

		/* skip pass */
		if (strsep(&pos, ":") == NULL)
			continue;

		/* get id */
		idstr = strsep(&pos, ":");
		if (idstr == NULL)
			continue;

		if (strcmp(uname, name) == 0) {
			id = strtoul(idstr, &tail, 10);
			if (tail[0] != '\0') {
				id = 0;
				dbg("no id found for '%s'",  name);
			} else
				dbg("id for '%s' is '%li'", name, id);
			break;
		}
	}

	file_unmap(buf, bufsize);
	return id;
}

uid_t lookup_user(const char *user)
{
	unsigned long id;

	id = get_id_by_name(user, PASSWD_FILE);
	return (uid_t) id;
}

gid_t lookup_group(const char *group)
{
	unsigned long id;

	id = get_id_by_name(group, GROUP_FILE);
	return (gid_t) id;
}
#endif /* __OWN_USERDB_PARSER__ */