| 1 |
/* Hey EMACS -*- linux-c -*- */ |
| 2 |
/* $Id: detect.c 2618 2006-07-20 08:44:49Z kevinkofler $ */ |
| 3 |
|
| 4 |
/* libticables2 - link cable library, a part of the TiLP project |
| 5 |
* Copyright (C) 1999-2005 Romain Lievin |
| 6 |
* Copyright (C) 2006 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 |
/* Mac OS X 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(!stat(pathname, &st)) |
| 180 |
{ |
| 181 |
char s[13]; |
| 182 |
ticables_info(_(" permissions/user/group:%s%s %s"), |
| 183 |
get_attributes(s, st.st_mode), |
| 184 |
get_user_name(st.st_uid), |
| 185 |
get_group_name(st.st_gid)); |
| 186 |
} |
| 187 |
else |
| 188 |
{ |
| 189 |
ticables_warning("can't stat '%s'.", pathname); |
| 190 |
return -1; |
| 191 |
} |
| 192 |
|
| 193 |
if(getuid() == st.st_uid) |
| 194 |
{ |
| 195 |
ticables_info("%s", _(" user can r/w on device: yes")); |
| 196 |
return 0; |
| 197 |
} |
| 198 |
else |
| 199 |
{ |
| 200 |
ticables_info("%s", _(" user can r/w on device: no")); |
| 201 |
} |
| 202 |
|
| 203 |
if((st.st_mode & S_IROTH) && (st.st_mode & S_IWOTH)) |
| 204 |
{ |
| 205 |
ticables_info("%s", _(" others can r/w on device: yes")); |
| 206 |
} |
| 207 |
else |
| 208 |
{ |
| 209 |
char *user, *group; |
| 210 |
|
| 211 |
ticables_info("%s", _(" others can r/w on device: no")); |
| 212 |
|
| 213 |
user = strdup(get_user_name(getuid())); |
| 214 |
group = strdup(get_group_name(st.st_gid)); |
| 215 |
|
| 216 |
if(!search_for_user_in_group(user, group)) |
| 217 |
{ |
| 218 |
ticables_info(_(" is the user '%s' in the group '%s': yes"), |
| 219 |
user, group); |
| 220 |
} |
| 221 |
else |
| 222 |
{ |
| 223 |
ticables_info(_(" is the user '%s' in the group '%s': no"), user, group); |
| 224 |
ticables_info(_(" => you should add your username at the group '%s' in '/etc/group'"), group); |
| 225 |
ticables_info("%s", _(" => you will have to restart your session, too")); |
| 226 |
free(user); |
| 227 |
free(group); |
| 228 |
|
| 229 |
return -1; |
| 230 |
} |
| 231 |
|
| 232 |
free(user); |
| 233 |
free(group); |
| 234 |
} |
| 235 |
|
| 236 |
return 0; |
| 237 |
} |
| 238 |
|
| 239 |
int macosx_check_root(void) |
| 240 |
{ |
| 241 |
uid_t uid = getuid(); |
| 242 |
|
| 243 |
ticables_info(_("Check for super-user access: %s"), |
| 244 |
uid ? "no" : "yes"); |
| 245 |
|
| 246 |
return (uid ? ERR_ROOT : 0); |
| 247 |
} |
| 248 |
|
| 249 |
int macosx_check_tty(const char *devname) |
| 250 |
{ |
| 251 |
ticables_info("%s", _("Check for tty usability:")); |
| 252 |
if(check_for_node_usability(devname) == -1) |
| 253 |
return ERR_TTDEV; |
| 254 |
|
| 255 |
return 0; |
| 256 |
} |
| 257 |
|
| 258 |
int macosx_check_parport(const char *devname) |
| 259 |
{ |
| 260 |
return ERR_PPDEV; |
| 261 |
} |
| 262 |
|
| 263 |
int macosx_check_libusb(void) |
| 264 |
{ |
| 265 |
ticables_info("%s", _("Check for libusb support:")); |
| 266 |
#if defined(HAVE_LIBUSB) || defined(HAVE_LIBUSB_1_0) |
| 267 |
ticables_info("%s", _(" usb support: available.")); |
| 268 |
return 0; |
| 269 |
#else |
| 270 |
ticables_info("%s", _(" usb support: not compiled.")); |
| 271 |
return ERR_USBFS; |
| 272 |
#endif |
| 273 |
} |