summaryrefslogtreecommitdiffstats
path: root/contrib/sendmail/src/control.c
blob: 415818cdd773cfa2ca1c2fdc116f3de32c6e173c (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
/*
 * Copyright (c) 1998 Sendmail, Inc.  All rights reserved.
 *
 * By using this file, you agree to the terms and conditions set
 * forth in the LICENSE file which can be found at the top level of
 * the sendmail distribution.
 *
 */

#ifndef lint
static char sccsid[] = "@(#)control.c	8.18 (Berkeley) 1/17/1999";
#endif /* not lint */

#include "sendmail.h"

int ControlSocket = -1;

/*
**  OPENCONTROLSOCKET -- create/open the daemon control named socket
**
**	Creates and opens a named socket for external control over
**	the sendmail daemon.
**
**	Parameters:
**		none.
**
**	Returns:
**		0 if successful, -1 otherwise
*/

int
opencontrolsocket()
{
#ifdef NETUNIX
# if _FFR_CONTROL_SOCKET
	int rval;
	int sff = SFF_SAFEDIRPATH|SFF_OPENASROOT|SFF_NOLINK|SFF_CREAT|SFF_MUSTOWN;
	struct sockaddr_un controladdr;

	if (ControlSocketName == NULL)
		return 0;

	if (strlen(ControlSocketName) >= sizeof controladdr.sun_path)
	{
		errno = ENAMETOOLONG;
		return -1;
	}

	rval = safefile(ControlSocketName, RunAsUid, RunAsGid, RunAsUserName,
			sff, S_IRUSR|S_IWUSR, NULL);

	/* if not safe, don't create */
	if (rval != 0)
	{
		errno = rval;
		return -1;
	}

	ControlSocket = socket(AF_UNIX, SOCK_STREAM, 0);
	if (ControlSocket < 0)
		return -1;

	unlink(ControlSocketName);
	bzero(&controladdr, sizeof controladdr);
	controladdr.sun_family = AF_UNIX;
	strcpy(controladdr.sun_path, ControlSocketName);

	if (bind(ControlSocket, (struct sockaddr *) &controladdr,
		 sizeof controladdr) < 0)
	{
		int save_errno = errno;

		clrcontrol();
		errno = save_errno;
		return -1;
	}

#  if _FFR_TRUSTED_USER
	if (geteuid() == 0 && TrustedUid != 0)
	{
		if (chown(ControlSocketName, TrustedUid, -1) < 0)
		{
			int save_errno = errno;

			sm_syslog(LOG_ALERT, NOQID,
				  "ownership change on %s failed: %s",
				  ControlSocketName, errstring(save_errno));
			message("050 ownership change on %s failed: %s",
				ControlSocketName, errstring(save_errno));
			closecontrolsocket(TRUE);
			errno = save_errno;
			return -1;
		}
	}
#  endif

	if (chmod(ControlSocketName, S_IRUSR|S_IWUSR) < 0)
	{
		int save_errno = errno;

		closecontrolsocket(TRUE);
		errno = save_errno;
		return -1;
	}

	if (listen(ControlSocket, 8) < 0)
	{
		int save_errno = errno;

		closecontrolsocket(TRUE);
		errno = save_errno;
		return -1;
	}
# endif
#endif
	return 0;
}
/*
**  CLOSECONTROLSOCKET -- close the daemon control named socket
**
**	Close a named socket.
**
**	Parameters:
**		fullclose -- if set, close the socket and remove it;
**			     otherwise, just remove it
**
**	Returns:
**		none.
*/

void
closecontrolsocket(fullclose)
	bool fullclose;
{
#ifdef NETUNIX
# if _FFR_CONTROL_SOCKET
	int sff = SFF_SAFEDIRPATH|SFF_OPENASROOT|SFF_NOLINK|SFF_CREAT|SFF_MUSTOWN;

	if (ControlSocket >= 0)
	{
		int rval;

		if (fullclose)
		{
			(void) close(ControlSocket);
			ControlSocket = -1;
		}

		rval = safefile(ControlSocketName, RunAsUid, RunAsGid, RunAsUserName,
				sff, S_IRUSR|S_IWUSR, NULL);
		
		/* if not safe, don't unlink */
		if (rval != 0)
			return;

		if (unlink(ControlSocketName) < 0)
		{
			sm_syslog(LOG_WARNING, NOQID,
				  "Could not remove control socket: %s",
				  errstring(errno));
			return;
		}
	}
# endif
#endif
	return;
}
/*
**  CLRCONTROL -- reset the control connection
**
**	Parameters:
**		none.
**
**	Returns:
**		none.
**
**	Side Effects:
**		releases any resources used by the control interface.
*/

void
clrcontrol()
{
#ifdef NETUNIX
# if _FFR_CONTROL_SOCKET
	if (ControlSocket >= 0)
		(void) close(ControlSocket);
	ControlSocket = -1;
# endif
#endif
}

#ifndef NOT_SENDMAIL

/*
**  CONTROL_COMMAND -- read and process command from named socket
**
**	Read and process the command from the opened socket.
**	Return the results down the same socket.
**
**	Parameters:
**		sock -- the opened socket from getrequests()
**		e -- the current envelope
**
**	Returns:
**		none.
*/

struct cmd
{
	char	*cmdname;	/* command name */
	int	cmdcode;	/* internal code, see below */
};

/* values for cmdcode */
# define CMDERROR	0	/* bad command */
# define CMDRESTART	1	/* restart daemon */
# define CMDSHUTDOWN	2	/* end daemon */
# define CMDHELP	3	/* help */
# define CMDSTATUS	4	/* daemon status */

static struct cmd	CmdTab[] =
{
	{ "help",	CMDHELP		},
	{ "restart",	CMDRESTART	},
	{ "shutdown",	CMDSHUTDOWN	},
	{ "status",	CMDSTATUS	},
	{ NULL,		CMDERROR	}
};

void
control_command(sock, e)
	int sock;
	ENVELOPE *e;
{
	FILE *s;
	FILE *traffic;
	FILE *oldout;
	char *cmd;
	char *p;
	struct cmd *c;
	char cmdbuf[MAXLINE];
	char inp[MAXLINE];
	extern char **SaveArgv;
	extern void help __P((char *));

	sm_setproctitle(FALSE, "control cmd read");
		
	s = fdopen(sock, "r+");
	if (s == NULL)
	{
		int save_errno = errno;

		close(sock);
		errno = save_errno;
		return;
	}
	setbuf(s, NULL);

	if (fgets(inp, sizeof inp, s) == NULL)
	{
		fclose(s);
		return;
	}
	(void) fflush(s);

	/* clean up end of line */
	fixcrlf(inp, TRUE);

	sm_setproctitle(FALSE, "control: %s", inp);

	/* break off command */
	for (p = inp; isascii(*p) && isspace(*p); p++)
		continue;
	cmd = cmdbuf;
	while (*p != '\0' &&
	       !(isascii(*p) && isspace(*p)) &&
	       cmd < &cmdbuf[sizeof cmdbuf - 2])
		*cmd++ = *p++;
	*cmd = '\0';
	
	/* throw away leading whitespace */
	while (isascii(*p) && isspace(*p))
		p++;
	
	/* decode command */
	for (c = CmdTab; c->cmdname != NULL; c++)
	{
		if (!strcasecmp(c->cmdname, cmdbuf))
			break;
	}

	switch (c->cmdcode)
	{
	  case CMDHELP:		/* get help */
		traffic = TrafficLogFile;
		TrafficLogFile = NULL;
		oldout = OutChannel;
		OutChannel = s;
		help("control");
		TrafficLogFile = traffic;
		OutChannel = oldout;
		break;
		
	  case CMDRESTART:	/* restart the daemon */
		if (SaveArgv[0][0] != '/')
		{
			fprintf(s, "ERROR: could not restart: need full path\r\n");
			break;
		}
		if (LogLevel > 3)
			sm_syslog(LOG_INFO, NOQID,
				  "restarting %s on due to control command",
				  SaveArgv[0]);
		closecontrolsocket(FALSE);
		if (drop_privileges(TRUE) != EX_OK)
		{
			if (LogLevel > 0)
				sm_syslog(LOG_ALERT, NOQID,
					  "could not set[ug]id(%d, %d): %m",
					  RunAsUid, RunAsGid);

			fprintf(s, "ERROR: could not set[ug]id(%d, %d): %s, exiting...\r\n",
				(int)RunAsUid, (int)RunAsGid, errstring(errno));
			finis(FALSE, EX_OSERR);
		}
		fprintf(s, "OK\r\n");
		clrcontrol();
		(void) fcntl(sock, F_SETFD, 1);
		execve(SaveArgv[0], (ARGV_T) SaveArgv, (ARGV_T) ExternalEnviron);
		if (LogLevel > 0)
			sm_syslog(LOG_ALERT, NOQID, "could not exec %s: %m",
				  SaveArgv[0]);
		fprintf(s, "ERROR: could not exec %s: %s, exiting...\r\n",
			SaveArgv[0], errstring(errno));
		finis(FALSE, EX_OSFILE);
		break;

	  case CMDSHUTDOWN:	/* kill the daemon */
		fprintf(s, "OK\r\n");
		finis(FALSE, EX_OK);
		break;

	  case CMDSTATUS:	/* daemon status */
		proc_list_probe();
		fprintf(s, "%d/%d\r\n", CurChildren, MaxChildren);
		proc_list_display(s);
		break;

	  case CMDERROR:	/* unknown command */
		fprintf(s, "Bad command (%s)\r\n", cmdbuf);
		break;
	}
	fclose(s);
}
#endif
OpenPOWER on IntegriCloud