-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.y
397 lines (352 loc) · 8.69 KB
/
parse.y
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
%{
#include <stdio.h>
#include <grp.h>
#include "doinkd.h"
#include <pwd.h>
#ifndef DEBUG
#define DEBUG 0
#endif
#define debug if (DEBUG > 0) logfile
int num;
char *name;
struct group *grp;
extern char *yytext;
extern char *config_file; /* The name of the config file, from doinkd.c */
extern char *strchr();
extern void addlist();
/************************************************************************
* The order of the tokens in the *first line* is significant. *
* *
* They dictate which rules and exemptions have precedence. *
* Hence, TTY has precedence over HOST has precedence over LOGIN, etc. *
* *
* The second two %token lines may be ordered anyway. *
* DEFAULT is the least specific, but will always match. *
* It must always remain in the last position. *
************************************************************************/
%}
%token TTY HOST LOGIN GROUP FILECOM DEFAULT
%token EXEMPT TIMEOUT SLEEP WARN IDLEMETHOD CONSWINS SESSION REFUSE MULTIPLES MAXUSER
%token NUM IDLE MULTIPLE NAME ALL
%token THRESHOLD NL
%token USERINPUT INPUTOUTPUT
%token NORMAL OFF
%union {
char *sb;
int nb;
}
%type <sb> NAME
%type <nb> NUM LOGIN GROUP TTY ALL IDLE MULTIPLE
%type <nb> who exempt_type name_type
%start cmd_cmd
%%
cmd_cmd : /*EMPTY*/
| cmd_cmd exempt_cmd
| cmd_cmd idle_cmd
| cmd_cmd refuse_cmd
| cmd_cmd sleep_cmd
| cmd_cmd warn_cmd
| cmd_cmd idlemethod_cmd
| cmd_cmd conswins_cmd
| cmd_cmd session_cmd
| cmd_cmd thresh_cmd
| cmd_cmd mult_cmd
| cmd_cmd maxuser_cmd
| cmd_cmd error NL
| cmd_cmd NL
;
thresh_cmd : THRESHOLD MULTIPLE NUM NL
{
m_threshold = $3;
}
| THRESHOLD SESSION NUM NL
{
s_threshold = $3;
}
| THRESHOLD error NL
{
yyerror("Malformed threshold command.");
}
;
exempt_cmd : EXEMPT who exempt_type NL
{
addlist(exmpt, $2, name, num, $3);
}
| EXEMPT FILECOM NAME exempt_type NL
{
filecom_parse(EXEMPT,$3,$4);
}
| EXEMPT error NL
{
yyerror("Malformed exempt command.");
}
;
refuse_cmd : REFUSE who NL
{
addlist(refuse, $2, name, num, 0);
}
| REFUSE FILECOM NAME NL
{
filecom_parse(REFUSE,$3,/* time or thing if any */0);
}
| REFUSE error NL
{
yyerror("Malformed refuse command.");
}
;
session_cmd : SESSION who NUM NL
{
addlist(session, $2, name, num, $3);
}
| SESSION FILECOM NAME NUM NL
{
filecom_parse(SESSION,$3,$4);
}
| SESSION DEFAULT NUM NL
{
session_default = $3;
}
| SESSION REFUSE NUM NL
{
sess_refuse_len = $3 * 60;
}
| SESSION error NL
{
yyerror("Malformed session command.");
}
;
idle_cmd : TIMEOUT who NUM NL
{
addlist(rules, $2, name, num, $3);
}
| TIMEOUT FILECOM NAME NUM NL
{
filecom_parse(TIMEOUT,$3,$4);
}
| TIMEOUT DEFAULT NUM NL
{
addlist(rules, DEFAULT, NULL, 0, $3);
}
| TIMEOUT error NL
{
yyerror("Malformed timeout command.");
}
;
sleep_cmd : SLEEP NUM NL
{
sleeptime = $2;
}
| SLEEP error NL
{
yyerror("Malformed sleep command.");
}
;
warn_cmd : WARN NUM NL
{
warntime = $2;
}
| WARN error NL
{
yyerror("Malformed warn command.");
}
;
idlemethod_cmd : IDLEMETHOD USERINPUT NL
{
ioidle = FALSE;
}
| IDLEMETHOD INPUTOUTPUT NL
{
ioidle = TRUE;
}
| IDLEMETHOD error NL
{
yyerror("Malformed idlemethod command.");
}
;
conswins_cmd : CONSWINS IDLE NUM NL
{
conswins_idle = $3 * 60;
}
| CONSWINS IDLE NORMAL NL
{
conswins_idle = -2;
}
| CONSWINS IDLE OFF NL
{
conswins_idle = -1;
}
| CONSWINS SESSION NUM NL
{
conswins_sess = $3 * 60;
}
| CONSWINS SESSION NORMAL NL
{
conswins_sess = -2;
}
| CONSWINS SESSION OFF NL
{
conswins_sess = -1;
}
| CONSWINS MULTIPLE NUM NL
{
conswins_mult = $3;
}
| CONSWINS MULTIPLE NORMAL NL
{
conswins_mult = -2;
}
| CONSWINS MULTIPLE OFF NL
{
conswins_mult = -1;
}
| CONSWINS error NL
{
yyerror("Malformed cons(ole) win(dow)s command.");
}
;
mult_cmd : MULTIPLES NUM NL
{
mult_per_user = $2;
}
| MULTIPLES error NL
{
yyerror("Malformed multiples command.");
}
;
maxuser_cmd : MAXUSER who NUM NL
{
addlist(maxuser, $2, name, num, $3);
}
| MAXUSER FILECOM NAME NUM NL
{
filecom_parse(MAXUSER,$3,$4);
}
| MAXUSER error NL
{
yyerror("Malformed maxuser command.");
}
;
who : name_type NAME
{
$$ = $1;
name = $2;
if ($1 == GROUP)
{
grp = getgrnam(name);
if (grp != NULL)
num = grp->gr_gid;
else
logfile("Error parsing conf file: unknown group name '%s'.",name);
}
if ($1 == LOGIN)
{
if (getpwnam(name) == NULL)
logfile("Warning parsing conf file: unknown login name '%s'.",name);
}
}
;
name_type : LOGIN { $$ = LOGIN; }
| HOST { $$ = HOST; }
| GROUP { $$ = GROUP; }
| TTY { $$ = TTY; }
;
exempt_type : ALL { $$ = ALL; }
| IDLE { $$ = IDLE; }
| MULTIPLE { $$ = MULTIPLE; }
| REFUSE { $$ = REFUSE; }
| SESSION { $$ = SESSION; }
;
%%
static int errorcnt = 0;
int
yyerror(sb)
char *sb;
{
extern int linenum;
logfile("%s: line %d: %s", config_file, linenum, sb);
errorcnt++;
return 0;
}
int
yywrap()
{
extern int linenum;
extern time_t conf_oldstamp;
if ( errorcnt > 0 && conf_oldstamp <= 1 )
{
logfile("Aborting due to conf file syntax errors.");
exit(1);
}
linenum = 1;
return 1;
}
#ifndef HAVE_YYRESTART
void yyrestart(handle)
FILE *handle;
{
extern int linenum;
linenum = 1;
}
#endif
/**************************************************************************
* Reads in more rules from a separate file, which contains the *
* login names of the users for that type, one per line. *
**************************************************************************/
void filecom_parse(type,filename,param)
int type; /* REFUSE, SESSION, TIMEOUT, or EXEMPT */
char *filename;
int param; /* idle/session time or exempt type */
{
FILE *handle;
handle = fopen(filename,"r");
if (handle == NULL)
{
char *buffer;
buffer = (char *) malloc(20+sizeof(filename));
sprintf(buffer,"Could not open file '%s'",filename);
yyerror(buffer);
free(buffer);
}
else
{
char lname[NAMELEN+1], trash[100], *c;
while (!feof(handle) && (fgets(lname,NAMELEN+1,handle) != NULL))
{
/* If we didn't read in the newline, do so now */
if (strchr(lname,'\n') == NULL)
fscanf(handle,"%[^\n]\n",trash);
/* First, strip away beyond the first space or newline */
c = strchr(lname,' '); if (c != NULL) *c = '\0';
c = strchr(lname,'\n'); if (c != NULL) *c = '\0';
if ((int)strlen(lname) > 0)
{
char *username;
username = (char *) malloc (strlen(lname)+1);
strcpy(username,lname);
switch(type)
{
case REFUSE:
debug("Refusing user %s.",username);
addlist(refuse, LOGIN, username, 0, 0);
break;
case MAXUSER:
debug("MaxUser Limiting group %s to %d logins.",username,param);
addlist(maxuser, GROUP, username, 0, param);
break;
case SESSION:
debug("Session Limiting user %s to %d minutes.",username,param);
addlist(session, LOGIN, username, 0, param);
break;
case TIMEOUT:
debug("Setting Idle timeout for user %s to %d minutes.",username,param);
addlist(rules, LOGIN, username, 0, param);
break;
case EXEMPT:
debug("Exempting user %s from type %d.",username,param);
addlist(exmpt, LOGIN, username, 0, param);
break;
}
}
}
}
}