Proof-reading - fixed one usage of the i18n plural form (it wasn't doing before,...
[kdeadmin.git] / kuser / ku_usersystem.cpp
blob2800d199ed9b7c05c0ff1124a03bddb09093cfff
1 /*
2 * Copyright (c) 1998 Denis Perchine <dyp@perchine.com>
3 * Copyright (c) 2004 Szombathelyi György <gyurco@freemail.hu>
4 * Former maintainer: Adriaan de Groot <groot@kde.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 **/
22 #include "globals.h"
23 #include <errno.h>
24 #include <pwd.h>
25 #ifdef HAVE_SHADOW_H
26 #include <shadow.h>
27 #endif
29 #include <kdebug.h>
31 #include "ku_misc.h"
32 #include "ku_usersystem.h"
34 KU_UserSystem::KU_UserSystem(KU_PrefsBase *cfg) : KU_Users( cfg )
36 caps = Cap_ReadOnly | Cap_Passwd;
37 #ifdef HAVE_SHADOW_H
38 if ( !mCfg->shadowsrc().isEmpty() ) caps |= Cap_Shadow;
39 #endif
40 #if defined(__FreeBSD__) || defined(__bsdi__)
41 caps |= Cap_BSD;
42 #endif
46 KU_UserSystem::~KU_UserSystem()
50 bool KU_UserSystem::reload()
52 mErrorString = mErrorDetails = QString();
53 if (!loadpwd())
54 return false;
56 if (!loadsdw())
57 return false;
59 return true;
62 // Load passwd file
64 bool KU_UserSystem::loadpwd()
66 passwd *p;
67 KU_User user;
68 QString tmp;
70 setpwent(); //This should be enough for BSDs
71 while ((p = getpwent()) != NULL) {
72 user = KU_User();
73 user.setCaps( KU_User::Cap_POSIX );
74 user.setUID(p->pw_uid);
75 user.setGID(p->pw_gid);
76 user.setName(QString::fromLocal8Bit(p->pw_name));
77 tmp = QString::fromLocal8Bit( p->pw_passwd );
78 if ( tmp != "x" && tmp != "*" && !tmp.startsWith('!') )
79 user.setDisabled( false );
80 else
81 user.setDisabled( true );
82 if ( tmp.startsWith('!') ) tmp.remove(0, 1);
83 user.setPwd( tmp );
84 user.setHomeDir(QString::fromLocal8Bit(p->pw_dir));
85 user.setShell(QString::fromLocal8Bit(p->pw_shell));
86 #if defined(__FreeBSD__) || defined(__bsdi__)
87 user.setClass(QString::fromLatin1(p->pw_class));
88 user.setLastChange(p->pw_change);
89 user.setExpire(p->pw_expire);
90 #endif
92 if ((p->pw_gecos != 0) && (p->pw_gecos[0] != 0))
93 fillGecos(user, p->pw_gecos);
94 append(user);
97 endpwent();
98 return true;
101 // Load shadow passwords
103 bool KU_UserSystem::loadsdw()
105 #ifdef HAVE_SHADOW_H
106 struct spwd *spw;
107 KU_User user;
108 QString tmp;
109 int index;
111 setspent();
112 while ((spw = getspent())) { // read a shadow password structure
115 if ((index = lookup(QString::fromLocal8Bit(spw->sp_namp))) == -1) {
116 continue;
119 user = at( index );
120 tmp = QString::fromLocal8Bit( spw->sp_pwdp );
121 if ( tmp.startsWith("!!") || tmp == "*" ) {
122 user.setDisabled( true );
123 tmp.remove( 0, 2 );
124 } else
125 user.setDisabled( false );
127 user.setSPwd( tmp ); // cp the encrypted pwd
128 user.setLastChange( daysToTime( spw->sp_lstchg ) );
129 user.setMin(spw->sp_min);
130 user.setMax(spw->sp_max);
131 #ifndef _SCO_DS
132 user.setWarn(spw->sp_warn);
133 user.setInactive(spw->sp_inact);
134 user.setExpire( daysToTime( spw->sp_expire ) );
135 user.setFlag(spw->sp_flag);
136 #endif
137 replace( index, user );
140 endspent();
141 #endif //HAVE_SHADOW_H
142 return true;