summaryrefslogtreecommitdiff
path: root/noncore/settings/usermanager/userdialog.cpp
blob: 365463935e1d9a22710002f21a8467777d7b2ea8 (plain)
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/***************************************************************************
 *                                                                         *
 *   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 "userdialog.h"
#include "passwd.h"

/* OPIE */
#include <opie2/odebug.h>
#include <opie2/odevice.h>
#include <opie2/oresource.h>
#include <qpe/qpeapplication.h>
using namespace Opie::Core;
using namespace Opie::Ui;

/* QT */
#include <qlayout.h>
#include <qlabel.h>
#include <qmessagebox.h>
#include <qfile.h>

/* STD */
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>

/**
 * UserDialog constructor. Setup the dialog, fill the groupComboBox & groupsListView with all groups.
 *
 */
UserDialog::UserDialog(int viewmode, QWidget* parent, const char* name, bool modal, WFlags fl) : QDialog(parent, name, modal, fl)
{
    vm=viewmode;
    QVBoxLayout *layout = new QVBoxLayout(this);
    myTabWidget=new QTabWidget(this,"User Tab Widget");
    layout->addWidget(myTabWidget);
    setupTab1();
    setupTab2();

    accounts->groupStringList.sort();
    // And also fill the listview & the combobox with all available groups.
    for( QStringList::Iterator it = accounts->groupStringList.begin(); it!=accounts->groupStringList.end(); ++it)
    {
        accounts->splitGroupEntry(*it);
        if(accounts->gr_name.find(QRegExp("^#"),0))
        {    // Skip commented lines.
            new QCheckListItem(groupsListView,accounts->gr_name,QCheckListItem::CheckBox);
            groupComboBox->insertItem(accounts->gr_name);
        }
    }
    QPEApplication::showDialog( this );
}

/**
 * Empty destructor.
 *
 */
UserDialog::~UserDialog()
{}

/**
 *  Creates the first tab, all userinfo is here.
 *
 */
void UserDialog::setupTab1()
{
    QPixmap mypixmap;
    QWidget *tabpage = new QWidget(myTabWidget,"page1");
    QVBoxLayout *layout = new QVBoxLayout(tabpage);
    layout->setMargin(5);

    // Picture
    picturePushButton = new QPushButton(tabpage,"Label");
    picturePushButton->setMinimumSize(48,48);
    picturePushButton->setMaximumSize(48,48);
    picturePushButton->setPixmap(Opie::Core::OResource::loadPixmap("usermanager/usericon"));    // Load default usericon.
    connect(picturePushButton,SIGNAL(clicked()),this,SLOT(clickedPicture()));    // Clicking the picture should invoke pictureselector.

    // Login
    QLabel *loginLabel=new QLabel(tabpage,"Login: ");
    loginLabel->setText("Login: ");
    loginLineEdit=new QLineEdit(tabpage,"Login: ");

    // UID
    QLabel *uidLabel=new QLabel(tabpage,"uid: ");
    uidLabel->setText("UserID: ");
    uidLineEdit=new QLineEdit(tabpage,"uid: ");
    uidLineEdit->setEnabled(false);

    // Username (gecos)
    QLabel *gecosLabel=new QLabel(tabpage,"gecos");
    gecosLabel->setText("Username: ");
    gecosLineEdit=new QLineEdit(tabpage,"gecos");

    // Password
    QLabel *passwordLabel=new QLabel(tabpage,"password");
    passwordLabel->setText("Password: ");
    passwordLineEdit=new QLineEdit(tabpage,"password");
    passwordLineEdit->setEchoMode(QLineEdit::Password);

    // Shell
    QLabel *shellLabel=new QLabel(tabpage,"shell");
    shellLabel->setText("Shell: ");
    shellComboBox=new QComboBox(tabpage,"shell");
    shellComboBox->setEditable(true);
    shellComboBox->insertItem("/bin/sh");
    shellComboBox->insertItem("/bin/ash");
    shellComboBox->insertItem("/bin/false");

    // Primary Group
    QLabel *groupLabel=new QLabel(tabpage,"group");
    groupLabel->setText("Primary group: ");
    groupComboBox=new QComboBox(tabpage,"PrimaryGroup");

    if(vm==VIEWMODE_NEW)
    {
        // Copy /etc/skel
        skelLabel=new QLabel(tabpage,"skel");
        skelLabel->setText("Copy /etc/skel: ");
        skelCheckBox=new QCheckBox(tabpage);
        skelCheckBox->setChecked(true);
    }

    // Widget layout
    QHBoxLayout *hlayout=new QHBoxLayout(-1,"hlayout");
    layout->addWidget(picturePushButton);
    layout->addSpacing(5);
    layout->addLayout(hlayout);
    QVBoxLayout *vlayout1=new QVBoxLayout(-1,"vlayout1");
    QVBoxLayout *vlayout2=new QVBoxLayout(-1,"vlayout2");
    // First column, labels
    vlayout1->addWidget(loginLabel);
    vlayout1->addSpacing(5);
    vlayout1->addWidget(uidLabel);
    vlayout1->addSpacing(5);
    vlayout1->addWidget(gecosLabel);
    vlayout1->addSpacing(5);
    vlayout1->addWidget(passwordLabel);
    vlayout1->addSpacing(5);
    vlayout1->addWidget(shellLabel);
    vlayout1->addSpacing(5);
    vlayout1->addWidget(groupLabel);
    if(vm==VIEWMODE_NEW)
    {
        vlayout1->addSpacing(5);
        vlayout1->addWidget(skelLabel);
    }
    // Second column, data
    vlayout2->addWidget(loginLineEdit);
    vlayout2->addSpacing(5);
    vlayout2->addWidget(uidLineEdit);
    vlayout2->addSpacing(5);
    vlayout2->addWidget(gecosLineEdit);
    vlayout2->addSpacing(5);
    vlayout2->addWidget(passwordLineEdit);
    vlayout2->addSpacing(5);
    vlayout2->addWidget(shellComboBox);
    vlayout2->addSpacing(5);
    vlayout2->addWidget(groupComboBox);
    if(vm==VIEWMODE_NEW)
    {
        vlayout2->addSpacing(5);
        vlayout2->addWidget(skelCheckBox);
    }
    hlayout->addLayout(vlayout1);
    hlayout->addLayout(vlayout2);

    myTabWidget->addTab(tabpage,"User Info");
}

/**
 *  Creates the second tab containing additional groups for the user.
 *
 */
void UserDialog::setupTab2()
{
    QWidget *tabpage = new QWidget(myTabWidget,"page2");
    QVBoxLayout *layout = new QVBoxLayout(tabpage);
    layout->setMargin(5);

    // Additional groups
    groupsListView=new QListView(tabpage,"groups");
    groupsListView->addColumn("Additional groups");
    groupsListView->setColumnWidthMode(0,QListView::Maximum);
    groupsListView->setMultiSelection(false);
    groupsListView->setAllColumnsShowFocus(false);

    layout->addSpacing(5);
    // Grouplist
    layout->addWidget(groupsListView);

    myTabWidget->addTab(tabpage,"User Groups");
}

/**
 * Static function that creates the userinfo dialog.
 * The user will be prompted to add a user.
 *
 * @param uid This is a suggested available UID.
 * @param gid This is a suggested available GID.
 *
 * @return <code>true</code> if the user was successfully added, otherwise <code>false</code>.
 *
 */
bool UserDialog::addUser(int uid, int gid)
{
    QCheckListItem *temp;
    QFile ozTest;
    int oz=false;
    if(ODevice::inst()->system()==System_OpenZaurus) oz=true;
    // viewmode is a workaround for a bug in qte-2.3.4 that gives bus error on manipulating adduserDialog's widgets here.
    UserDialog *adduserDialog=new UserDialog(VIEWMODE_NEW);
    adduserDialog->setCaption(tr("Add User"));
    adduserDialog->userID=uid;    // Set next available UID as default uid.
    adduserDialog->groupID=gid;    // Set next available GID as default gid.
    // Insert default group into groupComboBox
    adduserDialog->groupComboBox->insertItem("<create new group>",0);
    adduserDialog->uidLineEdit->setText(QString::number(uid));
    // If we're running on OZ, add new users to some default groups.
    if(oz)
    {
        QListViewItemIterator iter( adduserDialog->groupsListView );
        for ( ; iter.current(); ++iter )
        {
            temp=(QCheckListItem*)iter.current();
            if (temp->text()=="video") temp->setOn(true);
            if (temp->text()=="audio") temp->setOn(true);
            if (temp->text()=="time") temp->setOn(true);
            if (temp->text()=="power") temp->setOn(true);
            if (temp->text()=="input") temp->setOn(true);
            if (temp->text()=="sharp") temp->setOn(true);
            if (temp->text()=="tty") temp->setOn(true);
        }
    }
    // Show the dialog!
    if(!(adduserDialog->exec())) return false;
    if((adduserDialog->groupComboBox->currentItem()!=0))
    {
        accounts->findGroup(adduserDialog->groupComboBox->currentText());
        adduserDialog->groupID=accounts->gr_gid;
        owarn << QString::number(accounts->gr_gid) << oendl; 
    }
    if(!(accounts->addUser(adduserDialog->loginLineEdit->text(), adduserDialog->passwordLineEdit->text(),
                           adduserDialog->uidLineEdit->text().toInt(), adduserDialog->groupID, adduserDialog->gecosLineEdit->text(),
                           QString("/home/")+adduserDialog->loginLineEdit->text() , adduserDialog->shellComboBox->currentText())))
    {
        QMessageBox::information(0,"Ooops!","Something went wrong!\nUnable to add user.");
        return false;
    }

    // Add User to additional groups.
    QListViewItemIterator it( adduserDialog->groupsListView );
    for ( ; it.current(); ++it )
    {
        temp=(QCheckListItem*)it.current();
        if (temp->isOn() )
            accounts->addGroupMember(it.current()->text(0),adduserDialog->loginLineEdit->text());
    }
    // Copy image to pics/users/
    if(!(adduserDialog->userImage.isNull()))
    {
        QDir d;
        if(!(d.exists(QPEApplication::qpeDir() + "pics/users")))
        {
            d.mkdir(QPEApplication::qpeDir() + "pics/users");
        }
        QString filename= QPEApplication::qpeDir()+"pics/users/"+accounts->pw_name+".png";
        //        adduserDialog->userImage=adduserDialog->userImage.smoothScale(48,48);
        adduserDialog->userImage.save(filename,"PNG");
    }

    // Should we copy the skeleton homedirectory /etc/skel to the user's homedirectory?
    accounts->findUser(adduserDialog->loginLineEdit->text());
    if(adduserDialog->skelCheckBox->isChecked())
    {
        QString command_cp;
        QString command_chown;
        command_cp.sprintf("cp -a /etc/skel/* %s/",accounts->pw_dir.latin1());
        system(command_cp);

        command_cp.sprintf("cp -a /etc/skel/.[!.]* %s/",accounts->pw_dir.latin1());    // Bug in busybox, ".*" includes parent directory, does this work as a workaround?
        system(command_cp);

        command_chown.sprintf("chown -R %d:%d %s",accounts->pw_uid,accounts->pw_gid,accounts->pw_dir.latin1());
        system(command_chown);
    }

    return true;
}

/**
 * Deletes the user account.
 *
 * @param username User to be deleted.
 *
 * @return <code>true</code> if the user was successfully deleted, otherwise <code>false</code>.
 *
 */
bool UserDialog::delUser(const char *username)
{
    if((accounts->findUser(username)))
    {    // Does that user exist?
        if(!(accounts->delUser(username)))
        {    // Delete the user.
            QMessageBox::information(0,"Ooops!","Something went wrong\nUnable to delete user: "+QString(username)+".");
        }
    }
    else
    {
        QMessageBox::information(0,"Invalid Username","That username ("+QString(username)+")does not exist.");
        return false;
    }
    return true;
}

/**
 * This displays a confirmation dialog wether a user should be deleted or not.
 *  (And also deletes the account)
 *
 * @param username User to be deleted.
 *
 * @return <code>true</code> if the user was successfully deleted, otherwise <code>false</code>.
 *
 */
bool UserDialog::editUser(const char *username)
{
    int invalid_group=0;
    // viewmode is a workaround for a bug in qte-2.3.4 that gives bus error on manipulating edituserDialog's widgets here.
    UserDialog *edituserDialog=new UserDialog(VIEWMODE_EDIT);    // Create Dialog
    edituserDialog->setCaption(tr("Edit User"));
    accounts->findUser(username);    // Locate user in database and fill variables in 'accounts' object.
    if(!(accounts->findGroup(accounts->pw_gid)))
    {    // Locate the user's primary group, and fill group variables in 'accounts' object.
        invalid_group=1;
    }
    // Fill widgets with userinfo.
    edituserDialog->loginLineEdit->setText(accounts->pw_name);
    edituserDialog->uidLineEdit->setText(QString::number(accounts->pw_uid));
    edituserDialog->gecosLineEdit->setText(accounts->pw_gecos);
    // Set password to '........', we will later check if this still is the contents, if not, the password has been changed.
    edituserDialog->passwordLineEdit->setText("........");
    // If this user is not using /bin/sh,/bin/ash or /bin/false as shell, add that entry to the shell-combobox.
    if(accounts->pw_shell!="/bin/sh" && accounts->pw_shell!="/bin/ash" && accounts->pw_shell!="/bin/false")
    {
        edituserDialog->shellComboBox->insertItem(accounts->pw_shell,0);
        edituserDialog->shellComboBox->setCurrentItem(0);
    }
    // Select the primary group for this user.
    for(int i=0;i<edituserDialog->groupComboBox->count();++i)
    {
        if(accounts->gr_name==edituserDialog->groupComboBox->text(i))
        {
            edituserDialog->groupComboBox->setCurrentItem(i);
            break;
        }
    }
    if(invalid_group)
    {
        edituserDialog->groupComboBox->insertItem("<Undefined group>",0);
        edituserDialog->groupComboBox->setCurrentItem(0);
    }

    // Select the groups in the listview, to which the user belongs.
    QCheckListItem *temp;
    // BAH!!! QRegExp in qt2 sucks... or maybe I do... can't figure out how to check for EITHER end of input ($) OR a comma, so here we do two different QRegExps instead.
    QRegExp userRegExp(QString("[:,]%1$").arg(username));    // The end of line variant.
    QStringList tempList=accounts->groupStringList.grep(userRegExp);    // Find all entries in the group database, that the user is a member of.
    for(QStringList::Iterator it=tempList.begin(); it!=tempList.end(); ++it)
    {    // Iterate over all of them.
        owarn << *it << oendl; 
        QListViewItemIterator lvit( edituserDialog->groupsListView );    // Compare to all groups.
        for ( ; lvit.current(); ++lvit )
        {
            if(lvit.current()->text(0)==(*it).left((*it).find(":")))
            {
                temp=(QCheckListItem*)lvit.current();
                temp->setOn(true);    // If we find a line with that groupname, select it.;
            }
        }
    }
    userRegExp=QRegExp(QString("[:,]%1,").arg(username));    // And the other one. (not end of line.)
    tempList=accounts->groupStringList.grep(userRegExp);    // Find all entries in the group database, that the user is a member of.
    for(QStringList::Iterator it=tempList.begin(); it!=tempList.end(); ++it)
    {    // Iterate over all of them.
        owarn << *it << oendl; 
        QListViewItemIterator lvit( edituserDialog->groupsListView );    // Compare to all groups.
        for ( ; lvit.current(); ++lvit )
        {
            if(lvit.current()->text(0)==(*it).left((*it).find(":")))
            {
                temp=(QCheckListItem*)lvit.current();
                temp->setOn(true);    // If we find a line with that groupname, select it.;
            }
        }
    }

    if(!(edituserDialog->exec())) return false;    // SHOW THE DIALOG!

    accounts->findUser(username);    // Fill user variables in 'acccounts' object.
    accounts->pw_name=edituserDialog->loginLineEdit->text();
    // Has the password been changed ? Make a new "crypt":ed password.
    if(edituserDialog->passwordLineEdit->text()!="........") accounts->pw_passwd=crypt(edituserDialog->passwordLineEdit->text(), accounts->crypt_make_salt());

    // Set all variables in accounts object, that will be used when calling 'updateUser()'
    accounts->pw_uid=edituserDialog->uidLineEdit->text().toInt();
    if(accounts->findGroup(edituserDialog->groupComboBox->currentText()))
    {    // Fill all group variables in 'accounts' object.
        accounts->pw_gid=accounts->gr_gid;    // Only do this if the group is a valid group (ie. "<Undefined group>"), otherwise keep the old group.
    }
    accounts->pw_gecos=edituserDialog->gecosLineEdit->text();
    accounts->pw_shell=edituserDialog->shellComboBox->currentText();
    // Update userinfo, using the information stored in the user variables stored in the accounts object.
    accounts->updateUser(username);

    // Remove user from all groups he/she is a member of. (could be done in a better way I guess, this was simple though.)
    for(QStringList::Iterator it=tempList.begin(); it!=tempList.end(); ++it)
    {
        accounts->delGroupMember((*it).left((*it).find(":")),username);
    }

    // Add User to additional groups that he/she is a member of.
    QListViewItemIterator it( edituserDialog->groupsListView );
    for ( ; it.current(); ++it )
    {
        temp=(QCheckListItem*)it.current();
        if ( temp->isOn() )
            accounts->addGroupMember(it.current()->text(0),edituserDialog->loginLineEdit->text());
    }

    // Copy image to pics/users/
    if(!(edituserDialog->userImage.isNull()))
    {
        QDir d;
        if(!(d.exists(QPEApplication::qpeDir()+"pics/users")))
        {
            d.mkdir(QPEApplication::qpeDir()+"pics/users");
        }
        QString filename=QPEApplication::qpeDir()+"pics/users/"+accounts->pw_name+".png";
        //        edituserDialog->userImage=edituserDialog->userImage.smoothScale(48,48);
        edituserDialog->userImage.save(filename,"PNG");
    }
    return true;
}

/**
 * "OK" has been clicked. Verify some information before closing the dialog.
 *
 */
void UserDialog::accept()
{
    // Add checking... valid username? username taken?
    if(loginLineEdit->text().isEmpty())
    {
        QMessageBox::information(0,"Empty Login","Please enter a login.");
        return;
    }
    QDialog::accept();
}

/**
 * This slot is called when the usericon is clicked, this loads (should) the iconselector.
 *
 */
void UserDialog::clickedPicture()
{
    QString filename=OFileDialog::getOpenFileName(OFileSelector::EXTENDED, QString::null);
    if(!(filename.isEmpty()))
    {
        userImage.reset();
        if(!(userImage.load(filename)))
        {
            QMessageBox::information(0,"Sorry!","That icon could not be loaded.\nLoading failed on: "+filename);
        }
        else
        {
            //            userImage=userImage.smoothScale(48,48);
            QPixmap *picture;
            picture=(QPixmap *)picturePushButton->pixmap();
            picture->convertFromImage(userImage,0);
            picturePushButton->update();
        }
    }
}