2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-22 09:57:41 +00:00
sudo/parse.lex

353 lines
7.6 KiB
Plaintext
Raw Normal View History

1993-03-02 16:33:11 +00:00
%{
/*
1999-01-17 22:40:55 +00:00
* CU sudo version 1.5.8
1999-02-03 04:32:19 +00:00
* Copyright (c) 1996, 1998, 1999 Todd C. Miller <Todd.Miller@courtesan.com>
1993-03-02 16:33:11 +00:00
*
* 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 1, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*
1996-09-08 00:21:42 +00:00
* Please send bugs, changes, problems to sudo-bugs@courtesan.com
1995-03-29 04:12:17 +00:00
*
*******************************************************************
*
* parse.lex -- lexigraphical analyzer for sudo.
*
* Chris Jepeway <jepeway@cs.utk.edu>
1993-03-02 16:33:11 +00:00
*/
1994-05-28 17:21:11 +00:00
#include "config.h"
#ifdef STDC_HEADERS
#include <stdlib.h>
#endif /* STDC_HEADERS */
1995-03-24 03:32:53 +00:00
#ifdef HAVE_UNISTD_H
1994-08-30 22:29:48 +00:00
#include <unistd.h>
1995-03-24 03:32:53 +00:00
#endif /* HAVE_UNISTD_H */
#ifdef HAVE_STRING_H
#include <string.h>
#endif /* HAVE_STRING_H */
#if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
#include <malloc.h>
#endif /* HAVE_MALLOC_H && !STDC_HEADERS */
1995-03-24 03:32:53 +00:00
#include <ctype.h>
1993-03-02 16:33:11 +00:00
#include <sys/types.h>
#include <sys/param.h>
1994-08-12 02:00:39 +00:00
#include <netinet/in.h>
1993-03-02 16:33:11 +00:00
#include "sudo.h"
#include "sudo.tab.h"
1993-10-18 00:31:49 +00:00
1998-11-18 04:16:13 +00:00
#ifndef lint
1999-01-17 23:16:20 +00:00
static const char rcsid[] = "$Sudo$";
1998-11-18 04:16:13 +00:00
#endif /* lint */
#undef yywrap /* guard against a yywrap macro */
1994-03-12 22:42:16 +00:00
extern YYSTYPE yylval;
1995-05-02 03:32:50 +00:00
extern int clearaliases;
1995-03-24 03:32:53 +00:00
int sudolineno = 1;
static int sawspace = 0;
1996-07-22 19:30:42 +00:00
static int arg_len = 0;
static int arg_size = 0;
1995-03-24 03:32:53 +00:00
static void fill __P((char *, int));
static void fill_cmnd __P((char *, int));
static void fill_args __P((char *, int, int));
1995-08-14 04:07:15 +00:00
extern void reset_aliases __P((void));
1995-09-23 20:48:04 +00:00
extern void yyerror __P((char *));
1995-03-24 03:32:53 +00:00
/* realloc() to size + COMMANDARGINC to make room for command args */
#define COMMANDARGINC 64
1995-03-24 03:32:53 +00:00
#ifdef TRACELEXER
#define LEXTRACE(msg) fputs(msg, stderr)
#else
#define LEXTRACE(msg)
#endif
1993-03-02 16:33:11 +00:00
%}
OCTET [[:digit:]]{1,3}
1996-06-17 04:02:33 +00:00
DOTTEDQUAD {OCTET}(\.{OCTET}){3}
WORD [[:alnum:]_-]+
1995-03-24 03:32:53 +00:00
%e 4000
%p 6000
%k 3500
1995-08-14 04:07:15 +00:00
%s GOTCMND
%s GOTRUNAS
1995-08-14 04:07:15 +00:00
1993-03-02 16:33:11 +00:00
%%
[ \t]+ { /* throw away space/tabs */
sawspace = TRUE; /* but remember for fill_args */
}
1995-08-14 04:07:15 +00:00
\\[ \t]*\n {
sawspace = TRUE; /* remember for fill_args */
++sudolineno;
LEXTRACE("\n\t");
1995-03-24 03:32:53 +00:00
} /* throw away EOL after \ */
1995-08-14 04:07:15 +00:00
1996-07-24 16:49:43 +00:00
<GOTCMND>\\[:\,=\\ \t] {
LEXTRACE("QUOTEDCHAR ");
fill_args(yytext + 1, 1, sawspace);
sawspace = FALSE;
}
<GOTCMND>[:\,=\n] {
1996-04-28 19:01:02 +00:00
BEGIN INITIAL;
unput(*yytext);
return(COMMAND);
} /* end of command line args */
1995-03-24 03:32:53 +00:00
\n {
++sudolineno;
LEXTRACE("\n");
return(COMMENT);
1995-03-24 03:32:53 +00:00
} /* return newline */
1995-08-14 04:07:15 +00:00
1996-04-28 19:01:02 +00:00
<INITIAL>#.*\n {
++sudolineno;
LEXTRACE("\n");
return(COMMENT);
1995-03-24 03:32:53 +00:00
} /* return comments */
1995-07-25 00:01:34 +00:00
<GOTCMND>[^\\:, \t\n]+ {
LEXTRACE("ARG ");
fill_args(yytext, yyleng, sawspace);
sawspace = FALSE;
} /* a command line arg */
1995-08-14 04:07:15 +00:00
\, {
LEXTRACE(", ");
return(',');
1995-08-14 04:07:15 +00:00
} /* return ',' */
\! {
return('!'); /* return '!' */
}
1995-08-14 04:07:15 +00:00
= {
LEXTRACE("= ");
return('=');
1995-08-14 04:07:15 +00:00
} /* return '=' */
: {
LEXTRACE(": ");
return(':');
1995-08-14 04:07:15 +00:00
} /* return ':' */
\. {
return('.');
}
1995-08-14 04:07:15 +00:00
NOPASSWD[[:blank:]]*: {
/* cmnd does not require passwd for this user */
LEXTRACE("NOPASSWD ");
return(NOPASSWD);
}
1996-04-28 19:01:02 +00:00
\+{WORD} {
/* netgroup */
fill(yytext, yyleng);
return(NETGROUP);
1996-04-28 19:01:02 +00:00
}
1995-07-25 00:01:34 +00:00
1996-04-28 19:01:02 +00:00
\%{WORD} {
/* UN*X group */
1995-12-18 02:51:30 +00:00
fill(yytext, yyleng);
return(USERGROUP);
1996-04-28 19:01:02 +00:00
}
1995-12-18 02:51:30 +00:00
1996-06-17 04:02:33 +00:00
{DOTTEDQUAD}(\/{DOTTEDQUAD})? {
fill(yytext, yyleng);
1996-03-19 22:00:22 +00:00
LEXTRACE("NTWKADDR ");
return(NTWKADDR);
}
1995-03-24 03:32:53 +00:00
[[:alpha:]][[:alnum:]_-]*(\.{WORD})+ {
1996-03-19 22:00:22 +00:00
fill(yytext, yyleng);
LEXTRACE("FQHOST ");
return(FQHOST);
}
<INITIAL>\( {
BEGIN GOTRUNAS;
LEXTRACE("RUNAS ");
return (RUNAS);
}
<GOTRUNAS>[[:upper:]][[:upper:][:digit:]_]* {
1996-11-11 01:02:32 +00:00
/* Runas_Alias that user can run command as or ALL */
fill(yytext, yyleng);
if (strcmp(yytext, "ALL") == 0) {
LEXTRACE("ALL ");
return(ALL);
} else {
LEXTRACE("ALIAS ");
return(ALIAS);
}
}
1996-04-28 19:01:02 +00:00
<GOTRUNAS>#?{WORD} {
/* username/uid that user can run command as */
fill(yytext, yyleng);
LEXTRACE("NAME ");
return(NAME);
}
<GOTRUNAS>\) BEGIN INITIAL;
\/[^\,:=\\ \t\n#]+ {
/* directories can't have args... */
if (yytext[yyleng - 1] == '/') {
LEXTRACE("COMMAND ");
fill_cmnd(yytext, yyleng);
return(COMMAND);
} else {
BEGIN GOTCMND;
LEXTRACE("COMMAND ");
fill_cmnd(yytext, yyleng);
}
1995-03-24 03:32:53 +00:00
} /* a pathname */
[[:upper:]][[:upper:][:digit:]_]* {
fill(yytext, yyleng);
if (strcmp(yytext, "ALL") == 0) {
LEXTRACE("ALL ");
return(ALL);
}
LEXTRACE("ALIAS ");
return(ALIAS);
1995-03-24 03:32:53 +00:00
}
[[:alnum:]][[:alnum:]_-]* {
fill(yytext, yyleng);
if (strcmp(yytext, "Host_Alias") == 0) {
LEXTRACE("HOSTALIAS ");
return(HOSTALIAS);
}
if (strcmp(yytext, "Cmnd_Alias") == 0) {
LEXTRACE("CMNDALIAS ");
return(CMNDALIAS);
}
if (strcmp(yytext, "User_Alias") == 0) {
LEXTRACE("USERALIAS ");
return(USERALIAS);
}
1996-11-11 01:02:32 +00:00
if (strcmp(yytext, "Runas_Alias") == 0) {
LEXTRACE("RUNASALIAS ");
return(RUNASALIAS);
}
1996-11-11 01:02:32 +00:00
/* NAME is what RFC1034 calls a label */
LEXTRACE("NAME ");
return(NAME);
1995-03-24 03:32:53 +00:00
}
. {
return(ERROR);
} /* parse error */
1993-03-02 16:33:11 +00:00
%%
static void fill(s, len)
char *s;
int len;
{
yylval.string = (char *) malloc(len + 1);
if (yylval.string == NULL)
yyerror("unable to allocate memory");
/* copy the string and NULL-terminate it */
(void) strncpy(yylval.string, s, len);
yylval.string[len] = '\0';
1993-03-02 16:33:11 +00:00
}
static void fill_cmnd(s, len)
char *s;
int len;
{
1996-07-22 19:30:42 +00:00
arg_len = arg_size = 0;
yylval.command.cmnd = (char *) malloc(len + 1);
if (yylval.command.cmnd == NULL)
yyerror("unable to allocate memory");
/* copy the string and NULL-terminate it */
(void) strncpy(yylval.command.cmnd, s, len);
yylval.command.cmnd[len] = '\0';
yylval.command.args = NULL;
}
1996-07-22 19:30:42 +00:00
static void fill_args(s, len, addspace)
char *s;
int len;
1996-07-22 19:30:42 +00:00
int addspace;
{
int new_len;
1996-07-22 19:30:42 +00:00
char *p;
/*
* If first arg, malloc() some room, else if we don't
* have enough space realloc() some more.
1996-07-22 19:30:42 +00:00
*/
if (yylval.command.args == NULL) {
addspace = 0;
new_len = len;
1996-07-22 19:30:42 +00:00
while (new_len >= (arg_size += COMMANDARGINC))
;
yylval.command.args = (char *) malloc(arg_size);
if (yylval.command.args == NULL)
yyerror("unable to allocate memory");
} else {
new_len = arg_len + len + addspace;
if (new_len >= arg_size) {
/* Allocate more space than we need for subsequent args */
while (new_len >= (arg_size += COMMANDARGINC))
;
yylval.command.args = (char *) realloc(yylval.command.args, arg_size);
if (yylval.command.args == NULL)
yyerror("unable to allocate memory");
}
}
/* Efficiently append the arg (with a leading space if needed). */
1996-07-22 19:30:42 +00:00
p = yylval.command.args + arg_len;
if (addspace)
*p++ = ' ';
(void) strcpy(p, s);
arg_len = new_len;
1995-08-14 04:07:15 +00:00
}
int yywrap()
{
#ifdef YY_NEW_FILE
YY_NEW_FILE;
#endif /* YY_NEW_FILE */
1995-05-02 03:32:50 +00:00
/* don't reset the aliases if called by testsudoers */
if (clearaliases)
reset_aliases();
1995-09-01 05:13:23 +00:00
return(TRUE);
}