File: detect.c

package info (click to toggle)
libticables 1.2.0-2
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 3,260 kB
  • ctags: 1,519
  • sloc: ansic: 9,993; sh: 9,265; makefile: 208; sed: 16
file content (477 lines) | stat: -rw-r--r-- 10,242 bytes parent folder | download
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/* Hey EMACS -*- linux-c -*- */
/* $Id: detect.c 3905 2007-10-14 02:56:10Z kevinkofler $ */

/*  libticables2 - link cable library, a part of the TiLP project
 *  Copyright (C) 1999-2005  Romain Lievin
 *  Copyright (C) 2007  Kevin Kofler
 *
 *  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; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/* Linux probing module */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "stdints1.h"
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#ifdef HAVE_LINUX_PARPORT_H
#include <linux/parport.h>
#include <linux/ppdev.h>
#endif
#ifdef HAVE_LINUX_SERIAL_H
#include <linux/serial.h>
#endif
#ifdef HAVE_LINUX_TICABLE_H
# include <linux/ticable.h>     //ioctl codes
# ifndef IOCTL_TIUSB_GET_MAXPS
#  define IOCTL_TIUSB_GET_MAXPS      _IOR('N', 0x23, int) /* max packet size */
# endif
#endif
#include <dirent.h>

#include "../gettext.h"
#include "../error.h"
#include "../logging.h"

/*
  Returns mode string from mode value.
*/
static const char *get_attributes(mode_t attrib)
{
    const char *i = " ---------- ";
    static char s[13];

    strcpy(s, i);

    if (attrib & S_IRUSR)
	s[2] = 'r';
    if (attrib & S_IWUSR)
	s[3] = 'w';
    if (attrib & S_ISUID) 
    {
	if (attrib & S_IXUSR)
	    s[4] = 's';
	else
	    s[4] = 'S';
    }
    else if (attrib & S_IXUSR)
	s[4] = 'x';

    if (attrib & S_IRGRP)
	s[5] = 'r';
    if (attrib & S_IWGRP)
	s[6] = 'w';
    if (attrib & S_ISGID) 
    {
	if (attrib & S_IXGRP)
	    s[7] = 's';
	else
	    s[7] = 'S';
    }
    else if (attrib & S_IXGRP)
	s[7] = 'x';

    if (attrib & S_IROTH)
	s[8] = 'r';
    if (attrib & S_IWOTH)
	s[9] = 'w';
    if (attrib & S_ISVTX) 
    {
	if (attrib & S_IXOTH)
	    s[10] = 't';
	else
	    s[10] = 'T';
    }

    return s;
}

/*
  Returns user name from id.
*/
static const char *get_user_name(uid_t uid)
{
    struct passwd *pwuid;
    
    if((pwuid = getpwuid(uid)) != NULL)
	return pwuid->pw_name;
    
    return "not found";
}

/*
  Returns group name from id.
*/
static const char *get_group_name(uid_t uid)
{
    struct group *grpid;
    
    if ((grpid = getgrgid(uid)) != NULL)
	return grpid->gr_name;
    
    return "not found";
}

/* 
   Attempt to find a specific string in /proc (vfs) 
   - entry [in] : an entry such as '/proc/devices'
   - str [in) : an occurence to find (such as 'tipar')
*/
#if 0
static int find_string_in_proc(char *entry, char *str)
{
	FILE *f;
	char buffer[80];
	int found = 0;
	
	f = fopen(entry, "rt");
	if (f == NULL)
	{
	    ticables_warning("can't open '%s'", entry);
	    return -1;
	}

	while (!feof(f)) 
	{
		fscanf(f, "%s", buffer);
		if (strstr(buffer, str))
			found = 1;
	}
	fclose(f);
	
	return found;
}
#endif

/* 
   Attempt to find if an user is attached to a group.
   - user [in] : a user name
   - group [in] : a group name
*/
static int search_for_user_in_group(const char *user, const char *group)
{
    FILE *f;
    char buffer[256];
    const char *entry = "/etc/group";
    
    f = fopen(entry, "rt");
    if (f == NULL) 
    {
	ticables_warning(_("can't open '%s'."), entry);
	return -1;
    }
    
    while (!feof(f)) 
    {
	if (!fgets(buffer, 256, f))
		break;
	
	if (strstr(buffer, group)) 
	{
	    if(strstr(buffer, user)) 
	    {
		fclose(f);
		return 0;
	    } 
	    else 
	    {
		fclose(f);
		return -1;
	    }
	}
    }
    
    fclose(f);
    return -1;
}

static int check_for_node_usability(const char *pathname)
{
    struct stat st;
    
    if(!access(pathname, F_OK))
    {
	ticables_info(_("    node %s: exists"), pathname);
    }
    else 
    {
	ticables_info(_("    node %s: does not exist"), pathname);
	ticables_info(_("    => you will have to create the node."));
	
	return -1;
    }

    if(!access(pathname, R_OK | W_OK))
    {
       ticables_info(_("    node %s: accessible"), pathname);
       return 0;
    }
    
    if(!stat(pathname, &st)) 
    {
	ticables_info(_("    permissions/user/group:%s%s %s"),
		      get_attributes(st.st_mode),
		      get_user_name(st.st_uid),
		      get_group_name(st.st_gid));
    } 
    else 
    {
	ticables_warning("can't stat '%s'.", pathname);
	return -1;
    }	
    
    if(getuid() == st.st_uid) 
    {
	ticables_info(_("    user can r/w on device: yes"));
	ticables_info(_("    => device is inaccessible for unknown reasons (SELinux?)"));
    return -1;
    } 
    else 
    {
	ticables_info(_("    user can r/w on device: no"));
    }
    
    if((st.st_mode & S_IROTH) && (st.st_mode & S_IWOTH))
    {
	ticables_info(_("    others can r/w on device: yes"));
    }
    else 
    {
	char *user, *group;
	
	ticables_info(_("    others can r/w on device: no"));
	
	user = strdup(get_user_name(getuid()));
	group = strdup(get_group_name(st.st_gid));
	
	if(!search_for_user_in_group(user, group))
	{
	    ticables_info(_("    is the user '%s' in the group '%s': yes"), 
			  user, group); 
	}
	else 
	{
	    ticables_info(_("    is the user '%s' in the group '%s': no"), user, group);
	    ticables_info(_("    => you should add your username at the group '%s' in '/etc/group'"), group);
	    ticables_info(_("    => you will have to restart your session, too"));
	    free(user); 
	    free(group);
	    
	    return -1;	
	}
	
	free(user); 
	free(group);
    }	
    
	ticables_info(_("    => device is inaccessible for unknown reasons (SELinux?)"));
    return -1;
}

int linux_check_root(void)
{
    uid_t uid = getuid();
    
    ticables_info(_("Check for super-user access: %s"), 
		  uid ? "no" : "yes");
    
    return (uid ? ERR_ROOT : 0);
}

int linux_check_tty(const char *devname)
{
    struct serial_struct serinfo = { 0 };
    int fd;

    ticables_info(_("Check for tty support:"));
#ifdef HAVE_LINUX_SERIAL_H
    ticables_info(_("    tty support: available."));
#else
    ticables_info(_("    tty support: not compiled."));
    return ERR_TTDEV;
#endif

    // check for node usability
    ticables_info(_("Check for tty usability:"));
    if(check_for_node_usability(devname) == -1)
	return ERR_TTDEV;

#ifdef HAVE_LINUX_SERIAL_H
    // check for device availability
    fd = open(devname, 0);
    if (fd == -1)
    {
        ticables_warning("unable to open serial device '%s'", devname);
        return ERR_TTDEV;
    }
    
    serinfo.reserved_char[0] = 0;
    if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0)
    {
        close(fd);
        return ERR_TTDEV;
    }

    if(serinfo.type == PORT_UNKNOWN || serinfo.type == PORT_MAX)
    {
	ticables_info(_("    is useable: no"));
        close(fd);
        return ERR_TTDEV;
    }

    ticables_info(_("    is useable: yes"));
    close(fd);
#endif

    return 0;
}

int linux_check_parport(const char *devname)
{
    int fd;

    ticables_info(_("Check for parport support:"));
#ifdef HAVE_LINUX_PARPORT_H    
    ticables_info(_("    parport support: available."));
#else
    ticables_info(_("    parport support: not compiled."));
    return ERR_PPDEV;
#endif

    // check for node usability
    ticables_info(_("Check for parport usability:"));
    if(check_for_node_usability(devname) == -1)
        return ERR_PPDEV;

#ifdef HAVE_LINUX_PARPORT_H    
    // check for device availability
    fd = open(devname, 0);
    if (fd == -1)
    {
        ticables_warning("unable to open parallel device '%s'.", devname);
        return ERR_PPDEV;
    }

    if (ioctl(fd, PPCLAIM) == -1)
    {
	ticables_info(_("    is useable: no"));
	close(fd);
        return ERR_PPDEV;
    }

    ticables_info(_("    is useable: yes"));
    close(fd);
#endif

    return 0;
}

#define	USBFS	"/proc/bus/usb/"
#define DEVFS    "/dev/bus/usb/"

int linux_check_libusb(void)
{
	ticables_info(_("Check for lib-usb support:"));
#if defined(HAVE_LIBUSB)
	ticables_info(_("    usb support: available."));
#else
	ticables_info(_("    usb support: not compiled."));
	return ERR_USBFS;
#endif

    ticables_info(_("Check for lib-usb usability:"));

    if(!access(DEVFS, F_OK))
    {
	ticables_info(_("    usb filesystem (%s): %s"), DEVFS, "mounted");
    }
    else if(!access(USBFS, F_OK))
    {
      DIR* dir;
      struct dirent *dirent;
      int i;

      dir = opendir(USBFS);
      if(dir == NULL)
	{
	  ticables_info(_("    usb filesystem (%s): %s"), USBFS, "not mounted");
	  return ERR_USBFS;
	}
      
      for(i = 0; (dirent = readdir(dir)); i++);
      closedir(dir);

      if(i > 2)
	ticables_info(_("    usb filesystem (%s): %s"), USBFS, "mounted");
      else
	{
	  ticables_info(_("    usb filesystem (%s): %s"), USBFS, "not mounted");
	  return ERR_USBFS;
	}
    }
    else
    {
	ticables_info(_("    usb filesystem (/[proc|dev]/bus/usb): %s"), "not present");
	ticables_info(_("    => you must have udev or usbfs support."));
	ticables_info(_("    => take a look at the ticables2/CONFIG file."));
	
	return ERR_USBFS;
    }

    return 0;
}

int linux_check_tiusb(const char *devname)
{
    int fd;
    int arg;

    // check for node usability
    ticables_info(_("Check for tiusb usability:"));
    if(check_for_node_usability(devname) == -1)
        return ERR_TTDEV;

#ifdef HAVE_LINUX_TICABLE_H
    // check for device availability
    fd = open(devname, 0);
    if (fd == -1)
    {
        ticables_warning("unable to open USB device '%s'", devname);
        return ERR_TTDEV;
    }

    if (ioctl(fd, IOCTL_TIUSB_GET_MAXPS, &arg) == -1)
	return ERR_TTDEV;

    ticables_info(_("    is useable: yes"));
    close(fd);

    return 0;
#else
    ticables_info(_("    tiusb support: not compiled."));
    return ERR_TTDEV;
#endif
}