-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
loki_launchurl.c
215 lines (197 loc) · 6.53 KB
/
loki_launchurl.c
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
/*
Loki Game Utility Functions
Copyright (C) 1999 Loki Entertainment Software
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "loki_launchurl.h"
/* Current running environment */
typedef enum {
RUNNING_X11,
RUNNING_TEXT
} environment;
/* This function verifies that a program is in the path and executable */
int loki_valid_program(const char *program)
{
char temppath[PATH_MAX];
char *path;
char *last;
int found;
found = 0;
path = getenv("PATH");
do {
/* Initialize our filename variable */
temppath[0] = '\0';
/* Get next entry from path variable */
last = strchr(path, ':');
if ( ! last )
last = path+strlen(path);
/* Perform tilde expansion */
if ( *path == '~' ) {
strcpy(temppath, getenv("HOME") ? getenv("HOME") : ".");
++path;
}
/* Fill in the rest of the filename */
if ( last > (path+1) ) {
strncat(temppath, path, (last-path));
strcat(temppath, "/");
}
strcat(temppath, "./");
strcat(temppath, program);
/* See if it exists, and update path */
if ( access(temppath, X_OK) == 0 ) {
/* make sure it's not a directory... */
struct stat s;
if ((stat(temppath, &s) == 0) && (S_ISREG(s.st_mode)))
++found;
}
path = last+1;
} while ( *last && !found );
return found;
}
/* This function launches the user's web browser with the given URL.
The browser detection can be overridden by the LOKI_BROWSER environment
variable, which is used as the format string: %s is replaced with the
URL to be launched.
This function returns -1 if a browser could not be found, or the return
value of system("browser command") if one is available.
There is no way to tell whether or not the URL was valid.
WARNING: This function should NOT be called when a video mode is set.
*/
int loki_launchURL(const char *url)
{
/* List of programs and command strings to try */
struct {
environment running;
char *program;
char *command;
} browser_list[] = {
#ifdef darwin
/* Call the default browser in OS X */
{ RUNNING_X11,
"open",
"open %s" },
{ RUNNING_TEXT,
"open",
"open %s" },
#endif
{ RUNNING_X11,
"xdg-open",
"xdg-open %s &" },
{ RUNNING_X11,
"gnome-moz-remote",
"gnome-moz-remote --newwin %s &" },
{ RUNNING_X11,
"firefox",
"firefox %s &" },
{ RUNNING_X11,
"seamonkey",
"seamonkey %s &" },
{ RUNNING_X11,
"netscape",
"netscape -remote 'openURL(%s,new-window)' || netscape %s &" },
{ RUNNING_X11,
"galeon",
"galeon %s &" },
{ RUNNING_X11,
"konqueror",
"konqueror %s &" },
{ RUNNING_X11,
"mozilla",
"mozilla -chrome %s || mozilla %s &" },
{ RUNNING_X11,
"opera",
"opera -newwindow %s &" },
{ RUNNING_X11,
"NetPositive", /* this is BeOS's default browser. */
"NetPositive %s &" },
{ RUNNING_X11,
"opera",
"opera -page='%s' &" },
{ RUNNING_X11,
"links",
"xterm -T \"Web Browser\" -e links %s &" },
{ RUNNING_X11,
"lynx",
"xterm -T \"Web Browser\" -e lynx %s &" },
{ RUNNING_TEXT,
"links",
"links %s" },
{ RUNNING_TEXT,
"lynx",
"lynx %s" }
};
environment running = RUNNING_X11; /* default for BeOS */
int i;
int status = -1;
char *command;
char command_string[4*PATH_MAX];
/* Check for DISPLAY environment variable - assume X11 if exists */
/* BeOS will default to "X11", which is a little white lie. */
#if (!defined __BEOS__)
if ( getenv("DISPLAY") ) {
running = RUNNING_X11;
} else {
running = RUNNING_TEXT;
}
#endif
/* See what web browser is available */
command = getenv("LOKI_BROWSER");
if ( ! command ) {
command = getenv("BROWSER"); /* FIXME: We should try all colon-separated commands in turn */
}
if ( ! command ) {
for ( i=0; i<(sizeof browser_list)/(sizeof browser_list[0]); ++i ) {
if ( (running == browser_list[i].running) &&
loki_valid_program(browser_list[i].program) ) {
command = browser_list[i].command;
break;
}
}
}
if ( command ) {
if ( strstr(command, "%s") ) {
snprintf(command_string, sizeof(command_string), command, url, url);
} else {
snprintf(command_string, sizeof(command_string), "%s %s", command, url);
}
if (geteuid() == 0) { // Attempt to drop privileges with setpriv or sudo
const char *sudo_uid = getenv("SUDO_UID");
if (sudo_uid) {
const char *setpriv_path = loki_valid_program("setpriv");
if (setpriv_path) {
char setpriv_string[4*PATH_MAX];
snprintf(setpriv_string, sizeof(setpriv_string), "%s --reuid %s %s", setpriv_path, sudo_uid, command_string);
status = system(setpriv_string);
}
} else { // Attempt to use sudo instead
const char *user = getenv("USER");
if (user && strcmp(user, "root")) {
char sudo_string[4*PATH_MAX];
snprintf(sudo_string, sizeof(sudo_string), "sudo -E -u %s %s", user, command_string);
status = system(sudo_string);
} else { // Last ditch effort
status = system(command_string);
}
}
} else {
status = system(command_string);
}
}
return status;
}