summaryrefslogtreecommitdiffstats
path: root/kuser/kusersystem.cpp
blob: 8c67fcb37b7ff9e2fa538bc981a8c9da9e3fa791 (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
/*
 *  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 <pwd.h>
#ifdef HAVE_SHADOW
#include <shadow.h>
#endif

#include <tqstring.h>

#include <kmessagebox.h>
#include <kdebug.h>

#include "kglobal_.h"
#include "misc.h"
#include "kusersystem.h"

KUserSystem::KUserSystem(KUserPrefsBase *cfg) : KU::KUsers( cfg )
{
  mUsers.setAutoDelete(TRUE);

  caps = Cap_ReadOnly | Cap_Passwd;
#ifdef HAVE_SHADOW
  if ( !mCfg->shadowsrc().isEmpty() ) caps |= Cap_Shadow;
#endif
#if defined(__FreeBSD__) || defined(__bsdi__)
  caps |= Cap_BSD;
#endif

  reload();
}

KUserSystem::~KUserSystem()
{
}

bool KUserSystem::reload() 
{
  if (!loadpwd())
    return FALSE;

  if (!loadsdw())
    return FALSE;

  return TRUE;
}

// Load passwd file

bool KUserSystem::loadpwd()
{
  passwd *p;
  KU::KUser *tmpKU = 0;
  TQString tmp;

  setpwent(); //This should be enough for BSDs
  while ((p = getpwent()) != NULL) {
    tmpKU = new KU::KUser();
    tmpKU->setUID(p->pw_uid);
    tmpKU->setGID(p->pw_gid);
    tmpKU->setName(TQString::fromLocal8Bit(p->pw_name));
    tmp  = TQString::fromLocal8Bit( p->pw_passwd );
    if ( tmp != "x" && tmp != "*" && !tmp.startsWith("!") )
      tmpKU->setDisabled( false );
    else
      tmpKU->setDisabled( true );
    if ( tmp.startsWith("!") ) tmp.remove(0, 1);
    tmpKU->setPwd( tmp );
    tmpKU->setHomeDir(TQString::fromLocal8Bit(p->pw_dir));
    tmpKU->setShell(TQString::fromLocal8Bit(p->pw_shell));
#if defined(__FreeBSD__) || defined(__bsdi__)
    tmpKU->setClass(TQString::fromLatin1(p->pw_class));
    tmpKU->setLastChange(p->pw_change);
    tmpKU->setExpire(p->pw_expire);
#endif

    if ((p->pw_gecos != 0) && (p->pw_gecos[0] != 0))
      fillGecos(tmpKU, p->pw_gecos);
    mUsers.append(tmpKU);
  }

  endpwent();
  return(TRUE);
}

// Load shadow passwords

bool KUserSystem::loadsdw()
{
#ifdef HAVE_SHADOW
  struct spwd *spw;
  KU::KUser *up = NULL;
  TQString tmp;

  setspent();
  while ((spw = getspent())) {     // read a shadow password structure

    if ((up = lookup(TQString::fromLocal8Bit(spw->sp_namp))) == NULL) {
      continue;
    }

    tmp = TQString::fromLocal8Bit( spw->sp_pwdp );
    if ( tmp.startsWith("!!") || tmp == "*" ) {
      up->setDisabled( true );
      tmp.remove( 0, 2 );
    } else
      up->setDisabled( false );

    up->setSPwd( tmp );        // cp the encrypted pwd
    up->setLastChange( daysToTime( spw->sp_lstchg ) );
    up->setMin(spw->sp_min);
    up->setMax(spw->sp_max);
#ifndef _SCO_DS
    up->setWarn(spw->sp_warn);
    up->setInactive(spw->sp_inact);
    up->setExpire( daysToTime( spw->sp_expire ) );
    up->setFlag(spw->sp_flag);
#endif
  }

  endspent();
#endif //HAVE_SHADOW  
  return true;
}