Debian Codegraph @

src/bsd/detect.cc

raw
1 /* Hey EMACS -*- linux-c -*- */
2 /* $Id$ */
3
4 /* libticables2 - link cable library, a part of the TiLP project
5 * Copyright (C) 1999-2005 Romain Lievin
6 * Copyright (C) 2007 Kevin Kofler
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /* *BSD probing module */
24
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <stdint.h>
35 #include <unistd.h>
36 #include <pwd.h>
37 #include <grp.h>
38 #include <fcntl.h>
39
40 #include "../gettext.h"
41 #include "../error.h"
42 #include "../logging.h"
43 #include "detect.h"
44
45 /*
46 Returns mode string from mode value.
47 */
48 static const char *get_attributes(char s[13], mode_t attrib)
49 {
50 strcpy(s, " ---------- ");
51
52 if (attrib & S_IRUSR)
53 s[2] = 'r';
54 if (attrib & S_IWUSR)
55 s[3] = 'w';
56 if (attrib & S_ISUID)
57 {
58 if (attrib & S_IXUSR)
59 s[4] = 's';
60 else
61 s[4] = 'S';
62 }
63 else if (attrib & S_IXUSR)
64 s[4] = 'x';
65
66 if (attrib & S_IRGRP)
67 s[5] = 'r';
68 if (attrib & S_IWGRP)
69 s[6] = 'w';
70 if (attrib & S_ISGID)
71 {
72 if (attrib & S_IXGRP)
73 s[7] = 's';
74 else
75 s[7] = 'S';
76 }
77 else if (attrib & S_IXGRP)
78 s[7] = 'x';
79
80 if (attrib & S_IROTH)
81 s[8] = 'r';
82 if (attrib & S_IWOTH)
83 s[9] = 'w';
84 if (attrib & S_ISVTX)
85 {
86 if (attrib & S_IXOTH)
87 s[10] = 't';
88 else
89 s[10] = 'T';
90 }
91
92 return s;
93 }
94
95 /*
96 Returns user name from id.
97 */
98 static const char *get_user_name(uid_t uid)
99 {
100 struct passwd *pwuid;
101
102 if((pwuid = getpwuid(uid)) != NULL)
103 return pwuid->pw_name;
104
105 return "not found";
106 }
107
108 /*
109 Returns group name from id.
110 */
111 static const char *get_group_name(uid_t uid)
112 {
113 struct group *grpid;
114
115 if ((grpid = getgrgid(uid)) != NULL)
116 return grpid->gr_name;
117
118 return "not found";
119 }
120
121 /*
122 Attempt to find if an user is attached to a group.
123 - user [in] : a user name
124 - group [in] : a group name
125 */
126 static int search_for_user_in_group(const char *user, const char *group)
127 {
128 FILE *f;
129 char buffer[256];
130 const char *entry = "/etc/group";
131
132 f = fopen(entry, "rt");
133 if (f == NULL)
134 {
135 ticables_warning(_("can't open '%s'."), entry);
136 return -1;
137 }
138
139 while (!feof(f))
140 {
141 if (!fgets(buffer, 256, f))
142 break;
143
144 if (strstr(buffer, group))
145 {
146 if(strstr(buffer, user))
147 {
148 fclose(f);
149 return 0;
150 }
151 else
152 {
153 fclose(f);
154 return -1;
155 }
156 }
157 }
158
159 fclose(f);
160 return -1;
161 }
162
163 static int check_for_node_usability(const char *pathname)
164 {
165 struct stat st;
166
167 if(!access(pathname, F_OK))
168 {
169 ticables_info(_(" node %s: exists"), pathname);
170 }
171 else
172 {
173 ticables_info(_(" node %s: does not exist"), pathname);
174 ticables_info("%s", _(" => you will have to create the node."));
175
176 return -1;
177 }
178
179 if(!access(pathname, R_OK | W_OK))
180 {
181 ticables_info(_(" node %s: accessible"), pathname);
182 return 0;
183 }
184
185 if(!stat(pathname, &st))
186 {
187 char s[13];
188 ticables_info(_(" permissions/user/group:%s%s %s"),
189 get_attributes(s, st.st_mode),
190 get_user_name(st.st_uid),
191 get_group_name(st.st_gid));
192 }
193 else
194 {
195 ticables_warning("can't stat '%s'.", pathname);
196 return -1;
197 }
198
199 if(getuid() == st.st_uid)
200 {
201 ticables_info("%s", _(" user can r/w on device: yes"));
202 ticables_info("%s", _(" => device is inaccessible for unknown reasons"));
203 return -1;
204 }
205 else
206 {
207 ticables_info("%s", _(" user can r/w on device: no"));
208 }
209
210 if((st.st_mode & S_IROTH) && (st.st_mode & S_IWOTH))
211 {
212 ticables_info("%s", _(" others can r/w on device: yes"));
213 }
214 else
215 {
216 char *user, *group;
217
218 ticables_info("%s", _(" others can r/w on device: no"));
219
220 user = strdup(get_user_name(getuid()));
221 group = strdup(get_group_name(st.st_gid));
222
223 if(!search_for_user_in_group(user, group))
224 {
225 ticables_info(_(" is the user '%s' in the group '%s': yes"),
226 user, group);
227 }
228 else
229 {
230 ticables_info(_(" is the user '%s' in the group '%s': no"), user, group);
231 ticables_info(_(" => you should add your username at the group '%s' in '/etc/group'"), group);
232 ticables_info("%s", _(" => you will have to restart your session, too"));
233 free(user);
234 free(group);
235
236 return -1;
237 }
238
239 free(user);
240 free(group);
241 }
242
243 ticables_info("%s", _(" => device is inaccessible for unknown reasons"));
244 return -1;
245 }
246
247 int bsd_check_root(void)
248 {
249 uid_t uid = getuid();
250
251 ticables_info(_("Check for super-user access: %s"),
252 uid ? "no" : "yes");
253
254 return (uid ? ERR_ROOT : 0);
255 }
256
257 int bsd_check_tty(const char *devname)
258 {
259 int fd;
260
261 ticables_info("%s", _("Check for tty support:"));
262 ticables_info("%s", _(" tty support: available."));
263
264 // check for node usability
265 ticables_info("%s", _("Check for tty usability:"));
266 if(check_for_node_usability(devname) == -1)
267 return ERR_TTDEV;
268
269 // check for device availability
270 fd = open(devname, 0);
271 if (fd == -1)
272 {
273 ticables_warning("unable to open serial device '%s'", devname);
274 return ERR_TTDEV;
275 }
276
277 ticables_info("%s", _(" is usable: yes"));
278 close(fd);
279
280 return 0;
281 }
282
283 int bsd_check_parport(const char *devname)
284 {
285 return ERR_PPDEV;
286 }
287
288 int bsd_check_libusb(void)
289 {
290 ticables_info("%s", _("Check for libusb support:"));
291 #if defined(HAVE_LIBUSB) || defined(HAVE_LIBUSB_1_0)
292 ticables_info("%s", _(" usb support: available."));
293 return 0;
294 #else
295 ticables_info("%s", _(" usb support: not compiled."));
296 return ERR_USBFS;
297 #endif
298 }