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 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
|
%{
/* ----------------------------------------------------------------------- *
*
* master_parser.y - master map buffer parser.
*
* Copyright 2006 Ian Kent <raven@themaw.net>
*
* 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, Inc., 675 Mass Ave, Cambridge MA 02139,
* USA; 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.
*
* ----------------------------------------------------------------------- */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include <sys/ioctl.h>
#include "automount.h"
#include "master.h"
#define MAX_ERR_LEN 512
extern struct master *master_list;
char **add_argv(int, char **, char *);
const char **copy_argv(int, const char **);
int free_argv(int, const char **);
extern FILE *master_in;
extern char *master_text;
extern int master_lex(void);
extern int master_lineno;
extern void master_set_scan_buffer(const char *);
static char *master_strdup(char *);
static void local_init_vars(void);
static void local_free_vars(void);
static void trim_maptype(char *);
static int add_multi_mapstr(void);
static int master_error(const char *s);
static int master_notify(const char *s);
static int master_msg(const char *s);
static char *path;
static char *type;
static char *format;
static long timeout;
static long negative_timeout;
static unsigned nobind;
static unsigned ghost;
extern unsigned global_selection_options;
static unsigned random_selection;
static unsigned use_weight;
static char **tmp_argv;
static int tmp_argc;
static char **local_argv;
static int local_argc;
static char errstr[MAX_ERR_LEN];
static unsigned int verbose;
static unsigned int debug;
static int lineno;
#define YYDEBUG 0
#ifndef YYENABLE_NLS
#define YYENABLE_NLS 0
#endif
#ifndef YYLTYPE_IS_TRIVIAL
#define YYLTYPE_IS_TRIVIAL 0
#endif
#if YYDEBUG
static int master_fprintf(FILE *, char *, ...);
#undef YYFPRINTF
#define YYFPRINTF master_fprintf
#endif
%}
%union {
char strtype[2048];
int inttype;
long longtype;
}
%token COMMENT
%token MAP
%token OPT_TIMEOUT OPT_NTIMEOUT OPT_NOBIND OPT_NOGHOST OPT_GHOST OPT_VERBOSE
%token OPT_DEBUG OPT_RANDOM OPT_USE_WEIGHT
%token COLON COMMA NL DDASH
%type <strtype> map
%type <strtype> options
%type <strtype> dn
%type <strtype> dnattrs
%type <strtype> dnattr
%type <strtype> option
%type <strtype> daemon_option
%type <strtype> mount_option
%token <strtype> PATH
%token <strtype> QUOTE
%token <strtype> NILL
%token <strtype> SPACE
%token <strtype> EQUAL
%token <strtype> MULTITYPE
%token <strtype> MAPTYPE
%token <strtype> DNSERVER
%token <strtype> DNATTR
%token <strtype> DNNAME
%token <strtype> MAPHOSTS
%token <strtype> MAPNULL
%token <strtype> MAPXFN
%token <strtype> MAPNAME
%token <longtype> NUMBER
%token <strtype> OPTION
%start file
%%
file: {
master_lineno = 0;
#if YYDEBUG != 0
master_debug = YYDEBUG;
#endif
} line
;
line:
| PATH mapspec
{
path = master_strdup($1);
if (!path) {
local_free_vars();
YYABORT;
}
}
| PATH MULTITYPE maplist
{
char *tmp = NULL;
trim_maptype($2);
path = master_strdup($1);
if (!path) {
master_error("memory allocation error");
local_free_vars();
YYABORT;
}
if ((tmp = strchr($2, ',')))
*tmp++ = '\0';
type = master_strdup($2);
if (!type) {
master_error("memory allocation error");
local_free_vars();
YYABORT;
}
if (tmp) {
format = master_strdup(tmp);
if (!format) {
master_error("memory allocation error");
local_free_vars();
YYABORT;
}
}
}
| PATH COLON { master_notify($1); YYABORT; }
| PATH OPTION { master_notify($2); YYABORT; }
| PATH NILL { master_notify($2); YYABORT; }
| PATH OPT_RANDOM { master_notify($1); YYABORT; }
| PATH OPT_USE_WEIGHT { master_notify($1); YYABORT; }
| PATH OPT_DEBUG { master_notify($1); YYABORT; }
| PATH OPT_TIMEOUT { master_notify($1); YYABORT; }
| PATH OPT_NOBIND { master_notify($1); YYABORT; }
| PATH OPT_GHOST { master_notify($1); YYABORT; }
| PATH OPT_NOGHOST { master_notify($1); YYABORT; }
| PATH OPT_VERBOSE { master_notify($1); YYABORT; }
| PATH { master_notify($1); YYABORT; }
| QUOTE { master_notify($1); YYABORT; }
| OPTION { master_notify($1); YYABORT; }
| NILL { master_notify($1); YYABORT; }
| COMMENT { YYABORT; }
;
mapspec: map
{
local_argc = tmp_argc;
local_argv = tmp_argv;
tmp_argc = 0;
tmp_argv = NULL;
}
| map options
{
local_argc = tmp_argc;
local_argv = tmp_argv;
tmp_argc = 0;
tmp_argv = NULL;
}
;
maplist: map
{
if (!add_multi_mapstr()) {
master_error("memory allocation error");
local_free_vars();
YYABORT;
}
}
| map options
{
if (!add_multi_mapstr()) {
master_error("memory allocation error");
local_free_vars();
YYABORT;
}
}
| maplist DDASH map
{
local_argc++;
local_argv = add_argv(local_argc, local_argv, "--");
if (!local_argv) {
master_error("memory allocation error");
local_free_vars();
YYABORT;
}
if (!add_multi_mapstr()) {
master_error("memory allocation error");
local_free_vars();
YYABORT;
}
}
| maplist DDASH map options
{
local_argc++;
local_argv = add_argv(local_argc, local_argv, "--");
if (!local_argv) {
master_error("memory allocation error");
local_free_vars();
YYABORT;
}
if (!add_multi_mapstr()) {
master_error("memory allocation error");
local_free_vars();
YYABORT;
}
}
;
map: PATH
{
tmp_argc++;
tmp_argv = add_argv(tmp_argc, tmp_argv, $1);
if (!tmp_argv) {
master_error("memory allocation error");
local_free_vars();
YYABORT;
}
}
| MAPNAME
{
tmp_argc++;
tmp_argv = add_argv(tmp_argc, tmp_argv, $1);
if (!tmp_argv) {
master_error("memory allocation error");
local_free_vars();
YYABORT;
}
}
| MAPHOSTS
{
type = master_strdup($1 + 1);
if (!type) {
local_free_vars();
YYABORT;
}
}
| MAPXFN
{
master_notify($1);
master_msg("X/Open Federated Naming service not supported");
YYABORT;
}
| MAPNULL
{
type = master_strdup($1 + 1);
if (!type) {
local_free_vars();
YYABORT;
}
}
| dnattrs
{
type = master_strdup("ldap");
if (!type) {
local_free_vars();
YYABORT;
}
tmp_argc++;
tmp_argv = add_argv(tmp_argc, tmp_argv, $1);
if (!tmp_argv) {
master_error("memory allocation error");
local_free_vars();
YYABORT;
}
}
| MAPTYPE PATH
{
char *tmp = NULL;
trim_maptype($1);
if ((tmp = strchr($1, ',')))
*tmp++ = '\0';
type = master_strdup($1);
if (!type) {
master_error("memory allocation error");
local_free_vars();
YYABORT;
}
if (tmp) {
format = master_strdup(tmp);
if (!format) {
master_error("memory allocation error");
local_free_vars();
YYABORT;
}
}
tmp_argc++;
tmp_argv = add_argv(tmp_argc, tmp_argv, $2);
if (!tmp_argv) {
master_error("memory allocation error");
local_free_vars();
YYABORT;
}
}
| MAPTYPE MAPNAME
{
char *tmp = NULL;
trim_maptype($1);
if ((tmp = strchr($1, ',')))
*tmp++ = '\0';
type = master_strdup($1);
if (!type) {
master_error("memory allocation error");
local_free_vars();
YYABORT;
}
if (tmp) {
format = master_strdup(tmp);
if (!format) {
master_error("memory allocation error");
local_free_vars();
YYABORT;
}
}
tmp_argc++;
tmp_argv = add_argv(tmp_argc, tmp_argv, $2);
if (!tmp_argv) {
master_error("memory allocation error");
local_free_vars();
YYABORT;
}
}
| MAPTYPE dn
{
char *tmp = NULL;
trim_maptype($1);
if ((tmp = strchr($1, ',')))
*tmp++ = '\0';
type = master_strdup($1);
if (!type) {
master_error("memory allocation error");
local_free_vars();
YYABORT;
}
if (tmp) {
format = master_strdup(tmp);
if (!format) {
master_error("memory allocation error");
local_free_vars();
YYABORT;
}
}
tmp_argc++;
tmp_argv = add_argv(tmp_argc, tmp_argv, $2);
if (!tmp_argv) {
master_error("memory allocation error");
local_free_vars();
YYABORT;
}
/* Add back the type for lookup_ldap.c to handle ldaps */
if (*tmp_argv[0]) {
tmp = malloc(strlen(type) + strlen(tmp_argv[0]) + 2);
if (!tmp) {
master_error("memory allocation error");
local_free_vars();
YYABORT;
}
strcpy(tmp, type);
strcat(tmp, ":");
strcat(tmp, tmp_argv[0]);
free(tmp_argv[0]);
tmp_argv[0] = tmp;
}
}
;
dn: DNSERVER dnattrs
{
strcpy($$, $1);
strcat($$, $2);
}
| dnattrs
{
strcpy($$, $1);
}
|
{
master_notify("syntax error in dn");
YYABORT;
}
;
dnattrs: DNATTR EQUAL DNNAME
{
if (strcasecmp($1, "cn") &&
strcasecmp($1, "ou") &&
strcasecmp($1, "automountMapName") &&
strcasecmp($1, "nisMapName")) {
strcpy(errstr, $1);
strcat(errstr, "=");
strcat(errstr, $3);
master_notify(errstr);
YYABORT;
}
strcpy($$, $1);
strcat($$, "=");
strcat($$, $3);
}
| DNATTR EQUAL DNNAME COMMA dnattr
{
if (strcasecmp($1, "cn") &&
strcasecmp($1, "ou") &&
strcasecmp($1, "automountMapName") &&
strcasecmp($1, "nisMapName")) {
strcpy(errstr, $1);
strcat(errstr, "=");
strcat(errstr, $3);
master_notify(errstr);
YYABORT;
}
strcpy($$, $1);
strcat($$, "=");
strcat($$, $3);
strcat($$, ",");
strcat($$, $5);
}
| DNNAME
{
/* Matches map in old style syntax ldap:server:map */
strcpy($$, $1);
}
| DNATTR
{
master_notify($1);
YYABORT;
}
;
dnattr: DNATTR EQUAL DNNAME
{
if (!strcasecmp($1, "automountMapName") ||
!strcasecmp($1, "nisMapName")) {
strcpy(errstr, $1);
strcat(errstr, "=");
strcat(errstr, $3);
master_notify(errstr);
YYABORT;
}
strcpy($$, $1);
strcat($$, "=");
strcat($$, $3);
}
| DNATTR EQUAL DNNAME COMMA dnattr
{
if (!strcasecmp($1, "automountMapName") ||
!strcasecmp($1, "nisMapName")) {
strcpy(errstr, $1);
strcat(errstr, "=");
strcat(errstr, $3);
master_notify(errstr);
YYABORT;
}
strcpy($$, $1);
strcat($$, "=");
strcat($$, $3);
strcat($$, ",");
strcat($$, $5);
}
| DNATTR
{
master_notify($1);
YYABORT;
}
| DNNAME
{
master_notify($1);
YYABORT;
}
;
options: option {}
| options COMMA option {}
| options option {}
| options COMMA COMMA option
{
master_notify($1);
YYABORT;
}
| options EQUAL
{
master_notify($1);
YYABORT;
}
;
option: daemon_option
| mount_option {}
| error
{
master_notify("bogus option");
YYABORT;
}
;
daemon_option: OPT_TIMEOUT NUMBER { timeout = $2; }
| OPT_NTIMEOUT NUMBER { negative_timeout = $2; }
| OPT_NOBIND { nobind = 1; }
| OPT_NOGHOST { ghost = 0; }
| OPT_GHOST { ghost = 1; }
| OPT_VERBOSE { verbose = 1; }
| OPT_DEBUG { debug = 1; }
| OPT_RANDOM { random_selection = 1; }
| OPT_USE_WEIGHT { use_weight = 1; }
;
mount_option: OPTION
{
tmp_argc++;
tmp_argv = add_argv(tmp_argc, tmp_argv, $1);
if (!tmp_argv) {
master_error("memory allocation error");
local_free_vars();
YYABORT;
}
}
;
%%
#if YYDEBUG
static int master_fprintf(FILE *f, char *msg, ...)
{
va_list ap;
va_start(ap, msg);
vsyslog(LOG_DEBUG, msg, ap);
va_end(ap);
return 1;
}
#endif
static char *master_strdup(char *str)
{
char *tmp;
tmp = strdup(str);
if (!tmp)
master_error("memory allocation error");
return tmp;
}
static int master_error(const char *s)
{
logmsg("%s while parsing map.", s);
return 0;
}
static int master_notify(const char *s)
{
logmsg("syntax error in map near [ %s ]", s);
return(0);
}
static int master_msg(const char *s)
{
logmsg("%s", s);
return 0;
}
static void local_init_vars(void)
{
path = NULL;
type = NULL;
format = NULL;
verbose = 0;
debug = 0;
timeout = -1;
negative_timeout = 0;
nobind = 0;
ghost = defaults_get_browse_mode();
random_selection = global_selection_options & MOUNT_FLAG_RANDOM_SELECT;
use_weight = 0;
tmp_argv = NULL;
tmp_argc = 0;
local_argv = NULL;
local_argc = 0;
}
static void local_free_vars(void)
{
if (path)
free(path);
if (type)
free(type);
if (format)
free(format);
if (local_argv) {
free_argv(local_argc, (const char **) local_argv);
local_argv = NULL;
local_argc = 0;
}
if (tmp_argv) {
free_argv(tmp_argc, (const char **) tmp_argv);
tmp_argv = NULL;
tmp_argc = 0;
}
}
static void trim_maptype(char *type)
{
char *tmp;
tmp = strchr(type, ':');
if (tmp)
*tmp = '\0';
else {
int len = strlen(type);
while (len-- && isblank(type[len]))
type[len] = '\0';
}
return;
}
static int add_multi_mapstr(void)
{
if (type) {
/* If type given and format is non-null add it back */
if (format) {
int len = strlen(type) + strlen(format) + 2;
char *tmp = realloc(type, len);
if (!tmp)
return 0;
type = tmp;
strcat(type, ",");
strcat(type, format);
free(format);
format = NULL;
}
local_argc++;
local_argv = add_argv(local_argc, local_argv, type);
if (!local_argv) {
free(type);
type = NULL;
return 0;
}
free(type);
type = NULL;
}
local_argv = append_argv(local_argc, local_argv, tmp_argc, tmp_argv);
if (!local_argv) {
free(type);
type = NULL;
return 0;
}
local_argc += tmp_argc;
tmp_argc = 0;
tmp_argv = NULL;
return 1;
}
void master_init_scan(void)
{
lineno = 0;
}
int master_parse_entry(const char *buffer, unsigned int default_timeout, unsigned int logging, time_t age)
{
struct master *master = master_list;
struct mapent_cache *nc;
struct master_mapent *entry, *new;
struct map_source *source;
unsigned int logopt = logging;
unsigned int m_logopt = master->logopt;
int ret;
local_init_vars();
lineno++;
master_set_scan_buffer(buffer);
ret = master_parse();
if (ret != 0) {
local_free_vars();
return 0;
}
nc = master->nc;
/* Add null map entries to the null map cache */
if (type && !strcmp(type, "null")) {
cache_update(nc, NULL, path, NULL, lineno);
local_free_vars();
return 1;
}
/* Ignore all subsequent matching nulled entries */
if (cache_lookup_distinct(nc, path)) {
local_free_vars();
return 1;
}
if (debug || verbose) {
logopt = (debug ? LOGOPT_DEBUG : 0);
logopt |= (verbose ? LOGOPT_VERBOSE : 0);
}
new = NULL;
entry = master_find_mapent(master, path);
if (!entry) {
new = master_new_mapent(master, path, age);
if (!new) {
local_free_vars();
return 0;
}
entry = new;
} else {
if (entry->age && entry->age == age) {
if (strcmp(path, "/-")) {
info(m_logopt,
"ignoring duplicate indirect mount %s",
path);
local_free_vars();
return 0;
}
}
}
if (timeout < 0) {
/*
* If no timeout is given get the timout from first
* map (if it exists).
*/
if (entry->maps)
timeout = entry->maps->exp_timeout;
else
timeout = default_timeout;
}
if (!entry->ap) {
ret = master_add_autofs_point(entry, logopt, nobind, ghost, 0);
if (!ret) {
error(m_logopt, "failed to add autofs_point");
if (new)
master_free_mapent(new);
local_free_vars();
return 0;
}
}
if (random_selection)
entry->ap->flags |= MOUNT_FLAG_RANDOM_SELECT;
if (use_weight)
entry->ap->flags |= MOUNT_FLAG_USE_WEIGHT_ONLY;
if (negative_timeout)
entry->ap->negative_timeout = negative_timeout;
/*
source = master_find_map_source(entry, type, format,
local_argc, (const char **) local_argv);
if (!source)
source = master_add_map_source(entry, type, format, age,
local_argc, (const char **) local_argv);
else
source->age = age;
*/
source = master_add_map_source(entry, type, format, age,
local_argc, (const char **) local_argv);
if (!source) {
error(m_logopt, "failed to add source");
if (new)
master_free_mapent(new);
local_free_vars();
return 0;
}
source->exp_timeout = timeout;
source->master_line = lineno;
entry->age = age;
entry->current = NULL;
if (new)
master_add_mapent(master, entry);
local_free_vars();
return 1;
}
|