Diffstat (limited to 'noncore/settings/networksettings/ppp/runtests.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/settings/networksettings/ppp/runtests.cpp | 279 |
1 files changed, 279 insertions, 0 deletions
diff --git a/noncore/settings/networksettings/ppp/runtests.cpp b/noncore/settings/networksettings/ppp/runtests.cpp new file mode 100644 index 0000000..83ef2ea --- a/dev/null +++ b/noncore/settings/networksettings/ppp/runtests.cpp | |||
@@ -0,0 +1,279 @@ | |||
1 | /* | ||
2 | * kPPP: A pppd front end for the KDE project | ||
3 | * | ||
4 | * $Id$ | ||
5 | * | ||
6 | * Copyright (C) 1997 Bernd Johannes Wuebben | ||
7 | * wuebben@math.cornell.edu | ||
8 | * | ||
9 | * This file was contributed by Mario Weilguni <mweilguni@sime.com> | ||
10 | * Thanks Mario ! | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU Library General Public | ||
14 | * License as published by the Free Software Foundation; either | ||
15 | * version 2 of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This program is distributed in the hope that it will be useful, | ||
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
20 | * Library General Public License for more details. | ||
21 | * | ||
22 | * You should have received a copy of the GNU Library General Public | ||
23 | * License along with this program; if not, write to the Free | ||
24 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
25 | */ | ||
26 | |||
27 | #include <qdir.h> | ||
28 | #include "runtests.h" | ||
29 | #include <ctype.h> | ||
30 | #include <unistd.h> | ||
31 | #include <qmessagebox.h> | ||
32 | #include <sys/stat.h> | ||
33 | #include <stdlib.h> | ||
34 | #include <sys/types.h> | ||
35 | #include <pwd.h> | ||
36 | #include <netinet/in.h> | ||
37 | |||
38 | #ifdef HAVE_RESOLV_H | ||
39 | #include <arpa/nameser.h> | ||
40 | #include <resolv.h> | ||
41 | #endif | ||
42 | |||
43 | #ifndef _PATH_RESCONF | ||
44 | #define _PATH_RESCONF "/etc/resolv.conf" | ||
45 | #endif | ||
46 | |||
47 | //#include <klocale.h> | ||
48 | #define i18n QObject::tr | ||
49 | #include "pppdata.h" | ||
50 | |||
51 | // initial effective uid (main.cpp) | ||
52 | extern uid_t euid; | ||
53 | |||
54 | // secure pppd location (opener.cpp) | ||
55 | extern const char* pppdPath(); | ||
56 | |||
57 | // shamelessly stolen from pppd-2.3.5 | ||
58 | /******************************************************************** | ||
59 | * | ||
60 | * Internal routine to decode the version.modification.patch level | ||
61 | */ | ||
62 | |||
63 | static void decode_version (const char *_buf, int *version, | ||
64 | int *modification, int *patch) | ||
65 | { | ||
66 | char *buffer = qstrdup(_buf); | ||
67 | char *buf = buffer; | ||
68 | *version = (int) strtoul (buf, &buf, 10); | ||
69 | *modification = 0; | ||
70 | *patch = 0; | ||
71 | |||
72 | if (*buf == '.') | ||
73 | { | ||
74 | ++buf; | ||
75 | *modification = (int) strtoul (buf, &buf, 10); | ||
76 | if (*buf == '.') | ||
77 | { | ||
78 | ++buf; | ||
79 | *patch = (int) strtoul (buf, &buf, 10); | ||
80 | } | ||
81 | } | ||
82 | |||
83 | if (*buf != '\0') | ||
84 | { | ||
85 | *version = | ||
86 | *modification = | ||
87 | *patch = 0; | ||
88 | } | ||
89 | |||
90 | delete [] buffer; | ||
91 | } | ||
92 | |||
93 | |||
94 | void pppdVersion(int *version, int *modification, int *patch) { | ||
95 | char buffer[30]; | ||
96 | const char *pppd; | ||
97 | char *query; | ||
98 | |||
99 | *version = *modification = *patch = 0; | ||
100 | |||
101 | // locate pppd | ||
102 | if(!(pppd = pppdPath())) | ||
103 | return; | ||
104 | |||
105 | // call pppd with --version option | ||
106 | if(!(query = new char[strlen(pppd)+25])) | ||
107 | return; | ||
108 | strcpy(query, pppd); | ||
109 | // had to add a dummy device to prevent a "no device specified | ||
110 | // and stdin is not a tty" error from newer pppd versions. | ||
111 | strcat(query, " --version /dev/tty 2>&1"); | ||
112 | fflush(0L); | ||
113 | FILE *output = popen(query, "r"); | ||
114 | delete [] query; | ||
115 | if(!output) | ||
116 | return; | ||
117 | |||
118 | // read output | ||
119 | int size = fread(buffer, sizeof(char), 29, output); | ||
120 | |||
121 | if(ferror(output)) { | ||
122 | pclose(output); | ||
123 | return; | ||
124 | } | ||
125 | pclose(output); | ||
126 | buffer[size] = '\0'; | ||
127 | |||
128 | // find position of version number x.y.z | ||
129 | char *p = buffer; | ||
130 | while(*p && !isdigit(*p)) | ||
131 | p++; | ||
132 | if (*p == 0) | ||
133 | return; | ||
134 | char *p2 = p; | ||
135 | while(*p2 == '.' || isdigit(*p2)) | ||
136 | p2++; | ||
137 | *p2 = '\0'; | ||
138 | |||
139 | decode_version(p, version, modification, patch); | ||
140 | } | ||
141 | |||
142 | |||
143 | int uidFromName(const char *uname) { | ||
144 | struct passwd *pw; | ||
145 | |||
146 | setpwent(); | ||
147 | while((pw = getpwent()) != NULL) { | ||
148 | if(strcmp(uname, pw->pw_name) == 0) { | ||
149 | int uid = pw->pw_uid; | ||
150 | endpwent(); | ||
151 | return uid; | ||
152 | } | ||
153 | } | ||
154 | |||
155 | endpwent(); | ||
156 | return -1; | ||
157 | } | ||
158 | |||
159 | |||
160 | const char *homedirFromUid(uid_t uid) { | ||
161 | struct passwd *pw; | ||
162 | char *d = 0; | ||
163 | |||
164 | setpwent(); | ||
165 | while((pw = getpwent()) != NULL) { | ||
166 | if(pw->pw_uid == uid) { | ||
167 | d = strdup(pw->pw_dir); | ||
168 | endpwent(); | ||
169 | return d; | ||
170 | } | ||
171 | } | ||
172 | |||
173 | endpwent(); | ||
174 | return d; | ||
175 | } | ||
176 | |||
177 | |||
178 | const char* getHomeDir() { | ||
179 | static const char *hd = 0; | ||
180 | static bool ranTest = false; | ||
181 | if(!ranTest) { | ||
182 | hd = homedirFromUid(getuid()); | ||
183 | ranTest = true; | ||
184 | } | ||
185 | |||
186 | return hd; | ||
187 | } | ||
188 | |||
189 | |||
190 | int runTests() { | ||
191 | int warning = 0; | ||
192 | |||
193 | // Test pre-1: check if the user is allowed to dial-out | ||
194 | if(access("/etc/kppp.allow", R_OK) == 0 && getuid() != 0) { | ||
195 | bool access = FALSE; | ||
196 | FILE *f; | ||
197 | if((f = fopen("/etc/kppp.allow", "r")) != NULL) { | ||
198 | char buf[2048]; // safe | ||
199 | while(f != NULL && !feof(f)) { | ||
200 | if(fgets(buf, sizeof(buf), f) != NULL) { | ||
201 | QString s(buf); | ||
202 | |||
203 | s = s.stripWhiteSpace(); | ||
204 | if(s[0] == '#' || s.length() == 0) | ||
205 | continue; | ||
206 | |||
207 | if((uid_t)uidFromName(QFile::encodeName(s)) == getuid()) { | ||
208 | access = TRUE; | ||
209 | fclose(f); | ||
210 | f = NULL; | ||
211 | } | ||
212 | } | ||
213 | } | ||
214 | if(f) | ||
215 | fclose(f); | ||
216 | } | ||
217 | |||
218 | if(!access) { | ||
219 | QMessageBox::warning(0,"error", | ||
220 | i18n("You're not allowed to dial out with " | ||
221 | "kppp.\nContact your system administrator.")); | ||
222 | return TEST_CRITICAL; | ||
223 | } | ||
224 | } | ||
225 | |||
226 | // Test 1: search the pppd binary | ||
227 | const char *f = pppdPath(); | ||
228 | |||
229 | if(!f) { | ||
230 | QMessageBox::warning(0,"error", | ||
231 | i18n("Cannot find the PPP daemon!\n" | ||
232 | "Make sure that pppd is installed.")); | ||
233 | warning++; | ||
234 | } | ||
235 | |||
236 | // Test 2: check access to the pppd binary | ||
237 | if(f) { | ||
238 | #if 0 | ||
239 | if(access(f, X_OK) != 0 /* && geteuid() != 0 */) { | ||
240 | KMessageBox::warning(0, | ||
241 | i18n("You do not have the permission " | ||
242 | "to start pppd!\n" | ||
243 | "Contact your system administrator " | ||
244 | "and ask to get access to pppd.")); | ||
245 | return TEST_CRITICAL; | ||
246 | } | ||
247 | #endif | ||
248 | |||
249 | if(geteuid() != 0) { | ||
250 | struct stat st; | ||
251 | stat(f, &st); | ||
252 | if(st.st_uid != 0 || (st.st_mode & S_ISUID) == 0) { | ||
253 | QMessageBox::warning(0,"error", | ||
254 | i18n("You don't have sufficient permission to run\n" | ||
255 | "%1\n" | ||
256 | "Please make sure that kppp is owned by root " | ||
257 | "and has the SUID bit set.").arg(f)); | ||
258 | warning++; | ||
259 | } | ||
260 | } | ||
261 | } | ||
262 | |||
263 | // Test 5: check for existence of /etc/resolv.conf | ||
264 | if (access(_PATH_RESCONF, R_OK) != 0) { | ||
265 | QString file = _PATH_RESCONF" "; | ||
266 | QString msgstr = i18n("%1 is missing or can't be read!\n" | ||
267 | "Ask your system administrator to create " | ||
268 | "this file (can be empty) with appropriate " | ||
269 | "read and write permissions.").arg(file); | ||
270 | QMessageBox::warning(0, "errror", msgstr); | ||
271 | warning ++; | ||
272 | } | ||
273 | |||
274 | if(warning == 0) | ||
275 | return TEST_OK; | ||
276 | else | ||
277 | return TEST_WARNING; | ||
278 | } | ||
279 | |||