| 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 |
/* Linux 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 |
#include <sys/ioctl.h> |
| 40 |
#include <errno.h> |
| 41 |
#ifdef HAVE_LINUX_PARPORT_H |
| 42 |
#include <linux/parport.h> |
| 43 |
#include <linux/ppdev.h> |
| 44 |
#endif |
| 45 |
#ifdef HAVE_LINUX_SERIAL_H |
| 46 |
#include <linux/serial.h> |
| 47 |
#endif |
| 48 |
#include <dirent.h> |
| 49 |
|
| 50 |
#include "../gettext.h" |
| 51 |
#include "../error.h" |
| 52 |
#include "../logging.h" |
| 53 |
#include "detect.h" |
| 54 |
|
| 55 |
/* |
| 56 |
Returns mode string from mode value. |
| 57 |
*/ |
| 58 |
static const char *get_attributes(char s[13], mode_t attrib) |
| 59 |
{ |
| 60 |
strcpy(s, " ---------- "); |
| 61 |
|
| 62 |
if (attrib & S_IRUSR) |
| 63 |
s[2] = 'r'; |
| 64 |
if (attrib & S_IWUSR) |
| 65 |
s[3] = 'w'; |
| 66 |
if (attrib & S_ISUID) |
| 67 |
{ |
| 68 |
if (attrib & S_IXUSR) |
| 69 |
s[4] = 's'; |
| 70 |
else |
| 71 |
s[4] = 'S'; |
| 72 |
} |
| 73 |
else if (attrib & S_IXUSR) |
| 74 |
s[4] = 'x'; |
| 75 |
|
| 76 |
if (attrib & S_IRGRP) |
| 77 |
s[5] = 'r'; |
| 78 |
if (attrib & S_IWGRP) |
| 79 |
s[6] = 'w'; |
| 80 |
if (attrib & S_ISGID) |
| 81 |
{ |
| 82 |
if (attrib & S_IXGRP) |
| 83 |
s[7] = 's'; |
| 84 |
else |
| 85 |
s[7] = 'S'; |
| 86 |
} |
| 87 |
else if (attrib & S_IXGRP) |
| 88 |
s[7] = 'x'; |
| 89 |
|
| 90 |
if (attrib & S_IROTH) |
| 91 |
s[8] = 'r'; |
| 92 |
if (attrib & S_IWOTH) |
| 93 |
s[9] = 'w'; |
| 94 |
if (attrib & S_ISVTX) |
| 95 |
{ |
| 96 |
if (attrib & S_IXOTH) |
| 97 |
s[10] = 't'; |
| 98 |
else |
| 99 |
s[10] = 'T'; |
| 100 |
} |
| 101 |
|
| 102 |
return s; |
| 103 |
} |
| 104 |
|
| 105 |
/* |
| 106 |
Returns user name from id. |
| 107 |
*/ |
| 108 |
static const char *get_user_name(uid_t uid) |
| 109 |
{ |
| 110 |
struct passwd *pwuid; |
| 111 |
|
| 112 |
if((pwuid = getpwuid(uid)) != NULL) |
| 113 |
return pwuid->pw_name; |
| 114 |
|
| 115 |
return "not found"; |
| 116 |
} |
| 117 |
|
| 118 |
/* |
| 119 |
Returns group name from id. |
| 120 |
*/ |
| 121 |
static const char *get_group_name(uid_t uid) |
| 122 |
{ |
| 123 |
struct group *grpid; |
| 124 |
|
| 125 |
if ((grpid = getgrgid(uid)) != NULL) |
| 126 |
return grpid->gr_name; |
| 127 |
|
| 128 |
return "not found"; |
| 129 |
} |
| 130 |
|
| 131 |
/* |
| 132 |
Attempt to find a specific string in /proc (vfs) |
| 133 |
- entry [in] : an entry such as '/proc/devices' |
| 134 |
- str [in) : an occurence to find (such as 'tipar') |
| 135 |
*/ |
| 136 |
#if 0 |
| 137 |
static int find_string_in_proc(char *entry, char *str) |
| 138 |
{ |
| 139 |
FILE *f; |
| 140 |
char buffer[80]; |
| 141 |
int found = 0; |
| 142 |
|
| 143 |
f = fopen(entry, "rt"); |
| 144 |
if (f == NULL) |
| 145 |
{ |
| 146 |
ticables_warning("can't open '%s'", entry); |
| 147 |
return -1; |
| 148 |
} |
| 149 |
|
| 150 |
while (!feof(f)) |
| 151 |
{ |
| 152 |
fscanf(f, "%s", buffer); |
| 153 |
if (strstr(buffer, str)) |
| 154 |
found = 1; |
| 155 |
} |
| 156 |
fclose(f); |
| 157 |
|
| 158 |
return found; |
| 159 |
} |
| 160 |
#endif |
| 161 |
|
| 162 |
/* |
| 163 |
Attempt to find if an user is attached to a group. |
| 164 |
- user [in] : a user name |
| 165 |
- group [in] : a group name |
| 166 |
*/ |
| 167 |
static int search_for_user_in_group(const char *user, const char *group) |
| 168 |
{ |
| 169 |
FILE *f; |
| 170 |
char buffer[256]; |
| 171 |
const char *entry = "/etc/group"; |
| 172 |
|
| 173 |
f = fopen(entry, "rt"); |
| 174 |
if (f == NULL) |
| 175 |
{ |
| 176 |
ticables_warning(_("can't open '%s'."), entry); |
| 177 |
return -1; |
| 178 |
} |
| 179 |
|
| 180 |
while (!feof(f)) |
| 181 |
{ |
| 182 |
if (!fgets(buffer, 256, f)) |
| 183 |
break; |
| 184 |
|
| 185 |
if (strstr(buffer, group)) |
| 186 |
{ |
| 187 |
if(strstr(buffer, user)) |
| 188 |
{ |
| 189 |
fclose(f); |
| 190 |
return 0; |
| 191 |
} |
| 192 |
else |
| 193 |
{ |
| 194 |
fclose(f); |
| 195 |
return -1; |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
fclose(f); |
| 201 |
return -1; |
| 202 |
} |
| 203 |
|
| 204 |
static int check_for_node_usability(const char *pathname) |
| 205 |
{ |
| 206 |
struct stat st; |
| 207 |
|
| 208 |
if(!access(pathname, F_OK)) |
| 209 |
{ |
| 210 |
ticables_info(_(" node %s: exists"), pathname); |
| 211 |
} |
| 212 |
else |
| 213 |
{ |
| 214 |
ticables_info(_(" node %s: does not exist"), pathname); |
| 215 |
ticables_info("%s", _(" => you will have to create the node.")); |
| 216 |
|
| 217 |
return -1; |
| 218 |
} |
| 219 |
|
| 220 |
if(!access(pathname, R_OK | W_OK)) |
| 221 |
{ |
| 222 |
ticables_info(_(" node %s: accessible"), pathname); |
| 223 |
return 0; |
| 224 |
} |
| 225 |
|
| 226 |
if(!stat(pathname, &st)) |
| 227 |
{ |
| 228 |
char s[13]; |
| 229 |
ticables_info(_(" permissions/user/group:%s%s %s"), |
| 230 |
get_attributes(s, st.st_mode), |
| 231 |
get_user_name(st.st_uid), |
| 232 |
get_group_name(st.st_gid)); |
| 233 |
} |
| 234 |
else |
| 235 |
{ |
| 236 |
ticables_warning("can't stat '%s'.", pathname); |
| 237 |
return -1; |
| 238 |
} |
| 239 |
|
| 240 |
if(getuid() == st.st_uid) |
| 241 |
{ |
| 242 |
ticables_info("%s", _(" user can r/w on device: yes")); |
| 243 |
ticables_info("%s", _(" => device is inaccessible for unknown reasons")); |
| 244 |
return -1; |
| 245 |
} |
| 246 |
else |
| 247 |
{ |
| 248 |
ticables_info("%s", _(" user can r/w on device: no")); |
| 249 |
} |
| 250 |
|
| 251 |
if((st.st_mode & S_IROTH) && (st.st_mode & S_IWOTH)) |
| 252 |
{ |
| 253 |
ticables_info("%s", _(" others can r/w on device: yes")); |
| 254 |
} |
| 255 |
else |
| 256 |
{ |
| 257 |
char *user, *group; |
| 258 |
|
| 259 |
ticables_info("%s", _(" others can r/w on device: no")); |
| 260 |
|
| 261 |
user = strdup(get_user_name(getuid())); |
| 262 |
group = strdup(get_group_name(st.st_gid)); |
| 263 |
|
| 264 |
if(!search_for_user_in_group(user, group)) |
| 265 |
{ |
| 266 |
ticables_info(_(" is the user '%s' in the group '%s': yes"), |
| 267 |
user, group); |
| 268 |
} |
| 269 |
else |
| 270 |
{ |
| 271 |
ticables_info(_(" is the user '%s' in the group '%s': no"), user, group); |
| 272 |
ticables_info(_(" => you should add your username at the group '%s' in '/etc/group'"), group); |
| 273 |
ticables_info("%s", _(" => you will have to restart your session, too")); |
| 274 |
free(user); |
| 275 |
free(group); |
| 276 |
|
| 277 |
return -1; |
| 278 |
} |
| 279 |
|
| 280 |
free(user); |
| 281 |
free(group); |
| 282 |
} |
| 283 |
|
| 284 |
ticables_info("%s", _(" => device is inaccessible for unknown reasons")); |
| 285 |
return -1; |
| 286 |
} |
| 287 |
|
| 288 |
int linux_check_root(void) |
| 289 |
{ |
| 290 |
uid_t uid = getuid(); |
| 291 |
|
| 292 |
ticables_info(_("Check for super-user access: %s"), |
| 293 |
uid ? "no" : "yes"); |
| 294 |
|
| 295 |
return (uid ? ERR_ROOT : 0); |
| 296 |
} |
| 297 |
|
| 298 |
int linux_check_tty(const char *devname) |
| 299 |
{ |
| 300 |
int fd; |
| 301 |
#ifdef HAVE_LINUX_SERIAL_H |
| 302 |
struct serial_struct serinfo; |
| 303 |
|
| 304 |
memset(&serinfo, 0, sizeof(serinfo)); |
| 305 |
#endif |
| 306 |
ticables_info("%s", _("Check for tty support:")); |
| 307 |
#ifdef HAVE_LINUX_SERIAL_H |
| 308 |
ticables_info("%s", _(" tty support: available.")); |
| 309 |
#else |
| 310 |
ticables_info("%s", _(" tty support: not compiled.")); |
| 311 |
return ERR_TTDEV; |
| 312 |
#endif |
| 313 |
|
| 314 |
// check for node usability |
| 315 |
ticables_info("%s", _("Check for tty usability:")); |
| 316 |
if(check_for_node_usability(devname) == -1) |
| 317 |
{ |
| 318 |
ticables_warning("%s", _("not usable")); |
| 319 |
return ERR_TTDEV; |
| 320 |
} |
| 321 |
|
| 322 |
#ifdef HAVE_LINUX_SERIAL_H |
| 323 |
// check for device availability |
| 324 |
fd = open(devname, 0); |
| 325 |
if (fd == -1) |
| 326 |
{ |
| 327 |
ticables_warning(_("unable to open serial device '%s'"), devname); |
| 328 |
return ERR_TTDEV; |
| 329 |
} |
| 330 |
|
| 331 |
serinfo.reserved_char[0] = 0; |
| 332 |
if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0) |
| 333 |
{ |
| 334 |
ticables_warning(_("Error running TIOCGSERIAL ioctl on device %s - %s"), devname, strerror(errno)); |
| 335 |
ticables_warning("%s", _("NOTE: proceeding nevertheless, in an attempt to help usage of libticables with some USB/RS232 adapters, though most do not work at all for the purposes of TI's protocol")); |
| 336 |
} |
| 337 |
else |
| 338 |
{ |
| 339 |
ticables_info("%s", _(" is usable: yes")); |
| 340 |
if (serinfo.type == PORT_UNKNOWN || serinfo.type == PORT_MAX) |
| 341 |
{ |
| 342 |
ticables_warning(_("Serial port has unknown type %d"), serinfo.type); |
| 343 |
ticables_warning("%s", _("NOTE: proceeding nevertheless, in an attempt to help usage of libticables with some USB/RS232 adapters, though most do not work at all for the purposes of TI's protocol")); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
close(fd); |
| 348 |
#endif |
| 349 |
|
| 350 |
return 0; |
| 351 |
} |
| 352 |
|
| 353 |
int linux_check_parport(const char *devname) |
| 354 |
{ |
| 355 |
int fd; |
| 356 |
|
| 357 |
ticables_info("%s", _("Check for parport support:")); |
| 358 |
#ifdef HAVE_LINUX_PARPORT_H |
| 359 |
ticables_info("%s", _(" parport support: available.")); |
| 360 |
#else |
| 361 |
ticables_info("%s", _(" parport support: not compiled.")); |
| 362 |
return ERR_PPDEV; |
| 363 |
#endif |
| 364 |
|
| 365 |
// check for node usability |
| 366 |
ticables_info("%s", _("Check for parport usability:")); |
| 367 |
if(check_for_node_usability(devname) == -1) |
| 368 |
return ERR_PPDEV; |
| 369 |
|
| 370 |
#ifdef HAVE_LINUX_PARPORT_H |
| 371 |
// check for device availability |
| 372 |
fd = open(devname, 0); |
| 373 |
if (fd == -1) |
| 374 |
{ |
| 375 |
ticables_warning("unable to open parallel device '%s'.", devname); |
| 376 |
return ERR_PPDEV; |
| 377 |
} |
| 378 |
|
| 379 |
if (ioctl(fd, PPCLAIM) == -1) |
| 380 |
{ |
| 381 |
ticables_info("%s", _(" is usable: no")); |
| 382 |
close(fd); |
| 383 |
return ERR_PPDEV; |
| 384 |
} |
| 385 |
|
| 386 |
ticables_info("%s", _(" is usable: yes")); |
| 387 |
|
| 388 |
if (ioctl(fd, PPRELEASE) == -1) |
| 389 |
{ |
| 390 |
ticables_info("unable to release parallel device '%s'", devname); |
| 391 |
} |
| 392 |
|
| 393 |
close(fd); |
| 394 |
#endif |
| 395 |
|
| 396 |
return 0; |
| 397 |
} |
| 398 |
|
| 399 |
#define USBFS "/proc/bus/usb/" |
| 400 |
#define DEVFS "/dev/bus/usb/" |
| 401 |
|
| 402 |
int linux_check_libusb(void) |
| 403 |
{ |
| 404 |
ticables_info("%s", _("Check for libusb support:")); |
| 405 |
#if defined(HAVE_LIBUSB) || defined(HAVE_LIBUSB_1_0) |
| 406 |
ticables_info("%s", _(" usb support: available.")); |
| 407 |
#else |
| 408 |
ticables_info("%s", _(" usb support: not compiled.")); |
| 409 |
return ERR_USBFS; |
| 410 |
#endif |
| 411 |
|
| 412 |
ticables_info("%s", _("Check for libusb usability:")); |
| 413 |
|
| 414 |
if(!access(DEVFS, F_OK)) |
| 415 |
{ |
| 416 |
ticables_info(_(" usb filesystem (%s): %s"), DEVFS, "mounted"); |
| 417 |
} |
| 418 |
else if(!access(USBFS, F_OK)) |
| 419 |
{ |
| 420 |
DIR* dir; |
| 421 |
struct dirent *dirent; |
| 422 |
int i; |
| 423 |
|
| 424 |
dir = opendir(USBFS); |
| 425 |
if(dir == NULL) |
| 426 |
{ |
| 427 |
ticables_info(_(" usb filesystem (%s): %s"), USBFS, "not mounted"); |
| 428 |
return ERR_USBFS; |
| 429 |
} |
| 430 |
|
| 431 |
for(i = 0; (dirent = readdir(dir)); i++); |
| 432 |
closedir(dir); |
| 433 |
|
| 434 |
if(i > 2) |
| 435 |
ticables_info(_(" usb filesystem (%s): %s"), USBFS, "mounted"); |
| 436 |
else |
| 437 |
{ |
| 438 |
ticables_info(_(" usb filesystem (%s): %s"), USBFS, "not mounted"); |
| 439 |
return ERR_USBFS; |
| 440 |
} |
| 441 |
} |
| 442 |
else |
| 443 |
{ |
| 444 |
ticables_info(_(" usb filesystem (/[proc|dev]/bus/usb): %s"), "not present"); |
| 445 |
ticables_info("%s", _(" => you must have udev or usbfs support.")); |
| 446 |
ticables_info("%s", _(" => take a look at the ticables2/CONFIG file.")); |
| 447 |
|
| 448 |
return ERR_USBFS; |
| 449 |
} |
| 450 |
|
| 451 |
return 0; |
| 452 |
} |