]>
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" |
e5a2989e | 33 | #include "udev_lib.h" |
82962619 KS |
34 | #include "logging.h" |
35 | ||
36 | #define PW_FILE "/etc/passwd" | |
37 | #define GR_FILE "/etc/group" | |
534c853d KS |
38 | #define UTMP_FILE "/var/run/utmp" |
39 | ||
82962619 KS |
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; | |
3e441450 | 44 | char line[LINE_SIZE]; |
c81b35c0 | 45 | char *buf; |
3e441450 | 46 | char *bufline; |
c81b35c0 KS |
47 | size_t bufsize; |
48 | size_t cur; | |
49 | size_t count; | |
82962619 KS |
50 | char *pos; |
51 | char *name; | |
52 | char *idstr; | |
53 | char *tail; | |
54 | ||
c81b35c0 KS |
55 | if (file_map(dbfile, &buf, &bufsize) == 0) { |
56 | dbg("reading '%s' as db file", dbfile); | |
57 | } else { | |
58 | dbg("can't open '%s' as db file", dbfile); | |
82962619 KS |
59 | return -1; |
60 | } | |
61 | ||
c81b35c0 | 62 | /* loop through the whole file */ |
c81b35c0 | 63 | cur = 0; |
3e441450 | 64 | while (cur < bufsize) { |
c81b35c0 | 65 | count = buf_get_line(buf, bufsize, cur); |
3e441450 | 66 | bufline = &buf[cur]; |
67 | cur += count+1; | |
68 | ||
69 | if (count >= LINE_SIZE) | |
70 | continue; | |
c81b35c0 | 71 | |
3e441450 | 72 | strncpy(line, bufline, count); |
c81b35c0 KS |
73 | line[count] = '\0'; |
74 | pos = line; | |
75 | ||
82962619 KS |
76 | /* get name */ |
77 | name = strsep(&pos, ":"); | |
78 | if (name == NULL) | |
79 | continue; | |
80 | ||
81 | /* skip pass */ | |
82 | if (strsep(&pos, ":") == NULL) | |
83 | continue; | |
84 | ||
85 | /* get id */ | |
86 | idstr = strsep(&pos, ":"); | |
87 | if (idstr == NULL) | |
88 | continue; | |
89 | ||
90 | if (strcmp(uname, name) == 0) { | |
91 | id = strtoul(idstr, &tail, 10); | |
aebef544 | 92 | if (tail[0] != '\0') |
82962619 KS |
93 | id = -1; |
94 | else | |
95 | dbg("id for '%s' is '%li'", name, id); | |
96 | break; | |
97 | } | |
98 | } | |
99 | ||
c81b35c0 | 100 | file_unmap(buf, bufsize); |
82962619 KS |
101 | return id; |
102 | } | |
103 | ||
104 | struct passwd *getpwnam(const char *name) | |
105 | { | |
106 | static struct passwd pw; | |
107 | ||
108 | memset(&pw, 0x00, sizeof(struct passwd)); | |
109 | pw.pw_uid = (uid_t) get_id_by_name(name, PW_FILE); | |
110 | if (pw.pw_uid < 0) | |
111 | return NULL; | |
112 | else | |
113 | return &pw; | |
114 | } | |
115 | ||
116 | struct group *getgrnam(const char *name) | |
117 | { | |
118 | static struct group gr; | |
119 | ||
120 | memset(&gr, 0x00, sizeof(struct group)); | |
121 | gr.gr_gid = (gid_t) get_id_by_name(name, GR_FILE); | |
122 | if (gr.gr_gid < 0) | |
123 | return NULL; | |
124 | else | |
125 | return &gr; | |
126 | } | |
1e959a4b | 127 | |
534c853d KS |
128 | |
129 | int ufd = -1; | |
130 | ||
131 | void setutent() | |
132 | { | |
133 | if (ufd < 0) | |
134 | ufd = open(UTMP_FILE, O_RDONLY); | |
135 | fcntl(ufd, F_SETFD, FD_CLOEXEC); | |
136 | lseek(ufd, 0, SEEK_SET); | |
137 | } | |
138 | ||
139 | void endutent() { | |
140 | if (ufd < 0) | |
141 | return; | |
142 | close(ufd); | |
143 | ufd = -1; | |
144 | } | |
145 | ||
146 | struct utmp *getutent(void) | |
147 | { | |
148 | static struct utmp utmp; | |
149 | int retval; | |
150 | ||
151 | if (ufd < 0) { | |
152 | setutent(); | |
153 | if (ufd < 0) | |
154 | return NULL; | |
155 | } | |
156 | retval = read(ufd, &utmp, sizeof(struct utmp)); | |
157 | if (retval < 1) | |
158 | return NULL; | |
159 | return &utmp; | |
160 | } | |
161 | ||
1e959a4b | 162 | #endif |