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
|
/*
*
* $Id$
*
* This file is part of the KDE project, module tdesu.
* Copyright (C) 1999,2000 Geert Jansen <[email protected]>
*
* passwd.cpp: Change a user's password.
*/
#include <config.h> // setenv
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <pwd.h>
#include <signal.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <tqcstring.h>
#include <kdebug.h>
#include <kstandarddirs.h>
#include <tdesu/process.h>
#include "passwd.h"
PasswdProcess::PasswdProcess(TQCString user)
{
struct passwd *pw;
if (user.isEmpty())
{
pw = getpwuid(getuid());
if (pw == 0L)
{
kdDebug(1512) << "You don't exist!\n";
return;
}
m_User = pw->pw_name;
} else
{
pw = getpwnam(user);
if (pw == 0L)
{
kdDebug(1512) << k_lineinfo << "User " << user << "does not exist.\n";
return;
}
m_User = user;
}
bOtherUser = (pw->pw_uid != getuid());
}
PasswdProcess::~PasswdProcess()
{
}
int PasswdProcess::checkCurrent(const TQString &oldpass)
{
return exec(oldpass.local8Bit(), 0L, 1);
}
int PasswdProcess::exec(const char *oldpass, const char *newpass,
int check)
{
if (m_User.isEmpty())
return -1;
// if (check)
// setTerminal(true);
// Try to set the default locale to make the parsing of the output
// of `passwd' easier.
setenv("LANG","C", true /* override */);
QCStringList args;
if(bOtherUser)
args += m_User;
int ret = PtyProcess::exec("passwd", args);
if (ret < 0)
{
kdDebug(1512) << k_lineinfo << "Passwd not found!\n";
return PasswdNotFound;
}
ret = ConversePasswd(oldpass, newpass, check);
if (ret < 0)
kdDebug(1512) << k_lineinfo << "Conversation with passwd failed. pid = " << pid() << endl;
if ((waitForChild() != 0) && !check)
return PasswordNotGood;
return ret;
}
/*
* The tricky thing is to make this work with a lot of different passwd
* implementations. We _don't_ want implementation specific routines.
* Return values: -1 = unknown error, 0 = ok, >0 = error code.
*/
int PasswdProcess::ConversePasswd(const char *oldpass, const char *newpass,
int check)
{
TQCString line, errline;
int state = 0;
while (state != 7)
{
line = readLine();
if (line.isNull())
{
return -1;
}
if (state == 0 && isPrompt(line, "new"))
// If root is changing a user's password,
// passwd can't prompt for the original password.
// Therefore, we have to start at state=2.
state=2;
switch (state)
{
case 0:
// Eat garbage, wait for prompt
m_Error += line+"\n";
if (isPrompt(line, "password"))
{
WaitSlave();
write(m_Fd, oldpass, strlen(oldpass));
write(m_Fd, "\n", 1);
state++;
break;
}
if (m_bTerminal)
fputs(line, stdout);
break;
case 1: case 3: case 6:
// Wait for \n
if (line.isEmpty())
{
state++;
break;
}
// error
return -1;
case 2:
m_Error = "";
if( line.contains("again"))
{
m_Error = line;
kill(m_Pid, SIGKILL);
waitForChild();
return PasswordIncorrect;
}
// Wait for second prompt.
errline = line; // use first line for error message
while (!isPrompt(line, "new"))
{
line = readLine();
if (line.isNull())
{
// We didn't get the new prompt so assume incorrect password.
if (m_bTerminal)
fputs(errline, stdout);
m_Error = errline;
return PasswordIncorrect;
}
}
// we have the new prompt
if (check)
{
kill(m_Pid, SIGKILL);
waitForChild();
return 0;
}
WaitSlave();
write(m_Fd, newpass, strlen(newpass));
write(m_Fd, "\n", 1);
state++;
break;
case 4:
// Wait for third prompt
if (isPrompt(line, "re"))
{
WaitSlave();
write(m_Fd, newpass, strlen(newpass));
write(m_Fd, "\n", 1);
state += 2;
break;
}
// Warning or error about the new password
if (m_bTerminal)
fputs(line, stdout);
m_Error = line + "\n";
state++;
break;
case 5:
// Wait for either a "Reenter password" or a "Enter password" prompt
if (isPrompt(line, "re"))
{
WaitSlave();
write(m_Fd, newpass, strlen(newpass));
write(m_Fd, "\n", 1);
state++;
break;
}
else if (isPrompt(line, "password"))
{
kill(m_Pid, SIGKILL);
waitForChild();
return PasswordNotGood;
}
if (m_bTerminal)
fputs(line, stdout);
m_Error += line + "\n";
break;
}
}
// Are we ok or do we still get an error thrown at us?
m_Error = "";
state = 0;
while (state != 1)
{
line = readLine();
if (line.isNull())
{
// No more input... OK
return 0;
}
if (isPrompt(line, "password"))
{
// Uh oh, another prompt. Not good!
kill(m_Pid, SIGKILL);
waitForChild();
return PasswordNotGood;
}
m_Error += line + "\n"; // Collect error message
}
kdDebug(1512) << k_lineinfo << "Conversation ended successfully.\n";
return 0;
}
bool PasswdProcess::isPrompt(TQCString line, const char *word)
{
unsigned i, j, colon;
unsigned int lineLength(line.length());
for (i=0,j=0,colon=0; i<lineLength; i++)
{
if (line[i] == ':')
{
j = i; colon++;
continue;
}
if (!isspace(line[i]))
j++;
}
if ((colon != 1) || (line[j] != ':'))
return false;
if (word == 0L)
return true;
return line.contains(word, false);
}
|