summaryrefslogtreecommitdiff
path: root/logging.c
blob: e5bb082aa42d5bda48147b6679d7ad3ad0845f52 (plain)
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
/*
 * CU sudo version 1.3.1 (based on Root Group sudo version 1.1)
 *
 * This software comes with no waranty whatsoever, use at your own risk.
 *
 * Please send bugs, changes, problems to sudo-bugs@cs.colorado.edu
 *
 */

/*
 *  sudo version 1.1 allows users to execute commands as root
 *  Copyright (C) 1991  The Root Group, Inc.
 *
 *  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.
 *
 ****************************************************************
 *
 *  logging.c
 *
 *  this file supports the general logging facilities
 *  if you want to change any error messages, this is probably
 *  the place to be...
 *
 *  Jeff Nieusma   Thu Mar 21 23:39:04 MST 1991
 */

#ifndef lint
static char rcsid[] = "$Id$";
#endif /* lint */

#include "config.h"

#include <stdio.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#ifdef HAVE_STRING_H
#include <string.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif /* HAVE_STRINGS_H */
#include <signal.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <netinet/in.h>

#include "sudo.h"
#include "options.h"

/*
 * Prototypes for local functions
 */
static void send_mail		__P((void));
static RETSIGTYPE reapchild	__P((int));
static int appropriate		__P((int));

/*
 * Globals
 */
static char logline[MAXLOGLEN + 8];
extern int errorlineno;


/**********************************************************************
 *
 *  log_error()
 *
 *  This function attempts to deliver mail to ALERTMAIL and either
 *  syslogs the error or writes it to the log file
 */

void log_error(code)
    int code;
{
    int argc;
    char **argv;
    mode_t oldmask;
    register char *p;
    register int count;
    time_t now;
#if (LOGGING & SLOG_FILE)
    register FILE *fp;
#endif /* LOGGING & SLOG_FILE */
#if (LOGGING & SLOG_SYSLOG)
    register int pri;		/* syslog priority */
    char *tmp, save;
#endif /* LOGGING & SLOG_SYSLOG */

    /*
     * we will skip this stuff when using syslog(3) but it is
     * necesary for mail and file logs.
     */
    now = time((time_t) 0);
    (void) sprintf(logline, "%19.19s : %8.8s : ", ctime(&now), user);

    /*
     * we need a pointer to the end of logline
     */
    p = logline + strlen(logline);

    switch (code) {

	case ALL_SYSTEMS_GO:
	    (void) sprintf(p, "PWD=%s ; COMMAND=", cwd);
#if (LOGGING & SLOG_SYSLOG)
	    pri = Syslog_priority_OK;
#endif /* LOGGING & SLOG_SYSLOG */
	    break;

	case VALIDATE_NO_USER:
	    (void) sprintf(p, "user NOT in sudoers ; PWD=%s ; COMMAND=", cwd);
#if (LOGGING & SLOG_SYSLOG)
	    pri = Syslog_priority_NO;
#endif /* LOGGING & SLOG_SYSLOG */
	    break;

	case VALIDATE_NOT_OK:
	    (void) sprintf(p, "command not allowed ; PWD=%s ; COMMAND=", cwd);
#if (LOGGING & SLOG_SYSLOG)
	    pri = Syslog_priority_NO;
#endif /* LOGGING & SLOG_SYSLOG */
	    break;

	case VALIDATE_ERROR:
	    (void) sprintf(p, "error in %s, line %d ; PWD=%s.  ",
		_PATH_SUDO_SUDOERS, errorlineno, cwd);
#if (LOGGING & SLOG_SYSLOG)
	    pri = Syslog_priority_NO;
#endif /* LOGGING & SLOG_SYSLOG */
	    break;

	case GLOBAL_NO_PW_ENT:
	    (void) sprintf(p, "There is no /etc/passwd entry for uid %d.  ",
		uid);
#if (LOGGING & SLOG_SYSLOG)
	    pri = Syslog_priority_NO;
#endif /* LOGGING & SLOG_SYSLOG */
	    break;

	case PASSWORD_NOT_CORRECT:
	    (void) sprintf(p, "%d incorrect passwords ; PWD=%s ; COMMAND=",
		    TRIES_FOR_PASSWORD, cwd);
#if (LOGGING & SLOG_SYSLOG)
	    pri = Syslog_priority_NO;
#endif /* LOGGING & SLOG_SYSLOG */
	    break;

	case GLOBAL_NO_HOSTNAME:
	    strcat(p, "This machine does not have a hostname ");
#if (LOGGING & SLOG_SYSLOG)
	    pri = Syslog_priority_NO;
#endif /* LOGGING & SLOG_SYSLOG */
	    break;

	case NO_SUDOERS_FILE:
	    switch (errno) {
		case ENOENT:
		    (void) sprintf(p, "There is no %s file.  ",
			_PATH_SUDO_SUDOERS);
		    break;
		case EACCES:
		    (void) sprintf(p, "%s needs to run setuid root.  ",
			Argv[0]);
		    break;
		default:
		    (void) sprintf(p, "There is a problem opening %s ",
			_PATH_SUDO_SUDOERS);
		    break;
	    }
#if (LOGGING & SLOG_SYSLOG)
	    pri = Syslog_priority_NO;
#endif /* LOGGING & SLOG_SYSLOG */
	    break;

	case GLOBAL_HOST_UNREGISTERED:
	    (void) sprintf(p, "gethostbyname() cannot find host %s ", host);
#if (LOGGING & SLOG_SYSLOG)
	    pri = Syslog_priority_NO;
#endif /* LOGGING & SLOG_SYSLOG */
	    break;

	case SUDOERS_NO_OWNER:
	    (void) sprintf(p, "no passwd entry for sudoers file owner (%s) ",
		SUDOERS_OWNER);
#if (LOGGING & SLOG_SYSLOG)
	    pri = Syslog_priority_NO;
#endif /* LOGGING & SLOG_SYSLOG */
	    break;

	case SUDOERS_NOT_FILE:
	    (void) sprintf(p, "%s is not a regular file ", _PATH_SUDO_SUDOERS);
#if (LOGGING & SLOG_SYSLOG)
	    pri = Syslog_priority_NO;
#endif /* LOGGING & SLOG_SYSLOG */
	    break;

	case SUDOERS_WRONG_OWNER:
	    (void) sprintf(p, "%s is not owned by %s ", _PATH_SUDO_SUDOERS,
		SUDOERS_OWNER);
#if (LOGGING & SLOG_SYSLOG)
	    pri = Syslog_priority_NO;
#endif /* LOGGING & SLOG_SYSLOG */
	    break;

	case SUDOERS_RW_OTHER:
	    (void) sprintf(p, "%s is readable or writeable by other than %s ",
		_PATH_SUDO_SUDOERS, SUDOERS_OWNER);
#if (LOGGING & SLOG_SYSLOG)
	    pri = Syslog_priority_NO;
#endif /* LOGGING & SLOG_SYSLOG */
	    break;

	default:
	    strcat(p, "found a wierd error : ");
#if (LOGGING & SLOG_SYSLOG)
	    pri = Syslog_priority_NO;
#endif /* LOGGING & SLOG_SYSLOG */
	    break;
    }


    /*
     * If this is a parse error or if the error is from load_globals()
     * don't put  argv in the message.
     */
    if (code != VALIDATE_ERROR && !(code & GLOBAL_PROBLEM)) {

	strcat(logline, cmnd);	/* stuff the command into the logline */
	strcat(logline, " ");

	argc = Argc - 2;
	argv = Argv;
	argv++;
	p = logline + strlen(logline);
	count = (int) (logline + MAXLOGLEN - p);

	/*
	 * Now stuff as much of the rest of the line as will fit
	 * Do word wrap if logging to a file.
	 */
	while (count > 0 && argc--) {
	    strncpy(p, *(++argv), count);
	    strcat(p, " ");
	    p += 1 + (count < strlen(*argv) ? count : strlen(*argv));
	    count = (int) (logline + MAXLOGLEN - p);
	}
	if (count <= 0)		/* if the line is too long, */
	    strcat(p, " ... ");	/* add an elipsis to the end */

    }

#if (LOGGING & SLOG_SYSLOG)
#ifdef Syslog_facility
    openlog(Syslog_ident, Syslog_options, Syslog_facility);
#else
    openlog(Syslog_ident, Syslog_options);
#endif /* Syslog_facility */

    /*
     * Log the full line, breaking into multiple syslog(3) calls if necesary
     */
    p = &logline[33];			/* skip past the date and user */
    for (count=0; count < (strlen(logline) / MAXSYSLOGLEN) + 1; count++) {
	if (strlen(p) > MAXSYSLOGLEN) {
	    /*
	     * Break up the line into what will fit on one syslog(3) line
	     * Try to break on a word boundary if possible.
	     */
	    for (tmp = p + MAXSYSLOGLEN; tmp > p && *tmp != ' '; tmp--)
		;
	    if (tmp <= p)
		tmp = p + MAXSYSLOGLEN;

	    /* NULL terminate line, but save the char to restore later */
	    save = *tmp;
	    *tmp = '\0';

	    if (count == 0)
		syslog(pri, "%8.8s : %s", user, p);
	    else
		syslog(pri, "%8.8s : (command continued) %s", user, p);

	    *tmp = save;			/* restore saved character */

	    /* eliminate leading whitespace */
	    for (p=tmp; *p != ' '; p++)
		;
	} else {
	    if (count == 0)
		syslog(pri, "%8.8s : %s", user, p);
	    else
		syslog(pri, "%8.8s : (command continued) %s", user, p);
	}
    }
    closelog();
#endif /* LOGGING & SLOG_SYSLOG */
#if (LOGGING & SLOG_FILE)

    /* become root */
    set_perms(PERM_ROOT);

    oldmask = umask(077);
    fp = fopen(_PATH_SUDO_LOGFILE, "a");
    (void) umask(oldmask);
    if (fp == NULL) {
	(void) sprintf(logline, "Can\'t open log file: %s", _PATH_SUDO_LOGFILE);
	send_mail();
    } else {
	char *beg, *oldend, *end;
	register int maxlen = MAXLOGFILELEN;

	/*
	 * Print out logline with word wrap
	 */
	beg = end = logline;
	while (beg) {
	    oldend = end;
	    end = strchr(oldend, ' ');

	    if (end) {
		*end = '\0';
		if (strlen(beg) > maxlen) {
		    /* too far, need to back up & print the line */

		    if (beg == (char *)logline)
			maxlen -= 4;		/* don't indent first line */

		    *end = ' ';
		    if (oldend != beg) {
			/* rewind & print */
		    	end = oldend-1;
			while (*end == ' ')
			    --end;
			*(++end) = '\0';
			(void) fprintf(fp, "%s\n    ", beg);
			*end = ' ';
		    } else {
			(void) fprintf(fp, "%s\n    ", beg);
		    }

		    /* reset beg to point to the start of the new substring */
		    beg = end;
		    while (*beg == ' ')
			++beg;
		} else {
		    /* we still have room */
		    *end = ' ';
		}

		/* remove leading whitespace */
		while (*end == ' ')
		    ++end;
	    } else {
		/* final line */
		(void) fprintf(fp, "%s\n", beg);
		beg = NULL;			/* exit condition */
	    }
	}

	(void) fclose(fp);
    }

    /* relinquish root */
    set_perms(PERM_USER);
#endif /* LOGGING & SLOG_FILE */

    /* send mail if appropriate */
    if (appropriate(code))
	send_mail();
}



#ifdef MAILER
/**********************************************************************
 *
 *  send_mail()
 *
 *  This function attempts to mail to ALERTMAIL about the sudo error
 *
 */

static char *mail_argv[] = { "sendmail",
			     "-t",
			     ALERTMAIL,
			     (char *) NULL };

static void send_mail()
{
    char *mailer;
    char *subject = MAILSUBJECT;
    int fd[2];
    char buf[MAXLOGLEN + 1024];
#ifdef POSIX_SIGNALS
    struct sigaction action;

    (void) memset((VOID *)&action, 0, sizeof(action));
#endif /* POSIX_SIGNALS */

    if ((mailer = find_path(MAILER)) == NULL) {
	(void) fprintf(stderr, "%s: mailer (%s) not found\n", Argv[0], MAILER);
	exit(1);
    }

    /* catch children as they die */
#ifdef POSIX_SIGNALS
    action.sa_handler = reapchild;
    (void) sigaction(SIGCHLD, &action, NULL);
#else
    (void) signal(SIGCHLD, reapchild);
#endif /* POSIX_SIGNALS */

    if (fork())
	return;

    /*
     * we don't want any security problems ...
     */
    set_perms(PERM_FULL_USER);
    
#ifdef POSIX_SIGNALS
    action.sa_handler = SIG_IGN;
    (void) sigaction(SIGHUP, &action, NULL);
    (void) sigaction(SIGINT, &action, NULL);
    (void) sigaction(SIGQUIT, &action, NULL);
#else
    (void) signal(SIGHUP, SIG_IGN);
    (void) signal(SIGINT, SIG_IGN);
    (void) signal(SIGQUIT, SIG_IGN);
#endif /* POSIX_SIGNALS */

    if (pipe(fd)) {
	perror("send_mail: pipe");
	exit(1);
    }
    (void) dup2(fd[0], 0);
    (void) dup2(fd[1], 1);
    (void) close(fd[0]);
    (void) close(fd[1]);

    if (!fork()) {		/* child */
	(void) close(1);
	EXEC(mailer, mail_argv);

	/* this should not happen */
	perror(mailer);
	exit(1);
    } else {			/* parent */
	(void) close(0);

	/* feed the data to sendmail */
	(void) sprintf(buf, "To: %s\nSubject: %s\n\n%s : %s\n\n",
		ALERTMAIL, subject, host, logline);
	write(1, buf, strlen(buf));
	close(1);

	exit(0);
    }
}
#else
static void send_mail()
{
    /* no mailer defined */
    return;
}
#endif /* MAILER */



/****************************************************************
 *
 *  reapchild()
 *
 *  This function gets rid of all the ugly zombies
 */

static RETSIGTYPE reapchild(sig)
    int sig;
{
    (void) wait(NULL);
#ifndef POSIX_SIGNALS
    (void) signal(SIGCHLD, reapchild);
#endif /* POSIX_SIGNALS */
}



/**********************************************************************
 *
 *  inform_user ()
 *
 *  This function lets the user know what is happening 
 *  when an error occurs
 */

void inform_user(code)
    int code;
{

    switch (code) {
	case VALIDATE_NO_USER:
	    (void) fprintf(stderr,
		"%s is not in the sudoers file.  This incident will be reported.\n\n",
		user);
	    break;

	case VALIDATE_NOT_OK:
	    (void) fprintf(stderr,
		"Sorry, user %s is not allowed to execute %s on %s.\n\n",
		user, cmnd, host);
	    break;

	case VALIDATE_ERROR:
	    (void) fprintf(stderr,
		"Sorry, there is a fatal error in the sudoers file.\n\n");
	    break;

	case GLOBAL_NO_PW_ENT:
	    (void) fprintf(stderr,
		"Intruder Alert!  You don\'t exist in the passwd file\n\n");
	    break;

	case GLOBAL_NO_AUTH_ENT:
	    (void) fprintf(stderr,
		"Intruder Alert!  You don\'t exist in the auth database\n\n");
	    break;

	case GLOBAL_NO_HOSTNAME:
	    (void) fprintf(stderr,
		"This machine does not have a hostname\n\n");
	    break;

	case GLOBAL_HOST_UNREGISTERED:
	    (void) fprintf(stderr,
		"This machine is not available via gethostbyname()\n\n");
	    break;

	case PASSWORD_NOT_CORRECT:
	    (void) fprintf(stderr, "Password not entered correctly after %d tries\n\n",
		TRIES_FOR_PASSWORD);
	    break;

	case SUDOERS_NO_OWNER:
	    (void) fprintf(stderr,
		"No passwd entry for sudoers file owner (%s)\n", SUDOERS_OWNER);
	    break;

	case NO_SUDOERS_FILE:
	    (void) fprintf(stderr, "Can't stat %s: ", _PATH_SUDO_SUDOERS);
	    perror("");
	    break;

	case SUDOERS_NOT_FILE:
	    (void) fprintf(stderr,
		"%s is not a regular file!\n", _PATH_SUDO_SUDOERS);
	    break;

	case SUDOERS_WRONG_OWNER:
	    (void) fprintf(stderr, "%s is not owned by %s!\n",
		_PATH_SUDO_SUDOERS, SUDOERS_OWNER);
	    break;

	case SUDOERS_RW_OTHER:
	    (void) fprintf(stderr,
		"%s is readable or writeable by other than %s!\n",
		_PATH_SUDO_SUDOERS, SUDOERS_OWNER);
	    break;

	default:
	    (void) fprintf(stderr,
		"Something wierd happened.\n\n");
	    break;
    }
}



/****************************************************************
 *
 *  appropriate()
 *
 *  This function determines whether to send mail or not...
 */

static int appropriate(code)
    int code;
{

    switch (code) {

    /* 
     * these will NOT send mail
     */
    case VALIDATE_OK:
    case PASSWORD_NOT_CORRECT:
/*  case ALL_SYSTEMS_GO:               this is the same as OK */
	return (0);
	break;

    case VALIDATE_NO_USER:
#ifdef SEND_MAIL_WHEN_NO_USER
	return (1);
#else
	return (0);
#endif
	break;

    case VALIDATE_NOT_OK:
#ifdef SEND_MAIL_WHEN_NOT_OK
	return (1);
#else
	return (0);
#endif
	break;

    /*
     * these WILL send mail
     */
    case VALIDATE_ERROR:
    case NO_SUDOERS_FILE:
    default:
	return (1);
	break;

    }
}