View | Details | Raw Unified | Return to bug 210537 | Differences between
and this patch

Collapse All | Expand All

(-)Makefile (-2 / +3 lines)
Lines 4-12 Link Here
4
MAN=	cron.8
4
MAN=	cron.8
5
SRCS=	cron.c database.c do_command.c job.c user.c popen.c
5
SRCS=	cron.c database.c do_command.c job.c user.c popen.c
6
WARNS=	2
6
7
7
CFLAGS+= -DLOGIN_CAP
8
CFLAGS+= -DLOGIN_CAP
8
9
9
DPADD=	${LIBCRON} ${LIBUTIL}
10
DPADD=	${LIBCRON} ${LIBUTIL} ${LIBMAGIC} ${LIBZ}
10
LDADD=	${LIBCRON} -lutil
11
LDADD=	${LIBCRON} -lutil -lmagic -lz
11
12
12
.include <bsd.prog.mk>
13
.include <bsd.prog.mk>
(-)cron.8 (-2 / +20 lines)
Lines 29-32 Link Here
29
.Op Fl J Ar rootjitter
29
.Op Fl J Ar rootjitter
30
.Op Fl s
30
.Op Fl s
31
.Op Fl m
31
.Op Fl o
32
.Op Fl o
32
.Op Fl x Ar debugflag Ns Op , Ns Ar ...
33
.Op Fl x Ar debugflag Ns Op , Ns Ar ...
Lines 105-108 Link Here
105
.Fl j
106
.Fl j
106
except that it will affect jobs run by the superuser only.
107
except that it will affect jobs run by the superuser only.
108
.It Fl m
109
When preparing an e-mail with a job's output (if any), try to guess the
110
Content-Type of the message. This uses
111
.Lb libmagic
112
-- the same mechanism, as that used by
113
.Nm file.
114
.Pp
115
Note, only the initial portion of the output is analyzed (typicaly
116
-- up to 1024 characters), which could sometimes lead to incorrect results.
117
For example, if the text contains its first non-ASCII character beyond 
118
the initial portion, the charset part of the content-type may be incorrectly
119
set to "us-ascii".
107
.It Fl s
120
.It Fl s
108
Enable special handling of situations when the GMT offset of the local
121
Enable special handling of situations when the GMT offset of the local
Lines 176-180 Link Here
176
.Sh SEE ALSO
189
.Sh SEE ALSO
177
.Xr crontab 1 ,
190
.Xr crontab 1 ,
178
.Xr crontab 5
191
.Xr crontab 5 ,
192
.Xr file 1 ,
193
.Xr libmagic 3
179
.Sh AUTHORS
194
.Sh AUTHORS
180
.An Paul Vixie Aq paul@vix.com
195
.An Paul Vixie (original author) Aq paul@vix.com
196
.An Dmitry Morozovsky (jitter additions)
197
.An Sergey Babkin (time-zone changing code) Aq babkin@FreeBSD.org
198
.An Mikhail Teterin (Content-Type setting) Aq mi@aldan.algebra.com
(-)cron.c (-2 / +17 lines)
Lines 31-34 Link Here
31
# include <time.h>
31
# include <time.h>
32
#endif
32
#endif
33
#include <magic.h>
33
34
34
35
Lines 53-57 Link Here
53
    char **dflags;
54
    char **dflags;
54
55
55
	fprintf(stderr, "usage: cron [-j jitter] [-J rootjitter] "
56
	fprintf(stderr, "usage: cron [-j jitter] [-J rootjitter] [-m]"
56
			"[-s] [-o] [-x debugflag[,...]]\n");
57
			"[-s] [-o] [-x debugflag[,...]]\n");
57
	fprintf(stderr, "\ndebugflags: ");
58
	fprintf(stderr, "\ndebugflags: ");
Lines 433-436 Link Here
433
}
434
}
434
435
436
extern magic_t Magic;
435
437
436
static void
438
static void
Lines 442-447 Link Here
442
	char	*endp;
444
	char	*endp;
443
445
444
	while ((argch = getopt(argc, argv, "j:J:osx:")) != -1) {
446
	while ((argch = getopt(argc, argv, "mj:J:osx:")) != -1) {
445
		switch (argch) {
447
		switch (argch) {
448
		case 'm':
449
			Magic = magic_open(MAGIC_MIME|MAGIC_ERROR);
450
			if (Magic == NULL) {
451
				warnx("failed to allocate the magic structure");
452
				break;
453
			}
454
			if (!magic_load(Magic, NULL))
455
				break;
456
			warnx("failed to load magic DB: %s",
457
			    magic_error(Magic));
458
			magic_close(Magic);
459
			Magic = NULL;
460
			break;
446
		case 'j':
461
		case 'j':
447
			Jitter = strtoul(optarg, &endp, 10);
462
			Jitter = strtoul(optarg, &endp, 10);
(-)do_command.c (-20 / +47 lines)
Lines 33-37 Link Here
33
# include <login_cap.h>
33
# include <login_cap.h>
34
#endif
34
#endif
35
#include <magic.h>
36
#include <inttypes.h>
35
37
38
magic_t	Magic = NULL; /* Try to set Content-Type in generated e-mail? */
36
39
37
static void		child_process __P((entry *, user *)),
40
static void		child_process __P((entry *, user *)),
Lines 394-398 Link Here
394
	/*local*/{
397
	/*local*/{
395
		register FILE	*in = fdopen(stdout_pipe[READ_PIPE], "r");
398
		register FILE	*in = fdopen(stdout_pipe[READ_PIPE], "r");
396
		register int	ch;
399
		char		buf[BUFSIZ];
400
		size_t		bufsize;
397
401
398
		if (in == NULL) {
402
		if (in == NULL) {
Lines 401-413 Link Here
401
		}
405
		}
402
406
403
		ch = getc(in);
407
		/*
404
		if (ch != EOF) {
408
		 * Read a little bit of the job's output -- enough to
409
		 * determine the * Content-Type of it...
410
		 */
411
		bufsize = fread(buf, sizeof(char),
412
		    sizeof(buf)/sizeof(char), in);
413
414
		if (bufsize > 0) {
405
			register FILE	*mail;
415
			register FILE	*mail;
406
			register int	bytes = 1;
416
			off_t		bytes = 0;
407
			int		status = 0;
417
			int		status = 0;
408
418
409
			Debug(DPROC|DEXT,
419
			Debug(DPROC|DEXT,
410
				("[%d] got data (%x:%c) from grandchild\n",
420
				("[%d] got data (%x:%c) from grandchild, %d chars\n",
411
					getpid(), ch, ch))
421
					getpid(), buf[0], buf[0], (int)bufsize))
412
422
413
			/* get name of recipient.  this is MAILTO if set to a
423
			/* get name of recipient.  this is MAILTO if set to a
Lines 437-440 Link Here
437
				auto char	mailcmd[MAX_COMMAND];
447
				auto char	mailcmd[MAX_COMMAND];
438
				auto char	hostname[MAXHOSTNAMELEN];
448
				auto char	hostname[MAXHOSTNAMELEN];
449
				const char	*mimetype;
439
450
440
				(void) gethostname(hostname, MAXHOSTNAMELEN);
451
				(void) gethostname(hostname, MAXHOSTNAMELEN);
Lines 445-450 Link Here
445
					(void) _exit(ERROR_EXIT);
456
					(void) _exit(ERROR_EXIT);
446
				}
457
				}
458
447
				fprintf(mail, "From: %s (Cron Daemon)\n", usernm);
459
				fprintf(mail, "From: %s (Cron Daemon)\n", usernm);
448
				fprintf(mail, "To: %s\n", mailto);
460
				fprintf(mail, "To: %s\n", mailto);
461
462
				if (Magic) {
463
					mimetype = magic_buffer(Magic,
464
					    buf, bufsize*sizeof(char));
465
					if (mimetype == NULL) {
466
						fprintf(mail,
467
						    "X-Cron-MagicError: %s\n",
468
						    magic_error(Magic));
469
					} else {
470
						fprintf(mail,
471
						    "Mime-Version: 1.0\n"
472
						    "Content-Type: %s\n"
473
						    "Content-Transfer-"
474
						    "Encoding: 8bit\n",
475
						    mimetype);
476
					}
477
				}
478
449
				fprintf(mail, "Subject: Cron <%s@%s> %s\n",
479
				fprintf(mail, "Subject: Cron <%s@%s> %s\n",
450
					usernm, first_word(hostname, "."),
480
					usernm, first_word(hostname, "."),
Lines 458-465 Link Here
458
						*env);
488
						*env);
459
				fprintf(mail, "\n");
489
				fprintf(mail, "\n");
460
461
				/* this was the first char from the pipe
462
				 */
463
				putc(ch, mail);
464
			}
490
			}
465
491
Lines 468-477 Link Here
468
			 * mail pipe if we ARE mailing.
494
			 * mail pipe if we ARE mailing.
469
			 */
495
			 */
470
496
			do {
471
			while (EOF != (ch = getc(in))) {
497
				if (mailto) {
472
				bytes++;
498
					bytes += sizeof(char) *
473
				if (mailto)
499
					    fwrite(buf, sizeof(char),
474
					putc(ch, mail);
500
						bufsize, mail);
475
			}
501
				}
502
				bufsize = fread(buf, sizeof(char),
503
				    sizeof(buf)/sizeof(char), in);
504
			} while (bufsize != 0);
476
505
477
			/* only close pipe if we opened it -- i.e., we're
506
			/* only close pipe if we opened it -- i.e., we're
Lines 496-504 Link Here
496
			 */
525
			 */
497
			if (mailto && status) {
526
			if (mailto && status) {
498
				char buf[MAX_TEMPSTR];
499
500
				snprintf(buf, sizeof(buf),
527
				snprintf(buf, sizeof(buf),
501
			"mailed %d byte%s of output but got status 0x%04x\n",
528
			"mailed %jd byte%s of output but got status 0x%04x\n",
502
					bytes, (bytes==1)?"":"s",
529
					(intmax_t)bytes, (bytes==1)?"":"s",
503
					status);
530
					status);
504
				log_it(usernm, getpid(), "MAIL", buf);
531
				log_it(usernm, getpid(), "MAIL", buf);

Return to bug 210537