]>
Commit | Line | Data |
---|---|---|
82962619 KS |
1 | /* |
2 | * klibc_fixups.c - very simple implementation of stuff missing in klibc | |
3 | * | |
4 | * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com> | |
5 | * Copyright (C) 2004 Kay Sievers <kay@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 | */ | |
1e959a4b GKH |
21 | |
22 | #ifdef __KLIBC__ | |
23 | ||
24 | #include <stdlib.h> | |
82962619 | 25 | #include <stdio.h> |
1e959a4b GKH |
26 | #include <string.h> |
27 | #include <ctype.h> | |
534c853d | 28 | #include <fcntl.h> |
2023350e | 29 | #include <sys/types.h> |
82962619 | 30 | |
c81b35c0 | 31 | #include "udev.h" |
2023350e | 32 | #include "klibc_fixups.h" |
82962619 KS |
33 | #include "logging.h" |
34 | ||
35 | #define PW_FILE "/etc/passwd" | |
36 | #define GR_FILE "/etc/group" | |
534c853d KS |
37 | #define UTMP_FILE "/var/run/utmp" |
38 | ||
82962619 KS |
39 | |
40 | /* return the id of a passwd style line, selected by the users name */ | |
41 | static unsigned long get_id_by_name(const char *uname, const char *dbfile) | |
42 | { | |
43 | unsigned long id = -1; | |
c81b35c0 KS |
44 | char line[255]; |
45 | char *buf; | |
46 | size_t bufsize; | |
47 | size_t cur; | |
48 | size_t count; | |
82962619 KS |
49 | char *pos; |
50 | char *name; | |
51 | char *idstr; | |
52 | char *tail; | |
53 | ||
c81b35c0 KS |
54 | if (file_map(dbfile, &buf, &bufsize) == 0) { |
55 | dbg("reading '%s' as db file", dbfile); | |
56 | } else { | |
57 | dbg("can't open '%s' as db file", dbfile); | |
82962619 KS |
58 | return -1; |
59 | } | |
60 | ||
c81b35c0 KS |
61 | /* loop through the whole file */ |
62 | ||
63 | cur = 0; | |
82962619 | 64 | while (1) { |
c81b35c0 KS |
65 | count = buf_get_line(buf, bufsize, cur); |
66 | ||
67 | strncpy(line, buf + cur, count); | |
68 | line[count] = '\0'; | |
69 | pos = line; | |
70 | ||
71 | cur += count+1; | |
72 | if (cur > bufsize) | |
82962619 KS |
73 | break; |
74 | ||
75 | /* get name */ | |
76 | name = strsep(&pos, ":"); | |
77 | if (name == NULL) | |
78 | continue; | |
79 | ||
80 | /* skip pass */ | |
81 | if (strsep(&pos, ":") == NULL) | |
82 | continue; | |
83 | ||
84 | /* get id */ | |
85 | idstr = strsep(&pos, ":"); | |
86 | if (idstr == NULL) | |
87 | continue; | |
88 | ||
89 | if (strcmp(uname, name) == 0) { | |
90 | id = strtoul(idstr, &tail, 10); | |
aebef544 | 91 | if (tail[0] != '\0') |
82962619 KS |
92 | id = -1; |
93 | else | |
94 | dbg("id for '%s' is '%li'", name, id); | |
95 | break; | |
96 | } | |
97 | } | |
98 | ||
c81b35c0 | 99 | file_unmap(buf, bufsize); |
82962619 KS |
100 | return id; |
101 | } | |
102 | ||
103 | struct passwd *getpwnam(const char *name) | |
104 | { | |
105 | static struct passwd pw; | |
106 | ||
107 | memset(&pw, 0x00, sizeof(struct passwd)); | |
108 | pw.pw_uid = (uid_t) get_id_by_name(name, PW_FILE); | |
109 | if (pw.pw_uid < 0) | |
110 | return NULL; | |
111 | else | |
112 | return &pw; | |
113 | } | |
114 | ||
115 | struct group *getgrnam(const char *name) | |
116 | { | |
117 | static struct group gr; | |
118 | ||
119 | memset(&gr, 0x00, sizeof(struct group)); | |
120 | gr.gr_gid = (gid_t) get_id_by_name(name, GR_FILE); | |
121 | if (gr.gr_gid < 0) | |
122 | return NULL; | |
123 | else | |
124 | return &gr; | |
125 | } | |
1e959a4b | 126 | |
534c853d KS |
127 | |
128 | int ufd = -1; | |
129 | ||
130 | void setutent() | |
131 | { | |
132 | if (ufd < 0) | |
133 | ufd = open(UTMP_FILE, O_RDONLY); | |
134 | fcntl(ufd, F_SETFD, FD_CLOEXEC); | |
135 | lseek(ufd, 0, SEEK_SET); | |
136 | } | |
137 | ||
138 | void endutent() { | |
139 | if (ufd < 0) | |
140 | return; | |
141 | close(ufd); | |
142 | ufd = -1; | |
143 | } | |
144 | ||
145 | struct utmp *getutent(void) | |
146 | { | |
147 | static struct utmp utmp; | |
148 | int retval; | |
149 | ||
150 | if (ufd < 0) { | |
151 | setutent(); | |
152 | if (ufd < 0) | |
153 | return NULL; | |
154 | } | |
155 | retval = read(ufd, &utmp, sizeof(struct utmp)); | |
156 | if (retval < 1) | |
157 | return NULL; | |
158 | return &utmp; | |
159 | } | |
160 | ||
1e959a4b | 161 | #endif |