29 #include <sys/types.h>
33 #include <tqcstring.h>
36 #include <tdeconfig.h>
38 #include <tdelocale.h>
39 #include <kstandarddirs.h>
46 #define __PATH_SU "false"
50 #define __PATH_SUDO "false"
53 SuProcess::SuProcess(
const TQCString &user,
const TQCString &command)
58 TDEConfig* config = TDEGlobal::config();
59 config->setGroup(
"super-user-command");
60 superUserCommand = config->readEntry(
"super-user-command", DEFAULT_SUPER_USER_COMMAND);
61 if ( superUserCommand !=
"sudo" && superUserCommand !=
"su" ) {
62 kdWarning() <<
"unknown super user command" << endl;
63 superUserCommand =
"su";
68 SuProcess::~SuProcess()
74 return exec(password, Install);
79 return exec(0L, NeedPassword);
93 if (m_User !=
"root") {
94 superUserCommand =
"su";
98 if (superUserCommand ==
"sudo") {
107 if ((m_Scheduler != SchedNormal) || (m_Priority > 50))
112 if (superUserCommand ==
"su") {
115 args += TQCString(__TDE_BINDIR) +
"/tdesu_stub";
123 if (superUserCommand ==
"sudo") {
124 command = __PATH_SUDO;
129 if (::access(command, X_OK) != 0)
132 command = TQFile::encodeName( TDEGlobal::dirs()->findExe(superUserCommand.ascii()) );
133 if (command.isEmpty())
134 return check ? SuNotFound : -1;
140 return check ? SuNotFound : -1;
144 SuErrors ret = (SuErrors) ConverseSU(password);
150 kdError(900) << k_lineinfo <<
"Conversation with " << superUserCommand <<
" failed\n";
153 if (check == NeedPassword)
161 if ( superUserCommand ==
"sudo" ) {
165 if (kill(m_Pid, SIGKILL) < 0) {
166 kdDebug() << k_funcinfo <<
"kill < 0" << endl;
174 if (iret < 0) ret=error;
181 if (m_bErase && password)
183 char *ptr =
const_cast<char *
>(password);
184 const uint plen = strlen(password);
185 for (
unsigned i=0; i < plen; i++)
189 if (ret == notauthorized)
191 kill(m_Pid, SIGKILL);
192 if (superUserCommand !=
"sudo") {
195 return SuIncorrectPassword;
202 kdError(900) << k_lineinfo <<
"Converstation with tdesu_stub failed\n";
207 kill(m_Pid, SIGKILL);
209 return SuIncorrectPassword;
212 if (check == Install)
227 int SuProcess::ConverseSU(
const char *password)
229 enum { WaitForPrompt, CheckStar, HandleStub } state = WaitForPrompt;
239 return ( state == HandleStub ? notauthorized : error);
240 kdDebug(900) << k_lineinfo <<
"Read line <" << line <<
">" << endl;
248 if (line ==
"tdesu_stub")
265 kdDebug(900) << k_lineinfo <<
"Read line <" << more <<
">" << endl;
269 const uint len = line.length();
270 for (i=0,j=0,colon=0; i<len; i++)
277 if (!isspace(line[i]))
280 if ((colon == 1) && (line[j] ==
':'))
286 kdError(900) << superUserCommand <<
" has exited while waiting for pwd." << endl;
291 write(m_Fd, password, strlen(password));
292 write(m_Fd,
"\n", 1);
305 TQCString s = line.stripWhiteSpace();
311 const uint len = line.length();
312 for (i=0; i< len; i++)
323 if (line ==
"tdesu_stub")
327 }
else if (superUserCommand ==
"sudo") {
330 return notauthorized;
339 void SuProcess::virtual_hook(
int id,
void* data )
340 { StubProcess::virtual_hook(
id, data ); }
static int waitMS(int fd, int ms)
Wait ms miliseconds (ie.
void setTerminal(bool terminal)
Enables/disables terminal output.
int waitForChild()
Waits for the child to exit.
static bool checkPid(pid_t pid)
Basic check for the existence of pid.
TQCString readLine(bool block=true)
Reads a line from the program's standard out.
int WaitSlave()
Waits until the pty has cleared the ECHO flag.
int exec(const TQCString &command, const QCStringList &args)
Forks off and execute a command.
void unreadLine(const TQCString &line, bool addNewline=true)
Puts back a line of input.
int ConverseStub(int check)
Exchange all parameters with tdesu_stub.
int checkNeedPassword()
Checks if a password is needed.
int checkInstall(const char *password)
Checks if the stub is installed and the password is correct.
int exec(const char *password, int check=NoCheck)