File: su.cpp

package info (click to toggle)
kdelibs 4:3.5.10.dfsg.1-5+deb6u1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 98,764 kB
  • ctags: 76,783
  • sloc: cpp: 578,944; xml: 117,726; ansic: 28,321; sh: 11,188; perl: 6,290; java: 4,069; makefile: 3,795; yacc: 2,437; lex: 643; ruby: 329; asm: 166; jsp: 128; haskell: 116; f90: 99; ml: 75; awk: 71; tcl: 29; lisp: 24; php: 9
file content (342 lines) | stat: -rw-r--r-- 9,282 bytes parent folder | download | duplicates (5)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* vi: ts=8 sts=4 sw=4
*
* $Id: su.cpp 592751 2006-10-05 16:22:44Z coolo $
*
* This file is part of the KDE project, module kdesu.
* Copyright (C) 1999,2000 Geert Jansen <jansen@kde.org>
* 
* Sudo support added by Jonathan Riddell <jriddell@ ubuntu.com>
* Copyright (C) 2005 Canonical Ltd
*
* This is free software; you can use this library under the GNU Library 
* General Public License, version 2. See the file "COPYING.LIB" for the 
* exact licensing terms.
*
* su.cpp: Execute a program as another user with "class SuProcess".
*/

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>

#include <sys/types.h>
#include <sys/stat.h>

#include <qglobal.h>
#include <qcstring.h>
#include <qfile.h>

#include <kconfig.h>
#include <kdebug.h>
#include <klocale.h>
#include <kstandarddirs.h>

#include "su.h"
#include "kcookie.h"


#ifndef __PATH_SU
#define __PATH_SU "false"
#endif

#ifndef __PATH_SUDO
#define __PATH_SUDO "false"
#endif

SuProcess::SuProcess(const QCString &user, const QCString &command)
{
    m_User = user;
    m_Command = command;

    KConfig* config = KGlobal::config();
    config->setGroup("super-user-command");
    superUserCommand = config->readEntry("super-user-command", DEFAULT_SUPER_USER_COMMAND);
    if ( superUserCommand != "sudo" && superUserCommand != "su" ) {
	kdWarning() << "unknown super user command" << endl;
	superUserCommand = "su";
    }
}


SuProcess::~SuProcess()
{
}

int SuProcess::checkInstall(const char *password)
{
    return exec(password, Install);
}

int SuProcess::checkNeedPassword()
{
    return exec(0L, NeedPassword);
}

/*
* Execute a command with su(1).
*/

int SuProcess::exec(const char *password, int check)
{
    if (check)
        setTerminal(true);

    // since user may change after constructor (due to setUser())
    // we need to override sudo with su for non-root here
    if (m_User != "root") {
        superUserCommand = "su";
    }

    QCStringList args;
    if (superUserCommand == "sudo") {
        args += "-u";
    }

#ifdef Q_OS_DARWIN
    args += "-c";
    args += "staff";
#endif

    if ((m_Scheduler != SchedNormal) || (m_Priority > 50))
        args += "root";
    else
        args += m_User;
    
    if (superUserCommand == "su") {
        args += "-c";
    }
    args += QCString(__KDE_BINDIR) + "/kdesu_stub";
#ifndef Q_OS_DARWIN
    args += "-";
#endif

    ///    QCString command = __PATH_SU;
    ///    if (::access(__PATH_SU, X_OK) != 0)
    QCString command;
    if (superUserCommand == "sudo") {
        command = __PATH_SUDO;
    } else {
        command = __PATH_SU;
    }
 
    if (::access(command, X_OK) != 0)
    {
      ///        command = QFile::encodeName(KGlobal::dirs()->findExe("su"));
        command = QFile::encodeName( KGlobal::dirs()->findExe(superUserCommand.ascii()) );
        if (command.isEmpty())
            return check ? SuNotFound : -1;
    }

    // kdDebug(900) << k_lineinfo << "Call StubProcess::exec()" << endl;
    if (StubProcess::exec(command, args) < 0)
    {
        return check ? SuNotFound : -1;
    }
    // kdDebug(900) << k_lineinfo << "Done StubProcess::exec()" << endl;

    SuErrors ret = (SuErrors) ConverseSU(password);
    // kdDebug(900) << k_lineinfo << "Conversation returned " << ret << endl;

    if (ret == error)
    {
        if (!check)
            kdError(900) << k_lineinfo << "Conversation with " << superUserCommand << " failed\n";
        return ret;
    }
    if (check == NeedPassword)
    {
        if (ret == killme)
        {
	  ///            if (kill(m_Pid, SIGKILL) < 0)
	  ///            {
	  ///                ret=error;
	  ///            }
            if ( superUserCommand == "sudo" ) {
 	        // sudo can not be killed, just return
 	        return ret;
 	    }
 	    if (kill(m_Pid, SIGKILL) < 0) {
 	        kdDebug() << k_funcinfo << "kill < 0" << endl;
 		//FIXME SIGKILL doesn't work for sudo,
 		//why is this different from su?
 		ret=error;
 	    }
            else
            {
                int iret = waitForChild();
                if (iret < 0) ret=error;
                else /* nothing */ {} ;
            }
        }
        return ret;
    }

    if (m_bErase && password) 
    {
        char *ptr = const_cast<char *>(password);
        const uint plen = strlen(password);
        for (unsigned i=0; i < plen; i++)
            ptr[i] = '\000';
    }

    if (ret == notauthorized)
    {
        kill(m_Pid, SIGKILL);
        if (superUserCommand != "sudo") {
            waitForChild();
        }
        return SuIncorrectPassword;
    }

    int iret = ConverseStub(check);
    if (iret < 0)
    {
        if (!check)
            kdError(900) << k_lineinfo << "Converstation with kdesu_stub failed\n";
        return iret;
    }
    else if (iret == 1)
    {
        kill(m_Pid, SIGKILL);
        waitForChild();
        return SuIncorrectPassword;
    }

    if (check == Install)
    {
        waitForChild();
        return 0;
    }

    iret = waitForChild();
    return iret;
}

/*
* Conversation with su: feed the password.
* Return values: -1 = error, 0 = ok, 1 = kill me, 2 not authorized
*/

int SuProcess::ConverseSU(const char *password)
{	
    enum { WaitForPrompt, CheckStar, HandleStub } state = WaitForPrompt;
    int colon;
    unsigned i, j;
    // kdDebug(900) << k_lineinfo << "ConverseSU starting." << endl;

    QCString line;
    while (true)
    {
        line = readLine();
        if (line.isNull())
            return ( state == HandleStub ? notauthorized : error);
        kdDebug(900) << k_lineinfo << "Read line <" << line << ">" << endl;

        switch (state) 
        {
            //////////////////////////////////////////////////////////////////////////
            case WaitForPrompt:
            {
                // In case no password is needed.
                if (line == "kdesu_stub")
                {
                    unreadLine(line);
                    return ok;
                }
    
                while(waitMS(m_Fd,100)>0)
                {
                    // There is more output available, so the previous line
                    // couldn't have been a password prompt (the definition
                    // of prompt being that  there's a line of output followed 
                    // by a colon, and then the process waits).
                    QCString more = readLine();
                    if (more.isEmpty())
                        break;
    
                    line = more;
                    kdDebug(900) << k_lineinfo << "Read line <" << more << ">" << endl;
                }
    
                // Match "Password: " with the regex ^[^:]+:[\w]*$.
                const uint len = line.length();
                for (i=0,j=0,colon=0; i<len; i++) 
                {
                    if (line[i] == ':') 
                    {
                        j = i; colon++;
                        continue;
                    }
                    if (!isspace(line[i]))
                        j++;
                }
                if ((colon == 1) && (line[j] == ':')) 
                {
                    if (password == 0L)
                        return killme;
                    if (!checkPid(m_Pid))
                    {
                        kdError(900) << superUserCommand << " has exited while waiting for pwd." << endl;
                        return error;
                    }
                    if ((WaitSlave() == 0) && checkPid(m_Pid))
                    {
                        write(m_Fd, password, strlen(password));
                        write(m_Fd, "\n", 1);
                        state=CheckStar;
                    }
                    else
                    {
                        return error;
                    }
                }
                break;
            }
            //////////////////////////////////////////////////////////////////////////
            case CheckStar:
            {
                QCString s = line.stripWhiteSpace();
                if (s.isEmpty()) 
                {
                    state=HandleStub;
                    break;
                }
                const uint len = line.length();
                for (i=0; i< len; i++)
                    {
                if (s[i] != '*')
                    return error;
                }
                state=HandleStub;
                break;
            }
            //////////////////////////////////////////////////////////////////////////
            case HandleStub:
                // Read till we get "kdesu_stub"
                if (line == "kdesu_stub")
                {
                    unreadLine(line);
                    return ok;
 	        } else if (superUserCommand == "sudo") {
 	            // sudo gives a "sorry" line so reaches here
 	            // with the wrong password
 	            return notauthorized;
                }
                break;
            //////////////////////////////////////////////////////////////////////////
        } // end switch
    } // end while (true)
    return ok;
}

void SuProcess::virtual_hook( int id, void* data )
{ StubProcess::virtual_hook( id, data ); }