summaryrefslogtreecommitdiffstats
path: root/kuser/kuser.cpp
blob: 2ef2e80824aa901beba8b70d0e65972e88b93c2c (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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
/*
 *  Copyright (c) 1998 Denis Perchine <dyp@perchine.com>
 *  Copyright (c) 2004 Szombathelyi Gy├Ârgy <gyurco@freemail.hu>
 *  Former maintainer: Adriaan de Groot <groot@kde.org>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public
 *  License version 2 as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 **/

#include "globals.h"

#include <errno.h>
#include <sys/types.h>
#include <sys/file.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#include <fcntl.h>
#include <tqstring.h>
#include <tqdir.h>

#include "kglobal_.h"
#include "kuser.h"
#include "misc.h"
#include <kstandarddirs.h>
#include <kmessagebox.h>
#include <kprocess.h>
#include <kdebug.h>
#include <kio/netaccess.h>
#include <kurl.h>

// class KUser

KU::KUser::KUser()
{
  p_change = 0;
  p_expire = -1;
  p_uid     = 0;
  p_gid     = 100;

  s_min     = 0;
  s_max     = 99999;
  s_warn    = 7;
  s_inact   = -1;
//  s_flag    = 0;
  caps = 0;
  isCreateHome = false;
  isCreateMailBox = false;
  isCopySkel = false;
  isDeleteHome = false;
  isDeleteMailBox = false;

  isDisabled = true;
}

KU::KUser::KUser(const KU::KUser *user)
{
  copy(user);
}

void KU::KUser::copy(const KU::KUser *user)
{
  if ( user != this ) {
    caps = user->caps;
    p_name = user->p_name;
    p_surname = user->p_surname;
    p_email = user->p_email;
    p_pwd = user->p_pwd;
    p_dir = user->p_dir;
    p_shell = user->p_shell;
    p_fname = user->p_fname;
    p_office = user->p_office;
    p_ophone = user->p_ophone;
    p_hphone = user->p_hphone;
    p_class = user->p_class;
    p_change = user->p_change;
    p_expire = user->p_expire;
    p_office1 = user->p_office1;
    p_office2 = user->p_office2;
    p_address = user->p_address;

    p_uid     = user->p_uid;
    p_gid     = user->p_gid;

    s_pwd = user->s_pwd;
    s_min     = user->s_min;
    s_max     = user->s_max;
    s_warn    = user->s_warn;
    s_inact   = user->s_inact;
    s_flag    = user->s_flag;

    sam_lmpwd = user->sam_lmpwd;
    sam_ntpwd = user->sam_ntpwd;
    sam_loginscript = user->sam_loginscript;
    sam_profile = user->sam_profile;
    sam_homedrive = user->sam_homedrive;
    sam_homepath = user->sam_homepath;
    sam_workstations = user->sam_workstations;
    sam_domain = user->sam_domain;
    sid = user->sid;
    pgroup_sid = user->pgroup_sid;

    isCreateHome = user->isCreateHome;
    isCreateMailBox = user->isCreateMailBox;
    isDeleteHome = user->isDeleteHome;
    isDeleteMailBox = user->isDeleteMailBox;
    isCopySkel = user->isCopySkel;
    isDisabled = user->isDisabled;
  }
}

KU::KUser::~KUser()
{
}

void KU::KUser::setCaps( int data )
{
  caps = data;
}

int KU::KUser::getCaps()
{
  return caps;
}

bool KU::KUser::getDeleteHome()
{
  return isDeleteHome;
}

bool KU::KUser::getDeleteMailBox()
{
  return isDeleteMailBox;
}

bool KU::KUser::getCreateHome()
{
  return isCreateHome;
}

bool KU::KUser::getCreateMailBox()
{
  return isCreateMailBox;
}

bool KU::KUser::getCopySkel()
{
  return isCopySkel;
}

const TQString &KU::KUser::getName() const
{
  return p_name;
}

const TQString &KU::KUser::getSurname() const
{
  return p_surname;
}

const TQString &KU::KUser::getEmail() const
{
  return p_email;
}

const TQString &KU::KUser::getPwd() const
{
  return p_pwd;
}

const TQString &KU::KUser::getHomeDir() const
{
  return p_dir;
}

const TQString &KU::KUser::getShell() const
{
  return p_shell;
}

const TQString &KU::KUser::getFullName() const
{
  return p_fname;
}

bool KU::KUser::getDisabled() const
{
  return isDisabled;
}

// FreeBSD apparently uses the GECOS fields differently than other Unices.
// Create some better named functions to make the FreeBSD code clear
const TQString &KU::KUser::getOffice() const
{
  return p_office;
}

const TQString &KU::KUser::getWorkPhone() const
{
  return p_ophone;
}

const TQString &KU::KUser::getHomePhone() const
{
  return p_hphone;
}

// New fields needed for the FreeBSD /etc/master.passwd file
const TQString &KU::KUser::getClass() const
{
  return p_class;
}

const TQString &KU::KUser::getOffice1() const
{
  return p_office1;
}

const TQString &KU::KUser::getOffice2() const
{
  return p_office2;
}

const TQString &KU::KUser::getAddress() const
{
  return p_address;
}

uid_t KU::KUser::getUID() const
{
  return p_uid;
}

gid_t KU::KUser::getGID() const
{
  return p_gid;
}

const TQString &KU::KUser::getSPwd() const
{
  return s_pwd;
}

time_t KU::KUser::getLastChange() const
{
  return p_change;
}

int KU::KUser::getMin() const
{
  return s_min;
}

int KU::KUser::getMax() const
{
  return s_max;
}

int KU::KUser::getWarn() const
{
  return s_warn;
}

int KU::KUser::getInactive() const
{
  return s_inact;
}

int KU::KUser::getFlag() const
{
  return s_flag;
}

time_t KU::KUser::getExpire() const
{
  return p_expire;
}

const TQString &KU::KUser::getLMPwd() const //  sam_lmpwd,
{
  return sam_lmpwd;
}

const TQString &KU::KUser::getNTPwd() const //sam_ntpwd,
{
  return sam_ntpwd;
}

const TQString &KU::KUser::getLoginScript() const //sam_loginscript,
{
  return sam_loginscript;
}

const TQString &KU::KUser::getProfilePath() const //  sam_profile,
{
  return sam_profile;
}

const TQString &KU::KUser::getHomeDrive() const //sam_homedrive,
{
  return sam_homedrive;
}

const TQString &KU::KUser::getHomePath() const //sam_homepath;
{
  return sam_homepath;
}

const TQString &KU::KUser::getWorkstations() const //sam_workstation;
{
  return sam_workstations;
}

const TQString &KU::KUser::getDomain() const //sam_domain;
{
  return sam_domain;
}

const SID &KU::KUser::getSID() const //sid,
{
  return sid;
}

const SID &KU::KUser::getPGSID() const //pgroup_sid;
{
  return pgroup_sid;
}

void KU::KUser::setName(const TQString &data)
{
  p_name = data;
}

void KU::KUser::setSurname(const TQString &data)
{
  p_surname = data;
}

void KU::KUser::setEmail(const TQString &data)
{
  p_email = data;
}

void KU::KUser::setPwd(const TQString &data)
{
  p_pwd = data;
}

void KU::KUser::setHomeDir(const TQString &data)
{
  p_dir = data;
}

void KU::KUser::setShell(const TQString &data)
{
  p_shell = data;
}

void KU::KUser::setFullName(const TQString &data)
{
  p_fname = data;
}

void KU::KUser::setDisabled(bool data)
{
  isDisabled = data;
}

// FreeBSD apparently uses the GECOS fields differently than other Unices.
// Create some better named functions to make the FreeBSD code clear
void KU::KUser::setOffice(const TQString &data)
{
  p_office = data;
}

void KU::KUser::setWorkPhone(const TQString &data)
{
  p_ophone = data;
}

void KU::KUser::setHomePhone(const TQString &data)
{
  p_hphone = data;
}

// New fields needed for the FreeBSD /etc/master.passwd file
void KU::KUser::setClass(const TQString &data)
{
  p_class = data;
}

void KU::KUser::setLastChange(time_t data)
{
  p_change = data;
}

void KU::KUser::setExpire(time_t data)
{
  p_expire = data;
}

void KU::KUser::setOffice1(const TQString &data)
{
  p_office1 = data;
}

void KU::KUser::setOffice2(const TQString &data)
{
  p_office2 = data;
}

void KU::KUser::setAddress(const TQString &data)
{
  p_address = data;
}

void KU::KUser::setUID(uid_t data)
{
  p_uid = data;
}

void KU::KUser::setGID(gid_t data)
{
  p_gid = data;
}

void KU::KUser::setSPwd(const TQString &data)
{
  s_pwd = data;
}

void KU::KUser::setMin(int data)
{
  s_min = data;
}

void KU::KUser::setMax(int data)
{
  s_max = data;
}

void KU::KUser::setWarn(int data)
{
  s_warn = data;
}

void KU::KUser::setInactive(int data)
{
  s_inact = data;
}

void KU::KUser::setLMPwd( const TQString &data ) //  sam_lmpwd,
{
  sam_lmpwd = data;
}

void KU::KUser::setNTPwd( const TQString &data ) //sam_ntpwd,
{
  sam_ntpwd = data;
}

void KU::KUser::setLoginScript( const TQString &data ) //sam_loginscript,
{
  sam_loginscript = data;
}

void KU::KUser::setProfilePath( const TQString &data) //  sam_profile,
{
  sam_profile = data;
}

void KU::KUser::setHomeDrive( const TQString &data ) //sam_homedrive,
{
  sam_homedrive = data;
}

void KU::KUser::setHomePath( const TQString &data ) //sam_homepath;
{
  sam_homepath = data;
}

void KU::KUser::setWorkstations( const TQString &data ) //sam_workstation;
{
  sam_workstations = data;
}

void KU::KUser::setDomain( const TQString &data ) //sam_domain
{
  sam_domain = data;
}

void KU::KUser::setSID( const SID &data ) //sid,
{
  sid = data;
}

void KU::KUser::setPGSID( const SID &data ) //pgroup_sid;
{
  pgroup_sid = data;
}

void KU::KUser::setFlag(int data)
{
  s_flag = data;
}

void KU::KUser::setCreateHome(bool data)
{
  isCreateHome = data;
}

void KU::KUser::setCreateMailBox(bool data)
{
  isCreateMailBox = data;
}

void KU::KUser::setCopySkel(bool data)
{
  isCopySkel = data;
}

void KU::KUser::setDeleteHome(bool data)
{
  isDeleteHome = data;
}

void KU::KUser::setDeleteMailBox(bool data)
{
  isDeleteMailBox = data;
}

int KU::KUser::createHome()
{

  if(p_dir.isNull() || p_dir.isEmpty()) {
    KMessageBox::sorry( 0, i18n("Cannot create home folder for %1: it is null or empty.").arg(p_name) );
    return(0);
  }
  if (mkdir(TQFile::encodeName(p_dir), 0700) != 0) {
    if (errno != EEXIST)
    {
      KMessageBox::error( 0, i18n("Cannot create home folder %1.\nError: %2").arg(p_dir).arg(TQString::fromLocal8Bit(strerror(errno))) );
      return(0);
    }
  }

  if (chown(TQFile::encodeName(p_dir), p_uid, p_gid) != 0) {
    KMessageBox::error( 0, i18n("Cannot change owner of home folder %1.\nError: %2").arg(p_dir).arg(TQString::fromLocal8Bit(strerror(errno))) );
    return(1);
  }

  if (chmod(TQFile::encodeName(p_dir), KU_HOMEDIR_PERM) != 0) {
    KMessageBox::error( 0, i18n("Cannot change permissions on home folder %1.\nError: %2").arg(p_dir).arg(TQString::fromLocal8Bit(strerror(errno))) );
    return(1);
  }
  return(1);
}

int KU::KUser::tryCreate(const TQString &dir)
{
  struct stat sb;
  int rc = 0;

  rc = stat(TQFile::encodeName(dir), &sb);
  if (rc == 0) {
    if (S_ISDIR(sb.st_mode)) {
      if (KMessageBox::warningContinueCancel( 0,
        i18n("Folder %1 already exists!\nWill make %2 owner and change permissions.\nDo you want to continue?").arg(dir).arg(p_name),
        TQString::null, KStdGuiItem::cont() ) == KMessageBox::Continue) {

        if (chown(TQFile::encodeName(dir), p_uid, p_gid) != 0) {
          KMessageBox::error( 0, i18n("Cannot change owner of %1 folder.\nError: %2") .arg(dir).arg(TQString::fromLocal8Bit(strerror(errno))) );
        }
        return(0);
      } else {
        KMessageBox::information( 0, i18n("Folder %1 left 'as is'.\nVerify ownership and permissions for user %2 who may not be able to log in!").arg(dir).arg(p_name) );
        return(-1);
      }
    } else {
      KMessageBox::information( 0, i18n("%1 exists and is not a folder. User %2 will not be able to log in!").arg(dir).arg(p_name) );
      return(-1);
    }
  } else {
    if (errno == ENOENT) {
      if (mkdir(TQFile::encodeName(dir), 0700) != 0) {
        KMessageBox::error( 0, i18n("Cannot create %1 folder.\nError: %2").arg(dir).arg(TQString::fromLocal8Bit(strerror(errno))));
        return(-1);
      }
      if (chown(TQFile::encodeName(dir), p_uid, p_gid) != 0) {
        KMessageBox::error( 0, i18n("Cannot change owner of %1 folder.\nError: %2").arg(dir).arg(TQString::fromLocal8Bit(strerror(errno))) );
      }
      return(0);
    } else {
      KMessageBox::error( 0, i18n("stat call on %1 failed.\nError: %2").arg(dir).arg(TQString::fromLocal8Bit(strerror(errno))) );
      return(-1);
    }
  }
}

int KU::KUser::createMailBox()
{
  TQString mailboxpath;
  int fd;
  mailboxpath = TQFile::decodeName(MAIL_SPOOL_DIR) + "/" + p_name;
  if((fd = open(TQFile::encodeName(mailboxpath), O_CREAT|O_EXCL|O_WRONLY,
                S_IRUSR|S_IWUSR)) < 0) {
    if (errno != EEXIST)
    {
      KMessageBox::error( 0, i18n("Cannot create %1: %2")
                .arg(mailboxpath)
                .arg(TQString::fromLocal8Bit(strerror(errno))) );
      return -1;
    }
  }

  close(fd);

  if (chown(TQFile::encodeName(mailboxpath), p_uid, KU_MAILBOX_GID) != 0) {
    KMessageBox::error( 0, i18n("Cannot change owner on mailbox: %1\nError: %2")
                .arg(mailboxpath).arg(TQString::fromLocal8Bit(strerror(errno))) );
    return -1;
  }

  if (chmod(TQFile::encodeName(mailboxpath), KU_MAILBOX_PERM) != 0) {
    KMessageBox::error( 0, i18n("Cannot change permissions on mailbox: %1\nError: %2")
                .arg(mailboxpath).arg(TQString::fromLocal8Bit(strerror(errno))) );
    return -1;
  }

  return 0;
}

void KU::KUser::copyDir(const TQString &srcPath, const TQString &dstPath)
{
  mode_t mode;
  TQDir s(srcPath);
  TQDir d(dstPath);

  TQString dot = TQString::fromLatin1(".");
  TQString dotdot = TQString::fromLatin1("..");
  
  s.setFilter( TQDir::All | TQDir::Hidden | TQDir::System );

  for (uint i=0; i<s.count(); i++) {
    TQString name(s[i]);

    if (name == dot)
      continue;
    if (name == dotdot)
      continue;

    TQString filename(s.filePath(name));

    TQFileInfo info(filename);
    mode = 0;
    if ( info.permission(TQFileInfo::ReadOwner) ) mode |=  S_IRUSR;
    if ( info.permission(TQFileInfo::WriteOwner) ) mode |=  S_IWUSR;
    if ( info.permission(TQFileInfo::ExeOwner) ) mode |=  S_IXUSR;
    if ( info.permission(TQFileInfo::ReadGroup) ) mode |=  S_IRGRP;
    if ( info.permission(TQFileInfo::WriteGroup) ) mode |=  S_IWGRP;
    if ( info.permission(TQFileInfo::ExeGroup) ) mode |=  S_IXGRP;
    if ( info.permission(TQFileInfo::ReadOther) ) mode |=  S_IROTH;
    if ( info.permission(TQFileInfo::WriteOther) ) mode |=  S_IWOTH;
    if ( info.permission(TQFileInfo::ExeOther) ) mode |=  S_IXOTH;

    if ( info.isSymLink() ) {
      TQString link = info.readLink();

      if (symlink(TQFile::encodeName(link),TQFile::encodeName(d.filePath(name))) != 0) {
        KMessageBox::error( 0, i18n("Error creating symlink %1.\nError: %2")
                  .arg(d.filePath(s[i])).arg(TQString::fromLocal8Bit(strerror(errno))) );
      }
    } else if ( info.isDir() ) {
      TQDir dir(filename);

      d.mkdir(name, FALSE);
      copyDir(s.filePath(name), d.filePath(name));

      if (chown(TQFile::encodeName(d.filePath(name)), p_uid, p_gid) != 0) {
        KMessageBox::error( 0, i18n("Cannot change owner of folder %1.\nError: %2")
                  .arg(d.filePath(s[i])).arg(TQString::fromLocal8Bit(strerror(errno))) );
      }

      if (chmod(TQFile::encodeName(d.filePath(name)), mode) != 0) {
        KMessageBox::error( 0, i18n("Cannot change permissions on folder %1.\nError: %2")
                  .arg(d.filePath(s[i])).arg(TQString::fromLocal8Bit(strerror(errno))) );
      }

    } else {
      if (copyFile(filename, d.filePath(name)) == -1) {
        continue;
      }

      if (chown(TQFile::encodeName(d.filePath(name)), p_uid, p_gid) != 0) {
        KMessageBox::error( 0, i18n("Cannot change owner of file %1.\nError: %2")
                  .arg(d.filePath(s[i])).arg(TQString::fromLocal8Bit(strerror(errno))) );
      }

      if (chmod(TQFile::encodeName(d.filePath(name)), mode) != 0) {
        KMessageBox::error( 0, i18n("Cannot change permissions on file %1.\nError: %2")
                  .arg(d.filePath(s[i])).arg(TQString::fromLocal8Bit(strerror(errno))) );
      }
    }
  }
}

int KU::KUser::copySkel()
{
  TQDir s(TQFile::decodeName(SKELDIR));
  TQDir d(p_dir);
  mode_t mode;
  
  if (!s.exists()) {
    KMessageBox::error( 0, i18n("Folder %1 does not exist, cannot copy skeleton for %2.").arg(s.absPath()).arg(p_name) );
    return (-1);
  }

  if (!d.exists()) {
    KMessageBox::error( 0, i18n("Folder %1 does not exist, cannot copy skeleton.").arg(d.absPath()) );
    return (-1);
  }
  
  mode = umask(0007);
  copyDir(s.absPath(), d.absPath());
  umask( mode );
  
  return 0;
}

int KU::KUser::removeHome()
{
  struct stat sb;

  if (!stat(TQFile::encodeName(p_dir), &sb))
    if (S_ISDIR(sb.st_mode) && sb.st_uid == p_uid) {
      if (!KIO::NetAccess::del(KURL::fromPathOrURL(p_dir))) {
             KMessageBox::error( 0, i18n("Cannot remove home folder %1.\nError: %2")
                       .arg(p_dir).arg(KIO::NetAccess::lastErrorString()) );
      }
    } else {
      KMessageBox::error( 0, i18n("Removal of home folder %1 failed (uid = %2, gid = %3).").arg(p_dir).arg(sb.st_uid).arg(sb.st_gid) );
    }
  else {
    KMessageBox::error( 0, i18n("stat call on file %1 failed.\nError: %2")
                 .arg(p_dir).arg(TQString::fromLocal8Bit(strerror(errno))) );
  }

  return 0;
}

//TODO: remove at jobs too.

int KU::KUser::removeCrontabs()
{
  TQString file;
  TQString command;

  file = TQFile::decodeName(CRONTAB_DIR) + "/" + p_name;
  if ( access(TQFile::encodeName(file), F_OK) == 0 ) {
    command = TQString::fromLatin1("crontab -u %1 -r").arg(KProcess::quote(p_name));
    if ( system(TQFile::encodeName(command)) != 0 ) {
      KMessageBox::error( 0, i18n("Cannot remove crontab %1.\nError: %2")
                  .arg(command).arg(TQString::fromLocal8Bit(strerror(errno))) );
     }
  }

  return 0;
}

int KU::KUser::removeMailBox()
{
  TQString file;

  file = TQFile::decodeName(MAIL_SPOOL_DIR) + "/" + p_name;
  if (remove(TQFile::encodeName(file)) != 0) {
    KMessageBox::error( 0, i18n("Cannot remove mailbox %1.\nError: %2")
                .arg(file).arg(TQString::fromLocal8Bit(strerror(errno))) );
  }

  return 0;
}

int KU::KUser::removeProcesses()
{
  // be paranoid -- kill all processes owned by that user, if not root.

  if (p_uid != 0)
    switch (fork()) {
      case 0:
        setuid(p_uid);
        kill(-1, 9);
        _exit(0);
        break;
      case -1:
        KMessageBox::error( 0,
          i18n("Cannot fork while trying to kill processes for uid %1.").arg(p_uid) );
        break;
    }

  return 0;
}

KU::KUsers::KUsers(KUserPrefsBase *cfg)
{
  mUsers.setAutoDelete(TRUE);
  mCfg = cfg;
}

KU::KUsers::~KUsers()
{
  mUsers.clear();
}

const TQString &KU::KUsers::getDOMSID() const
{
  return domsid;
}

void KU::KUsers::parseGecos( const char *gecos, TQString &name, 
  TQString &field1, TQString &field2, TQString &field3 )
{
  int no = 0;
  const char *s = gecos;
  const char *pos = NULL;
  // At least one part of the string exists
  for(;;) {
    pos = strchr(s, ',');
    TQString val;
    if(pos == NULL)
      val = TQString::fromLocal8Bit(s);
    else
      val = TQString::fromLocal8Bit(s, (int)(pos-s));

    switch(no) {
      case 0: name = val; break;
      case 1: field1 = val; break;
      case 2: field2 = val; break;
      case 3: field3 = val; break;
    }
    if(pos == NULL) break;
    s = pos+1;
    no++;
  }
}  

void KU::KUsers::fillGecos(KU::KUser *user, const char *gecos)
{
  TQString name,field1,field2,field3;
  parseGecos( gecos, name, field1, field2, field3 );
  user->setFullName( name );
  caps & Cap_BSD ? user->setOffice( field1 ) : user->setOffice1( field1 );
  caps & Cap_BSD ? user->setWorkPhone( field2 ) : user->setOffice2( field2 );
  caps & Cap_BSD ? user->setHomePhone( field3 ) : user->setAddress( field3 );
}

bool KU::KUsers::doCreate(KU::KUser *user)
{
  TQString h_dir;

  if(user->getCreateMailBox()) {
    user->createMailBox();
    user->setCreateMailBox(false);
  }

  if(user->getCreateHome()) {
    if(user->createHome()) {
      user->setCreateHome(false);
     } else {
       return false; // if createHome fails, copySkel is irrelevant!
     }

     if(user->getCopySkel()) {
       if((user->copySkel()) == 0) {
         user->setCopySkel(false);
       }
     }

  }
  return TRUE;
}

bool KU::KUsers::doDelete( KU::KUser *user )
{
  kdDebug() << "delete user: " << user->getName() << " uid: " << user->getUID() << endl;
  if ( user->isDeleteHome ) {
    user->removeHome();
    user->removeCrontabs();
  }
  if ( user->isDeleteMailBox )
    user->removeMailBox();
/*
  user->removeProcesses();
*/
  return TRUE;
}

KU::KUser *KU::KUsers::lookup(const TQString & name)
{
  KU::KUser *user;
  TQPtrListIterator<KU::KUser> it( mUsers );

  while ( (user = it.current()) != 0 && user->getName() != name ) ++it;
  return user;
}

KU::KUser *KU::KUsers::lookup(uid_t uid)
{
  KU::KUser *user;
  TQPtrListIterator<KU::KUser> it( mUsers );

  while ( (user = it.current()) != 0 && user->getUID() != uid ) ++it;
  return user;
}

KU::KUser *KU::KUsers::lookup_sam( const SID &sid )
{
  KU::KUser *user;
  TQPtrListIterator<KU::KUser> it( mUsers );

  while ( (user = it.current()) != 0 && user->getSID() != sid ) ++it;
  return user;
}

KU::KUser *KU::KUsers::lookup_sam( const TQString &sid )
{
  KU::KUser *user;
  TQPtrListIterator<KU::KUser> it( mUsers );

  while ( (user = it.current()) != 0 && user->getSID().getSID() != sid ) ++it;
  return user;
}

KU::KUser *KU::KUsers::lookup_sam( uint rid )
{
  KU::KUser *user;
  TQPtrListIterator<KU::KUser> it( mUsers );

  while ( (user = it.current()) != 0 && user->getSID().getRID() != rid ) ++it;
  return user;
}

uid_t KU::KUsers::first_free()
{
  uid_t t;

  for (t = mCfg->firstUID() ; t<65534; t++)
    if (lookup(t) == NULL)
      return t;

  return NO_FREE;
}

uint KU::KUsers::first_free_sam()
{
  uint t;

  for (t = 1000; t<65534; t++)
    if (lookup_sam(t) == NULL)
      return t;

  return 0;
}

uint KU::KUsers::count() const
{
  return mUsers.count();
}

KU::KUser *KU::KUsers::operator[](uint num)
{
  return mUsers.at(num);
}

KU::KUser *KU::KUsers::first()
{
  return mUsers.first();
}

KU::KUser *KU::KUsers::next()
{
  return mUsers.next();
}

void KU::KUsers::add(KU::KUser *user)
{
  mAdd.append( user );
}

void KU::KUsers::del(KU::KUser *user)
{
  mDel.append( user );
}

void KU::KUsers::mod(KU::KUser *uold, const KU::KUser &unew)
{
  mMod.insert( uold, unew );
}

void KU::KUsers::commit()
{
  kdDebug() << "KU::KUsers::commit()" << endl;
  KU::KUser *user;
  DelIt dit( mDelSucc );
  AddIt ait( mAddSucc );
  ModIt mit = mModSucc.begin();

//commit modifications
  while ( mit != mModSucc.end() ) {
    *(mit.key()) = mit.data();
    mit++;
  }
//commit deletes
  while ( (user = dit.current()) != 0 ) {
    ++dit;
    doDelete( user );
    mUsers.remove( user );
  }
//commit additions
  while ( (user = ait.current()) != 0 ) {
    ++ait;
    doCreate( user );
    mUsers.append( user );
  }

//clear the unsuccessful modifications
  cancelMods();
}

void KU::KUsers::cancelMods()
{
  KU::KUser *user;
  while ( (user = mAdd.first()) ) {
    delete user;
    mAdd.remove();
  }
  mDel.clear();
  mMod.clear();
}