summaryrefslogtreecommitdiff
path: root/src/manconv_client.c
blob: b8e7fd8dbc1ebf8411f62ac4e11dd83d2b9a690b (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
/*
 * manconv_client.c: use manconv in a pipeline
 *
 * Copyright (C) 2007, 2008, 2010 Colin Watson.
 *
 * This file is part of man-db.
 *
 * man-db 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.
 *
 * man-db 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 man-db; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif /* HAVE_CONFIG_H */

#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include "manconfig.h"

#include "pipeline.h"
#include "decompress.h"

#ifdef SECURE_MAN_UID
#  include "idpriv.h"
#  include "security.h"
#endif /* SECURE_MAN_UID */

#include "manconv.h"
#include "manconv_client.h"

struct manconv_codes {
	char **from;
	char *to;
};

static void manconv_stdin (void *data)
{
	struct manconv_codes *codes = data;
	pipeline *p;

#ifdef SECURE_MAN_UID
	/* iconv_open may not work correctly in setuid processes; in GNU
	 * libc, gconv modules may be linked against other gconv modules and
	 * rely on RPATH $ORIGIN to load those modules from the correct
	 * path, but $ORIGIN is disabled in setuid processes.  It is
	 * impossible to reset libc's idea of setuidness without creating a
	 * whole new process image.  Therefore, if the calling process is
	 * setuid, we must drop privileges and execute manconv.
	 *
	 * If dropping privileges fails, fall through to the in-process
	 * code, as in some situations it may actually manage to work.
	 */
	if (running_setuid () && !idpriv_drop ()) {
		char **from_code;
		char *sources = NULL;
		pipecmd *cmd;

		for (from_code = codes->from; *from_code; ++from_code) {
			sources = appendstr (sources, *from_code, NULL);
			if (*(from_code + 1))
				sources = appendstr (sources, ":", NULL);
		}

		cmd = pipecmd_new_args (MANCONV, "-f", sources,
					"-t", codes->to, NULL);
		free (sources);

		if (quiet >= 2)
			pipecmd_arg (cmd, "-q");

		pipecmd_exec (cmd);
		/* never returns */
	}
#endif /* SECURE_MAN_UID */

	p = decompress_fdopen (dup (STDIN_FILENO));
	pipeline_start (p);
	manconv (p, codes->from, codes->to);
	pipeline_wait (p);
	pipeline_free (p);
}

static void free_manconv_codes (void *data)
{
	struct manconv_codes *codes = data;
	char **try_from;

	for (try_from = codes->from; *try_from; ++try_from)
		free (*try_from);
	free (codes->from);
	free (codes->to);
	free (codes);
}

void add_manconv (pipeline *p, const char *source, const char *target)
{
	struct manconv_codes *codes = xmalloc (sizeof *codes);
	char *name;
	pipecmd *cmd;

	if (STREQ (source, "UTF-8") && STREQ (target, "UTF-8"))
		return;

	/* informational only; no shell quoting concerns */
	name = appendstr (NULL, MANCONV, " -f ", NULL);
	if (STREQ (source, "UTF-8")) {
		codes->from = XNMALLOC (2, char *);
		codes->from[0] = xstrdup (source);
		codes->from[1] = NULL;
		name = appendstr (name, source, NULL);
	} else {
		codes->from = XNMALLOC (3, char *);
		codes->from[0] = xstrdup ("UTF-8");
		codes->from[1] = xstrdup (source);
		codes->from[2] = NULL;
		name = appendstr (name, "UTF-8:", source, NULL);
	}
	codes->to = appendstr (NULL, target, "//IGNORE", NULL);
	/* informational only; no shell quoting concerns */
	name = appendstr (name, " -t ", codes->to, NULL);
	if (quiet >= 2)
		name = appendstr (name, " -q", NULL);
	cmd = pipecmd_new_function (name, &manconv_stdin, &free_manconv_codes,
				    codes);
	free (name);
	pipeline_command (p, cmd);
}