2010-02-08 21:39:42 +01:00
|
|
|
/* Copyright (c) 2009, Nathan Freitas, Orbot / The Guardian Project - http://openideals.com/guardian */
|
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
package org.torproject.android.service;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
2011-02-09 22:11:53 +01:00
|
|
|
import java.io.File;
|
2010-09-17 23:32:21 +02:00
|
|
|
import java.io.IOException;
|
2010-02-08 21:39:42 +01:00
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.OutputStreamWriter;
|
|
|
|
import java.util.StringTokenizer;
|
|
|
|
|
2011-10-28 06:29:57 +02:00
|
|
|
import org.torproject.android.TorConstants;
|
|
|
|
|
2010-02-08 21:39:42 +01:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
public class TorServiceUtils implements TorServiceConstants {
|
|
|
|
|
2010-09-17 23:32:21 +02:00
|
|
|
|
2011-10-28 06:29:57 +02:00
|
|
|
public static boolean isRootPossible()
|
|
|
|
{
|
2011-06-28 05:42:52 +02:00
|
|
|
|
|
|
|
StringBuilder log = new StringBuilder();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
// Check if Superuser.apk exists
|
2012-01-14 01:02:55 +01:00
|
|
|
File fileSU = new File("/system/app/Superuser.apk");
|
|
|
|
if (fileSU.exists())
|
|
|
|
return true;
|
|
|
|
|
2012-06-20 02:45:25 +02:00
|
|
|
fileSU = new File("/system/app/superuser.apk");
|
2012-01-14 01:02:55 +01:00
|
|
|
if (fileSU.exists())
|
|
|
|
return true;
|
2011-06-28 05:42:52 +02:00
|
|
|
|
2012-06-20 02:45:25 +02:00
|
|
|
fileSU = new File("/system/bin/su");
|
|
|
|
if (fileSU.exists())
|
|
|
|
{
|
|
|
|
String[] cmd = {"su"};
|
|
|
|
int exitCode = TorServiceUtils.doShellCommand(cmd, log, false, true);
|
|
|
|
if (exitCode != 0)
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-06-28 05:42:52 +02:00
|
|
|
//Check for 'su' binary
|
|
|
|
String[] cmd = {"which su"};
|
|
|
|
int exitCode = TorServiceUtils.doShellCommand(cmd, log, false, true);
|
|
|
|
|
2012-01-09 06:23:54 +01:00
|
|
|
if (exitCode == 0) {
|
2012-06-20 02:45:25 +02:00
|
|
|
TorService.logMessage("root exists, but not sure about permissions");
|
2011-06-28 05:42:52 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
//this means that there is no root to be had (normally) so we won't log anything
|
|
|
|
TorService.logException("Error checking for root access",e);
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
TorService.logException("Error checking for root access",e);
|
|
|
|
//this means that there is no root to be had (normally)
|
|
|
|
}
|
|
|
|
|
|
|
|
TorService.logMessage("Could not acquire root permissions");
|
2011-10-28 06:29:57 +02:00
|
|
|
|
|
|
|
|
2011-06-28 05:42:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
2010-02-28 00:56:46 +01:00
|
|
|
|
2010-02-08 21:39:42 +01:00
|
|
|
|
|
|
|
public static int findProcessId(String command)
|
|
|
|
{
|
2010-07-20 00:34:15 +02:00
|
|
|
int procId = -1;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
procId = findProcessIdWithPidOf(command);
|
|
|
|
|
|
|
|
if (procId == -1)
|
|
|
|
procId = findProcessIdWithPS(command);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
procId = findProcessIdWithPS(command);
|
|
|
|
}
|
|
|
|
catch (Exception e2)
|
|
|
|
{
|
2011-10-28 06:29:57 +02:00
|
|
|
Log.w(TorConstants.TAG,"Unable to get proc id for: " + command,e2);
|
2010-07-20 00:34:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return procId;
|
|
|
|
}
|
|
|
|
|
|
|
|
//use 'pidof' command
|
|
|
|
public static int findProcessIdWithPidOf(String command) throws Exception
|
|
|
|
{
|
2010-03-06 15:56:39 +01:00
|
|
|
|
2010-02-08 21:39:42 +01:00
|
|
|
int procId = -1;
|
|
|
|
|
|
|
|
Runtime r = Runtime.getRuntime();
|
|
|
|
|
|
|
|
Process procPs = null;
|
|
|
|
|
2011-02-09 22:11:53 +01:00
|
|
|
String baseName = new File(command).getName();
|
|
|
|
//fix contributed my mikos on 2010.12.10
|
|
|
|
procPs = r.exec(new String[] {SHELL_CMD_PIDOF, baseName});
|
|
|
|
//procPs = r.exec(SHELL_CMD_PIDOF);
|
2010-02-08 21:39:42 +01:00
|
|
|
|
2010-07-20 00:34:15 +02:00
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream()));
|
|
|
|
String line = null;
|
|
|
|
|
|
|
|
while ((line = reader.readLine())!=null)
|
|
|
|
{
|
2011-02-09 22:11:53 +01:00
|
|
|
|
|
|
|
try
|
2010-07-20 00:34:15 +02:00
|
|
|
{
|
|
|
|
//this line should just be the process id
|
|
|
|
procId = Integer.parseInt(line.trim());
|
|
|
|
break;
|
|
|
|
}
|
2011-02-09 22:11:53 +01:00
|
|
|
catch (NumberFormatException e)
|
|
|
|
{
|
2011-04-17 08:04:27 +02:00
|
|
|
TorService.logException("unable to parse process pid: " + line,e);
|
2011-02-09 22:11:53 +01:00
|
|
|
}
|
2010-07-20 00:34:15 +02:00
|
|
|
}
|
2010-02-08 21:39:42 +01:00
|
|
|
|
2010-07-20 00:34:15 +02:00
|
|
|
|
|
|
|
return procId;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//use 'ps' command
|
|
|
|
public static int findProcessIdWithPS(String command) throws Exception
|
|
|
|
{
|
|
|
|
|
|
|
|
int procId = -1;
|
|
|
|
|
|
|
|
Runtime r = Runtime.getRuntime();
|
|
|
|
|
|
|
|
Process procPs = null;
|
|
|
|
|
|
|
|
procPs = r.exec(SHELL_CMD_PS);
|
2010-02-08 21:39:42 +01:00
|
|
|
|
2010-07-20 00:34:15 +02:00
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream()));
|
|
|
|
String line = null;
|
|
|
|
|
|
|
|
while ((line = reader.readLine())!=null)
|
|
|
|
{
|
2011-02-09 22:11:53 +01:00
|
|
|
if (line.indexOf(' ' + command)!=-1)
|
2010-07-20 00:34:15 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
StringTokenizer st = new StringTokenizer(line," ");
|
|
|
|
st.nextToken(); //proc owner
|
|
|
|
|
|
|
|
procId = Integer.parseInt(st.nextToken().trim());
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2010-02-08 21:39:42 +01:00
|
|
|
}
|
|
|
|
|
2010-07-20 00:34:15 +02:00
|
|
|
|
|
|
|
|
2010-02-08 21:39:42 +01:00
|
|
|
return procId;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-28 06:43:58 +02:00
|
|
|
public static int doShellCommand(String[] cmds, StringBuilder log, boolean runAsRoot, boolean waitFor) throws Exception
|
2010-02-08 21:39:42 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
Process proc = null;
|
|
|
|
int exitCode = -1;
|
|
|
|
|
2012-01-17 04:54:42 +01:00
|
|
|
if (runAsRoot)
|
|
|
|
proc = Runtime.getRuntime().exec("su");
|
|
|
|
else
|
|
|
|
proc = Runtime.getRuntime().exec("sh");
|
2012-07-24 19:08:18 +02:00
|
|
|
|
2012-01-17 04:54:42 +01:00
|
|
|
OutputStreamWriter out = new OutputStreamWriter(proc.getOutputStream());
|
|
|
|
|
|
|
|
for (int i = 0; i < cmds.length; i++)
|
|
|
|
{
|
2012-02-12 07:25:02 +01:00
|
|
|
TorService.logMessage("executing shell cmd: " + cmds[i] + "; runAsRoot=" + runAsRoot + ";waitFor=" + waitFor);
|
|
|
|
|
2012-01-17 04:54:42 +01:00
|
|
|
out.write(cmds[i]);
|
|
|
|
out.write("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
out.flush();
|
|
|
|
out.write("exit\n");
|
|
|
|
out.flush();
|
|
|
|
|
|
|
|
if (waitFor)
|
|
|
|
{
|
|
|
|
|
|
|
|
final char buf[] = new char[10];
|
|
|
|
|
|
|
|
// Consume the "stdout"
|
|
|
|
InputStreamReader reader = new InputStreamReader(proc.getInputStream());
|
|
|
|
int read=0;
|
|
|
|
while ((read=reader.read(buf)) != -1) {
|
|
|
|
if (log != null) log.append(buf, 0, read);
|
2010-02-08 21:39:42 +01:00
|
|
|
}
|
2012-01-17 04:54:42 +01:00
|
|
|
|
|
|
|
// Consume the "stderr"
|
|
|
|
reader = new InputStreamReader(proc.getErrorStream());
|
|
|
|
read=0;
|
|
|
|
while ((read=reader.read(buf)) != -1) {
|
|
|
|
if (log != null) log.append(buf, 0, read);
|
|
|
|
}
|
|
|
|
|
|
|
|
exitCode = proc.waitFor();
|
|
|
|
|
|
|
|
}
|
2010-07-28 06:43:58 +02:00
|
|
|
|
2010-02-08 21:39:42 +01:00
|
|
|
|
|
|
|
return exitCode;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|