All pastes #624624 Raw Edit

Untitled

public text v1 · immutable
·published 2007-07-18 12:35 UTC
rendered paste body
/***************************************************************************
                          kdesudo.cpp  -  description
                             -------------------
    begin                : Sam Feb 15 15:42:12 CET 2003
    copyright            : (C) 2003 by Robert Gruber <rgruber@users.sourceforge.net>
                           (C) 2007 by Kubuntu-KdeSudo Team

 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "kdesudo.h"

#include <klocale.h>
#include <kmessagebox.h>
#include <kpushbutton.h>
#include <kpassdlg.h>
#include <kdesu/kcookie.h>
#include <kstandarddirs.h>
#include <qfile.h>
#include <stdlib.h>

KdeSudo::KdeSudo(QWidget *parent, const char *name, const QString& cmd, const QString& runas, const QString& comment,const QString& icon, const QString& generic, bool withIgnoreButton, bool newDcop) 
  : KPasswordDialog(KPasswordDialog::Password, false, (withIgnoreButton ? User1: false), icon, parent, name)
{
  QString defaultComment = i18n("<b>%1</b> needs administrative privileges. Please enter your password for verification.");
  p=NULL;
  bNeedPasswd=false;
  bError=false;

  if (comment.isEmpty()) {
    if (! generic.isEmpty()) {
      setPrompt(defaultComment.arg(generic));
    }
    else {
      setPrompt(defaultComment.arg(cmd));
    }
  }
  else {
  setPrompt(comment);
  }

  if( withIgnoreButton )
    setButtonText(User1, i18n("&Ignore"));
	    
  QString command = cmd;
  /* vExecCommand */
  p = new KShellProcess;
  /* dcop */
  if(newDcop == true) {
    int fd;
//    int oldumask = umask(077);
    cookie = new KCookie();
    char iceauthority[] = "/tmp/iceauth.XXXXXXXXXX";


    QCString ksycoca = QFile::encodeName(locateLocal("cache", "ksycoca"));
    p->setEnvironment("KDESYCOCA",ksycoca);
    p->setEnvironment("DCOPSERVER",cookie->dcopServer());
    fd = mkstemp(iceauthority);
//    umask(oldumask);
    close(fd);
    p->setEnvironment("ICEAUTHORITY",iceauthority);
  }
  p->clearArguments();
  if (runas.isEmpty())
    *p << "sudo -S -p passprompt" <<  cmd;
  else
    *p << "sudo -S -p passprompt -u " << runas << ' ' <<  cmd;
  connect( p, SIGNAL(receivedStdout(KProcess*, char*, int)), this, SLOT(receivedOut(KProcess*, char*, int)) );
  connect( p, SIGNAL(receivedStderr(KProcess*, char*, int)), this, SLOT(receivedOut(KProcess*, char*, int)) );
  connect( p, SIGNAL(processExited (KProcess *)), this, SLOT(procExited(KProcess*)));
  p->start( KProcess::NotifyOnExit, KProcess::All );

}

KdeSudo::~KdeSudo()
{
}

void KdeSudo::receivedOut(KProcess*proc, char*buffer, int buflen)
{
  char *pcTmp= new char[buflen+1];
  strncpy(pcTmp,buffer,buflen);
  pcTmp[buflen]='\0';
  QString strOut(pcTmp);
  if (strOut.find("passprompt")!=-1)
  {
    bNeedPasswd=true;
    this->show();
  }else
  if (strOut.find("Sorry, try again")!=-1)
  {
    this->show();
    KMessageBox::error(this, i18n("Wrong password, try again!"));
  }else
  if (strOut.find("command not found")!=-1)
  {
    bError=true;
    KMessageBox::error(this, i18n("Command not found!"));
    kapp->quit();
  }else
  if (strOut.find("is not in the sudoers file")!=-1)
  {
    bError=true;
    KMessageBox::error(this, i18n("Your username is unknown to sudo!"));
    kapp->quit();
  }else
  if (strOut.find("is not allowed to execute")!=-1)
  {
    bError=true;
    KMessageBox::error(this, i18n("Your user is not allowed to run the specified command!"));
    kapp->quit();
  }
  if (strOut.find("is not allowed to run sudo on")!=-1)
  {
    bError=true;
    KMessageBox::error(this, i18n("Your user is not allowed to run sudo on this host!"));
    kapp->quit();
  }
  if (strOut.find("may not run sudo on")!=-1)
  {
    bError=true;
    KMessageBox::error(this, i18n("Your user is not allowed to run sudo on this host!"));
    kapp->quit();
  }

  //dump the output to stderr  
  fprintf(stderr,"%s\n",strOut.ascii());
}

void KdeSudo::procExited(KProcess *proc)
{
//DEBUG  KMessageBox::information(this, "Proc exited!");
  fprintf(stdout, "Proc ended! bError==%d\n", (bError)?1:0);
  if (!bError)
    kapp->quit();
}

void KdeSudo::slotOk()
{
  QString strTmp(password());
  strTmp+="\n";
  p->writeStdin(strTmp.ascii(),(int)strTmp.length());
  this->hide();
}

void KdeSudo::slotUser1()
{
    done(AsUser);
}