| 1 |
/* |
| 2 |
Copyright (C) 2001-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Inetutils. |
| 5 |
|
| 6 |
GNU Inetutils is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Inetutils 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 |
| 17 |
along with this program. If not, see `http://www.gnu.org/licenses/'. */ |
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
|
| 21 |
#include <intalkd.h> |
| 22 |
#include <pwd.h> |
| 23 |
#include <regex.h> |
| 24 |
#include <ctype.h> |
| 25 |
#include "argcv.h" |
| 26 |
|
| 27 |
typedef struct netdef netdef_t; |
| 28 |
|
| 29 |
struct netdef |
| 30 |
{ |
| 31 |
netdef_t *next; |
| 32 |
in_addr_t ipaddr; |
| 33 |
in_addr_t netmask; |
| 34 |
}; |
| 35 |
|
| 36 |
typedef struct acl acl_t; |
| 37 |
|
| 38 |
struct acl |
| 39 |
{ |
| 40 |
acl_t *next; |
| 41 |
regex_t re; |
| 42 |
netdef_t *netlist; |
| 43 |
int system; |
| 44 |
int action; |
| 45 |
}; |
| 46 |
|
| 47 |
|
| 48 |
acl_t *acl_head, *acl_tail; |
| 49 |
|
| 50 |
#define DOTTED_QUAD_LEN 16 |
| 51 |
|
| 52 |
static int |
| 53 |
read_address (char **line_ptr, char *ptr) |
| 54 |
{ |
| 55 |
char *startp = *line_ptr; |
| 56 |
char *endp; |
| 57 |
int dotcount = 0; |
| 58 |
|
| 59 |
for (endp = startp; *endp; endp++, ptr++) |
| 60 |
if (!(isdigit (*endp) || *endp == '.')) |
| 61 |
break; |
| 62 |
else if (endp < startp + DOTTED_QUAD_LEN) |
| 63 |
{ |
| 64 |
if (*endp == '.') |
| 65 |
dotcount++; |
| 66 |
*ptr = *endp; |
| 67 |
} |
| 68 |
else |
| 69 |
break; |
| 70 |
*line_ptr = endp; |
| 71 |
*ptr = 0; |
| 72 |
return dotcount; |
| 73 |
} |
| 74 |
|
| 75 |
static netdef_t * |
| 76 |
netdef_parse (char *str) |
| 77 |
{ |
| 78 |
in_addr_t ipaddr, netmask; |
| 79 |
netdef_t *netdef; |
| 80 |
char ipbuf[DOTTED_QUAD_LEN + 1]; |
| 81 |
|
| 82 |
if (strcmp (str, "any") == 0) |
| 83 |
{ |
| 84 |
ipaddr = 0; |
| 85 |
netmask = 0; |
| 86 |
} |
| 87 |
else |
| 88 |
{ |
| 89 |
read_address (&str, ipbuf); |
| 90 |
ipaddr = inet_addr (ipbuf); |
| 91 |
if (ipaddr == INADDR_NONE) |
| 92 |
return NULL; |
| 93 |
if (*str == 0) |
| 94 |
netmask = 0xfffffffful; |
| 95 |
else if (*str != '/') |
| 96 |
return NULL; |
| 97 |
else |
| 98 |
{ |
| 99 |
str++; |
| 100 |
if (read_address (&str, ipbuf) == 0) |
| 101 |
{ |
| 102 |
/* netmask length */ |
| 103 |
unsigned int len = strtoul (ipbuf, NULL, 0); |
| 104 |
if (len > 32) |
| 105 |
return NULL; |
| 106 |
netmask = 0xfffffffful >> (32 - len); |
| 107 |
netmask <<= (32 - len); |
| 108 |
/*FIXME: hostorder? */ |
| 109 |
} |
| 110 |
else |
| 111 |
netmask = inet_network (ipbuf); |
| 112 |
netmask = htonl (netmask); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
netdef = malloc (sizeof *netdef); |
| 117 |
if (!netdef) |
| 118 |
{ |
| 119 |
syslog (LOG_ERR, "Out of memory"); |
| 120 |
exit (EXIT_FAILURE); |
| 121 |
} |
| 122 |
|
| 123 |
netdef->next = NULL; |
| 124 |
netdef->ipaddr = ipaddr; |
| 125 |
netdef->netmask = netmask; |
| 126 |
|
| 127 |
return netdef; |
| 128 |
} |
| 129 |
|
| 130 |
void |
| 131 |
read_acl (char *config_file, int system) |
| 132 |
{ |
| 133 |
FILE *fp; |
| 134 |
int line; |
| 135 |
char buf[128]; |
| 136 |
char *ptr; |
| 137 |
|
| 138 |
if (!config_file || !config_file[0]) |
| 139 |
return; |
| 140 |
|
| 141 |
fp = fopen (config_file, "r"); |
| 142 |
if (!fp) |
| 143 |
{ |
| 144 |
if (system > 0) |
| 145 |
{ |
| 146 |
/* A missing, yet specified, site-wide ACL is a serious error. |
| 147 |
* Abort execution whenever this happens. |
| 148 |
*/ |
| 149 |
syslog (LOG_ERR, "Cannot open config file %s: %m", config_file); |
| 150 |
exit (EXIT_FAILURE); |
| 151 |
} |
| 152 |
return; /* User setting may fail to exist. Just ignore. */ |
| 153 |
} |
| 154 |
|
| 155 |
if (system < 0) |
| 156 |
{ |
| 157 |
/* Alarmed state, violating file access policy. |
| 158 |
* Insert a single, general denial equivalent to |
| 159 |
* a rule reading `deny .* any'. |
| 160 |
*/ |
| 161 |
const char any_user[] = "^.*$"; |
| 162 |
char any_host[] = "any"; |
| 163 |
acl_t *acl; |
| 164 |
regex_t re; |
| 165 |
netdef_t *cur; |
| 166 |
|
| 167 |
fclose (fp); |
| 168 |
acl = malloc (sizeof *acl); |
| 169 |
|
| 170 |
if (!acl) |
| 171 |
{ |
| 172 |
syslog (LOG_ERR, "Out of memory"); |
| 173 |
exit (EXIT_FAILURE); |
| 174 |
} |
| 175 |
if (regcomp (&re, any_user, 0) != 0) |
| 176 |
{ |
| 177 |
syslog (LOG_ERR, "Bad regexp '%s'.", any_user); |
| 178 |
exit (EXIT_FAILURE); |
| 179 |
} |
| 180 |
cur = netdef_parse (any_host); |
| 181 |
if (!cur) |
| 182 |
{ |
| 183 |
syslog (LOG_ERR, "Cannot parse netdef '%s'.", any_host); |
| 184 |
exit (EXIT_FAILURE); |
| 185 |
} |
| 186 |
|
| 187 |
acl->next = NULL; |
| 188 |
acl->action = ACL_DENY; |
| 189 |
acl->system = 0; |
| 190 |
acl->re = re; |
| 191 |
acl->netlist = cur; |
| 192 |
|
| 193 |
if (!acl_tail) |
| 194 |
acl_head = acl; |
| 195 |
else |
| 196 |
acl_tail->next = acl; |
| 197 |
acl_tail = acl; |
| 198 |
|
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
line = 0; |
| 203 |
while ((ptr = fgets (buf, sizeof buf, fp))) |
| 204 |
{ |
| 205 |
int len, i; |
| 206 |
int argc; |
| 207 |
char **argv; |
| 208 |
int action; |
| 209 |
regex_t re; |
| 210 |
netdef_t *head, *tail; |
| 211 |
acl_t *acl; |
| 212 |
|
| 213 |
line++; |
| 214 |
len = strlen (ptr); |
| 215 |
if (len > 0 && ptr[len - 1] == '\n') |
| 216 |
ptr[--len] = 0; |
| 217 |
|
| 218 |
while (*ptr && isspace (*ptr)) |
| 219 |
ptr++; |
| 220 |
if (!*ptr || *ptr == '#') |
| 221 |
continue; |
| 222 |
|
| 223 |
argcv_get (ptr, "", &argc, &argv); |
| 224 |
if (argc < 2) |
| 225 |
{ |
| 226 |
syslog (LOG_ERR, "%s:%d: too few fields", config_file, line); |
| 227 |
argcv_free (argc, argv); |
| 228 |
continue; |
| 229 |
} |
| 230 |
|
| 231 |
if (strcmp (argv[0], "allow") == 0) |
| 232 |
action = ACL_ALLOW; |
| 233 |
else if (strcmp (argv[0], "deny") == 0) |
| 234 |
action = ACL_DENY; |
| 235 |
else |
| 236 |
{ |
| 237 |
syslog (LOG_WARNING, "%s:%d: unknown keyword", config_file, line); |
| 238 |
argcv_free (argc, argv); |
| 239 |
continue; |
| 240 |
} |
| 241 |
|
| 242 |
if (regcomp (&re, argv[1], 0) != 0) |
| 243 |
{ |
| 244 |
syslog (LOG_WARNING, "%s:%d: bad regexp", config_file, line); |
| 245 |
argcv_free (argc, argv); |
| 246 |
continue; |
| 247 |
} |
| 248 |
|
| 249 |
head = tail = NULL; |
| 250 |
for (i = 2; i < argc; i++) |
| 251 |
{ |
| 252 |
netdef_t *cur = netdef_parse (argv[i]); |
| 253 |
if (!cur) |
| 254 |
{ |
| 255 |
syslog (LOG_ERR, "%s:%d: can't parse netdef: %s", |
| 256 |
config_file, line, argv[i]); |
| 257 |
continue; |
| 258 |
} |
| 259 |
if (!tail) |
| 260 |
head = cur; |
| 261 |
else |
| 262 |
tail->next = cur; |
| 263 |
tail = cur; |
| 264 |
} |
| 265 |
|
| 266 |
argcv_free (argc, argv); |
| 267 |
|
| 268 |
acl = malloc (sizeof *acl); |
| 269 |
if (!acl) |
| 270 |
{ |
| 271 |
syslog (LOG_ERR, "Out of memory"); |
| 272 |
exit (EXIT_FAILURE); |
| 273 |
} |
| 274 |
acl->next = NULL; |
| 275 |
acl->action = action; |
| 276 |
acl->system = system; |
| 277 |
acl->netlist = head; |
| 278 |
acl->re = re; |
| 279 |
|
| 280 |
if (!acl_tail) |
| 281 |
acl_head = acl; |
| 282 |
else |
| 283 |
acl_tail->next = acl; |
| 284 |
acl_tail = acl; |
| 285 |
} |
| 286 |
|
| 287 |
fclose (fp); |
| 288 |
} |
| 289 |
|
| 290 |
static acl_t * |
| 291 |
open_users_acl (char *name) |
| 292 |
{ |
| 293 |
int level = 0; /* Private file, not mandatory. */ |
| 294 |
int rc; |
| 295 |
char *filename; |
| 296 |
struct passwd *pw; |
| 297 |
struct stat st; |
| 298 |
acl_t *mark; |
| 299 |
|
| 300 |
pw = getpwnam (name); |
| 301 |
if (!pw) |
| 302 |
return NULL; |
| 303 |
|
| 304 |
filename = |
| 305 |
malloc (strlen (pw->pw_dir) + sizeof (USER_ACL_NAME) + |
| 306 |
2 /* Null and separator. */ ); |
| 307 |
if (!filename) |
| 308 |
{ |
| 309 |
syslog (LOG_ERR, "Out of memory"); |
| 310 |
return NULL; |
| 311 |
} |
| 312 |
|
| 313 |
sprintf (filename, "%s/%s", pw->pw_dir, USER_ACL_NAME); |
| 314 |
|
| 315 |
/* The location must be a file, and must be owned by the |
| 316 |
* indicated user and his corresponding group. No write |
| 317 |
* access by group or world. Record a syslog warning, |
| 318 |
* should either of these not be true. |
| 319 |
*/ |
| 320 |
rc = stat (filename, &st); |
| 321 |
if (rc < 0) |
| 322 |
return NULL; |
| 323 |
if (!S_ISREG (st.st_mode) |
| 324 |
|| st.st_uid != pw->pw_uid |
| 325 |
|| st.st_gid != pw->pw_gid |
| 326 |
|| st.st_mode & S_IWGRP || st.st_mode & S_IWOTH) |
| 327 |
{ |
| 328 |
if (logging || debug) |
| 329 |
syslog (LOG_WARNING, "Discarding '%s': insecure access.", filename); |
| 330 |
level = -1; /* Enforce a deny rule. */ |
| 331 |
} |
| 332 |
|
| 333 |
mark = acl_tail; |
| 334 |
read_acl (filename, level); |
| 335 |
free (filename); |
| 336 |
|
| 337 |
return mark; |
| 338 |
} |
| 339 |
|
| 340 |
static void |
| 341 |
netdef_free (netdef_t *netdef) |
| 342 |
{ |
| 343 |
netdef_t *next; |
| 344 |
|
| 345 |
while (netdef) |
| 346 |
{ |
| 347 |
next = netdef->next; |
| 348 |
free (netdef); |
| 349 |
netdef = next; |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
static void |
| 354 |
acl_free (acl_t *acl) |
| 355 |
{ |
| 356 |
acl_t *next; |
| 357 |
|
| 358 |
while (acl) |
| 359 |
{ |
| 360 |
next = acl->next; |
| 361 |
regfree (&acl->re); |
| 362 |
netdef_free (acl->netlist); |
| 363 |
free (acl); |
| 364 |
acl = next; |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
static void |
| 369 |
discard_acl (acl_t *mark) |
| 370 |
{ |
| 371 |
if (mark) |
| 372 |
{ |
| 373 |
acl_free (mark->next); |
| 374 |
acl_tail = mark; |
| 375 |
acl_tail->next = NULL; |
| 376 |
} |
| 377 |
else |
| 378 |
acl_head = acl_tail = NULL; |
| 379 |
} |
| 380 |
|
| 381 |
int |
| 382 |
acl_match (CTL_MSG *msg, struct sockaddr_in *sa_in) |
| 383 |
{ |
| 384 |
acl_t *acl, *mark; |
| 385 |
in_addr_t ip; |
| 386 |
int system_action = ACL_ALLOW, user_action = ACL_ALLOW; |
| 387 |
int found_user_acl = 0; |
| 388 |
|
| 389 |
if (strict_policy) |
| 390 |
system_action = ACL_DENY; |
| 391 |
|
| 392 |
mark = open_users_acl (msg->r_name); |
| 393 |
ip = sa_in->sin_addr.s_addr; |
| 394 |
if (mark && (mark != acl_tail)) |
| 395 |
found_user_acl = 1; |
| 396 |
|
| 397 |
for (acl = acl_head; acl; acl = acl->next) |
| 398 |
{ |
| 399 |
netdef_t *net; |
| 400 |
|
| 401 |
for (net = acl->netlist; net; net = net->next) |
| 402 |
{ |
| 403 |
/* Help the administrator and his users |
| 404 |
* to simplify net list syntax: |
| 405 |
* |
| 406 |
* mask the address `net->ipaddr' with |
| 407 |
* `net->netmask' for less computations |
| 408 |
* within the ACL specification. |
| 409 |
*/ |
| 410 |
if ((net->ipaddr & net->netmask) == (ip & net->netmask)) |
| 411 |
{ |
| 412 |
/* |
| 413 |
* Site-wide ACLs concern user's name on this machine, |
| 414 |
* whereas user's rules concern the incoming client name. |
| 415 |
*/ |
| 416 |
if (acl->system && |
| 417 |
regexec (&acl->re, msg->r_name, 0, NULL, 0) == 0) |
| 418 |
system_action = acl->action; |
| 419 |
else if (regexec (&acl->re, msg->l_name, 0, NULL, 0) == 0) |
| 420 |
user_action = acl->action; |
| 421 |
} |
| 422 |
} |
| 423 |
} |
| 424 |
discard_acl (mark); |
| 425 |
|
| 426 |
if (system_action == ACL_ALLOW) |
| 427 |
return user_action; |
| 428 |
|
| 429 |
if (strict_policy) |
| 430 |
return ACL_DENY; /* Equal to `system_action'. */ |
| 431 |
|
| 432 |
/* At this point it is known that last activated site-wide |
| 433 |
* ACL rule has set SYSTEM_ACTION to ACL_DENY. Do we |
| 434 |
* always want it to be overridable? |
| 435 |
*/ |
| 436 |
|
| 437 |
/* Override ACL_DENY only if there was a user specific file |
| 438 |
* ~/.talkrc containing some active rules at all. In other |
| 439 |
* words, a site-policy claiming `deny' will need an act of |
| 440 |
* will by the user in order that it be overridden. |
| 441 |
*/ |
| 442 |
|
| 443 |
return found_user_acl ? user_action : ACL_DENY; |
| 444 |
} |