summaryrefslogtreecommitdiff
path: root/src/pmpost/pmpost.c
blob: 2691cc9420461de7db319594e46d7b653f7eb6d1 (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
/*
 * Copyright (c) 1995-2003 Silicon Graphics, Inc.  All Rights Reserved.
 * 
 * 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 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 "pmapi.h"
#include "impl.h"
#include <sys/stat.h>
#include <sys/file.h>
#ifdef HAVE_GRP_H
#include <grp.h>
#endif

/*
 * Attempt to setup the notices file in a way that members of
 * the (unprivileged) "pcp" group account can write to it.
 */
int gid;
#ifdef HAVE_GETGRNAM
static void
setup_group(void)
{
    char *name = pmGetConfig("PCP_GROUP");
    struct group *group;

    if (!name || name[0] == '\0')
	name = "pcp";
    group = getgrnam(name);
    if (group)
	gid = group->gr_gid;
}
#else
#define setup_group()
#endif

static int
mkdir_r(char *path)
{
    struct stat	sbuf;
    int sts;

    if (stat(path, &sbuf) < 0) {
	if (mkdir_r(dirname(strdup(path))) < 0)
	    return -1;
	sts = mkdir2(path, 0775);
	if (chown(path, 0, gid) < 0)
	    fprintf(stderr, "pmpost: cannot set dir gid[%d]: %s\n", gid, path);
	return sts;
    }
    else if ((sbuf.st_mode & S_IFDIR) == 0) {
	fprintf(stderr, "pmpost: \"%s\" is not a directory\n", path);
	exit(1);
    }
    return 0;
}

#define LAST_UNDEFINED	-1
#define LAST_NEWFILE	-2

int
main(int argc, char **argv)
{
    int		i;
    int		fd;
    FILE	*np;
    char	*dir;
    struct stat	sbuf;
    time_t	now;
    int		lastday = LAST_UNDEFINED;
    struct tm	*tmp;
    int		sts = 0;
    char	notices[MAXPATHLEN];
#ifndef IS_MINGW
    char	*ep;
    struct flock lock;
    extern char **environ;
    static char *newenviron[] =
	{ "HOME=/nowhere", "SHELL=/noshell", "PATH=/nowhere", NULL };
    static char *keepname[] =
	{ "TZ", "PCP_DIR", "PCP_CONF", NULL };
    char *keepval[] =
	{ NULL, NULL,      NULL,       NULL };

    /*
     * Fix for bug #827972, do not trust the environment.
     * See also below.
     */
    for (i = 0; keepname[i] != NULL; i++)
	keepval[i] = getenv(keepname[i]);
    environ = newenviron;
    for (i = 0; keepname[i] != NULL; i++) {
	if (keepval[i] != NULL) {
	    snprintf(notices, sizeof(notices), "%s=%s", keepname[i], keepval[i]);
	    if ((ep = strdup(notices)) != NULL)
		putenv(ep);
	}
    }
#endif
    umask(0002);

    if ((argc == 1) || (argc == 2 && strcmp(argv[1], "-?") == 0)) {
	fprintf(stderr, "Usage: pmpost message\n");
	exit(1);
    }

    snprintf(notices, sizeof(notices), "%s%c" "NOTICES",
		pmGetConfig("PCP_LOG_DIR"), __pmPathSeparator());

    setup_group();
    dir = dirname(strdup(notices));
    if (mkdir_r(dir) < 0) {
	fprintf(stderr, "pmpost: cannot create directory \"%s\": %s\n",
	    dir, osstrerror());
	exit(1);
    }

    if ((fd = open(notices, O_WRONLY|O_APPEND, 0)) < 0) {
	if ((fd = open(notices, O_WRONLY|O_CREAT|O_APPEND, 0664)) < 0) {
	    fprintf(stderr, "pmpost: cannot create file \"%s\": %s\n",
		notices, osstrerror());
	    exit(1);
	} else if ((fchown(fd, 0, gid)) < 0) {
	    fprintf(stderr, "pmpost: cannot set file gid \"%s\": %s\n",
		notices, osstrerror());
	}
	lastday = LAST_NEWFILE;
    }

#ifndef IS_MINGW
    /*
     * drop root privileges for bug #827972
     */
    if (setuid(getuid()) < 0)
    	exit(1);

    lock.l_type = F_WRLCK;
    lock.l_whence = 0;
    lock.l_start = 0;
    lock.l_len = 0;

    /*
     * willing to try for 3 seconds to get the lock ... note fcntl()
     * does not block, unlike flock()
     */
    for (i = 0; i < 3; i++) {
	if ((sts = fcntl(fd, F_SETLK, &lock)) != -1)
	    break;
	sleep(1);
    }
    
    if (sts == -1) {
	fprintf(stderr, "pmpost: warning, cannot lock file \"%s\"", notices);
	if (oserror() != EINTR)
	    fprintf(stderr, ": %s", osstrerror());
	fputc('\n', stderr);
    }
    sts = 0;
#endif

    /*
     * have lock, get last modified day unless file just created
     */
    if (lastday != LAST_NEWFILE) {
	if (fstat(fd, &sbuf) < 0)
	    /* should never happen */
	    ;
	else {
	    tmp = localtime(&sbuf.st_mtime);
	    lastday = tmp->tm_yday;
	}
    }

    if ((np = fdopen(fd, "a")) == NULL) {
	fprintf(stderr, "pmpost: fdopen: %s\n", osstrerror());
	exit(1);
    }

    time(&now);
    tmp = localtime(&now);

    if (lastday != tmp->tm_yday) {
	if (fprintf(np, "\nDATE: %s", ctime(&now)) < 0)
	    sts = oserror();
    }

    if (fprintf(np, "%02d:%02d", tmp->tm_hour, tmp->tm_min) < 0)
	sts = oserror();

    for (i = 1; i < argc; i++) {
	if (fprintf(np, " %s", argv[i]) < 0)
	    sts = oserror();
    }

    if (fputc('\n', np) < 0)
	sts = oserror();

    if (fclose(np) < 0)
	sts = oserror();

    if (sts < 0) {
	fprintf(stderr, "pmpost: write failed: %s\n", osstrerror());
	fprintf(stderr, "Lost message ...");
	for (i = 1; i < argc; i++) {
	    fprintf(stderr, " %s", argv[i]);
	}
	fputc('\n', stderr);
    }

    exit(0);
}