Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mumble_plugin_linux.h « plugins - github.com/mumble-voip/mumble.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fe2b6a6aeaffbc4aa09fc6b35c6beb0f145106b8 (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
// Copyright 2005-2017 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#ifndef MUMBLE_PLUGIN_LINUX_H_
#define MUMBLE_PLUGIN_LINUX_H_

#ifndef PTR_TYPE_CONCRETE
# define PTR_TYPE_CONCRETE PTR_TYPE
#endif

#include <sys/uio.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <cstring>
#include <iostream>
#include <fstream>
#include <sstream>

#include "mumble_plugin.h"

pid_t pPid;
static PTR_TYPE_CONCRETE pModule;

static inline std::string readAll(std::string fn) {
	std::ifstream ifs;
	ifs.open(fn.c_str(), std::ifstream::binary);

	std::string content;

	char buf[256];
	while (ifs.good()) {
		ifs.read(&buf[0], sizeof(buf));
		size_t nread = ifs.gcount();
		if (nread > 0) {
			content.append(&buf[0], nread);
		}
	}

	return content;
}

static inline PTR_TYPE_CONCRETE getModuleAddr(pid_t pid, const wchar_t *modname) {
	std::wstring modnameWide(modname);
	std::string modnameNonWide(modnameWide.begin(), modnameWide.end());

	std::stringstream ss;
	ss << std::string("/proc/");
	ss << static_cast<unsigned long>(pid);
	ss << std::string("/maps");
	std::string mapsFn = ss.str();
	std::string maps = readAll(mapsFn);

	if (maps.size() == 0) {
		return 0;
	}

	std::stringstream ssPath(maps);
	while (ssPath.good()) {
		std::string baseaddr;

		int ch;
		while (1) {
			ch = ssPath.get();
			if (ch == '-') {
				break;
			} else if (ch == EOF) {
				return 0;
			}
			baseaddr.push_back(static_cast<char>(ch));
		}

		// seek to perms
		do {
			ch = ssPath.get();
			if (ch == EOF) {
				return 0;
			}
		} while (ch != ' ');

		// seek to offset
		do {
			ch = ssPath.get();
			if (ch == EOF) {
				return 0;
			}
		} while (ch != ' ');

		// seek to dev
		do {
			ch = ssPath.get();
			if (ch == EOF) {
				return 0;
			}
		} while (ch != ' ');

		// seek to inode
		do {
			ch = ssPath.get();
			if (ch == EOF) {
				return 0;
			}
		} while (ch != ' ');

		// seek to pathname
		do {
			ch = ssPath.get();
			if (ch == EOF) {
				return 0;
			}
		} while (ch != ' ');

		// eat spaces until we're at the beginning of pathname.
		while (ch == ' ') {
			if (ch == EOF) {
				return 0;
			}
			ch = ssPath.get();
		}
		ssPath.unget();

		std::string pathname;
		while (1) {
			ch = ssPath.get();
			if (ch == '\n') {
				break;
			} else if (ch == EOF) {
				return 0;
			}
			pathname.push_back(static_cast<char>(ch));
		};

		// OK, we found 'em!
		// Only treat path as a real path if it starts with /.
		if (pathname.size() > 0 && pathname.at(0) == '/') {
			// Find the basename.
			size_t lastSlash = pathname.find_last_of('/');
			if (pathname.size() > lastSlash + 1) {
				std::string basename = pathname.substr(lastSlash + 1);
				if (basename == modnameNonWide) {
					unsigned long addr = strtoul(baseaddr.c_str(), NULL, 16);
					return addr;
				}
			}
		}
	}

	return 0;
}

static inline PTR_TYPE_CONCRETE getModuleAddr(const wchar_t *modname) {
	return getModuleAddr(pPid, modname);
}

static inline bool peekProc(PTR_TYPE base, void *dest, size_t len) {
	struct iovec in;
	in.iov_base = reinterpret_cast<void *>(base); // Address from target process
	in.iov_len = len; // Length

	struct iovec out;
	out.iov_base = dest;
	out.iov_len = len;

	ssize_t nread = process_vm_readv(pPid, &out, 1, &in, 1, 0);

	return (nread != -1 && static_cast<size_t>(nread) == in.iov_len);
}

template<class T>
T peekProc(PTR_TYPE base) {
	T v = 0;
	if (!peekProc(base, reinterpret_cast<T *>(&v), sizeof(T))) {
		return 0;
	}
	return v;
}

template<class T>
bool peekProc(PTR_TYPE base, T &dest) {
	struct iovec in;
	in.iov_base = reinterpret_cast<void *>(base); // Address from target process
	in.iov_len = sizeof(T); // Length

	struct iovec out;
	out.iov_base = &dest;
	out.iov_len = sizeof(T);

	ssize_t nread = process_vm_readv(pPid, &out, 1, &in, 1, 0);

	return (nread != -1 && static_cast<size_t>(nread) == in.iov_len);
}

static bool inline initialize(const std::multimap<std::wstring, unsigned long long int> &pids, const wchar_t *procname, const wchar_t *modname = NULL) {
	pModule = 0;

	if (! pids.empty()) {
		std::multimap<std::wstring, unsigned long long int>::const_iterator iter = pids.find(std::wstring(procname));

		if (iter != pids.end())
			pPid = static_cast<pid_t>(iter->second);
		else
			pPid = 0;
	} else {
		pPid = 0;
	}

	if (pPid == 0)
		return false;

	pModule = getModuleAddr(modname ? modname : procname);

	if (pModule == 0) {
		pPid = 0;
		return false;
	}

	return true;
}

static void generic_unlock() {
	pModule = 0;
	pPid = 0;
}

#endif