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.
22 #include "ku_groupfiles.h"
26 #include <ku_config.h>
31 #include <sys/types.h>
40 #include <kstandarddirs.h>
45 KU_GroupFiles::KU_GroupFiles( KU_PrefsBase
*cfg
) : KU_Groups( cfg
)
58 KU_GroupFiles::~KU_GroupFiles()
62 bool KU_GroupFiles::reload()
67 QString group_filename
;
70 mErrorString
= mErrorDetails
= QString();
72 group_filename
= mCfg
->groupsrc();
73 if(group_filename
.isEmpty()) {
74 mErrorString
= i18n("Groups file name not set, please check 'Settings/Files'");
78 // Start reading group file(s)
80 rc
= stat(QFile::encodeName(group_filename
), &st
);
82 mErrorString
= i18n("stat() call on file %1 failed: %2\nCheck KUser settings.",
83 group_filename
, QString::fromLocal8Bit(strerror(errno
)));
91 // We are reading our configuration specified group file
93 FILE *fgrp
= fopen(QFile::encodeName(group_filename
), "r");
96 mErrorString
= i18n("Error opening %1 for reading.", group_filename
);
100 while ((p
= fgetgrent(fgrp
)) != NULL
) {
103 while ((p
= getgrent()) != NULL
) {
106 group
.setGID(p
->gr_gid
);
107 group
.setName(QString::fromLocal8Bit(p
->gr_name
));
108 group
.setPwd(QString::fromLocal8Bit(p
->gr_passwd
));
112 while ((u_name
= p
->gr_mem
[i
])!=0) {
113 group
.addUser(QString::fromLocal8Bit(u_name
));
120 // End reading filename
122 #ifdef HAVE_FGETGRENT
131 bool KU_GroupFiles::save()
133 kDebug() << "KU_GroupFiles::save() ";
134 FILE *group_fd
= NULL
;
135 FILE *gshadow_fd
= NULL
;
137 QString tmpGe
, tmpSe
, tmp2
;
138 QString group_filename
, new_group_filename
;
139 QString gshadow_filename
, new_gshadow_filename
;
141 // read KUser configuration info
143 group_filename
= mCfg
->groupsrc();
144 new_group_filename
= group_filename
+ QString::fromLatin1(KU_CREATE_EXT
);
146 gshadow_filename
= mCfg
->gshadowsrc();
147 if ( !KStandardDirs::exists( gshadow_filename
) )
148 gshadow_filename
= QString();
150 new_gshadow_filename
= gshadow_filename
+ QString::fromLatin1(KU_CREATE_EXT
);
155 if(!group_filename
.isEmpty()) {
157 if ( !backup(group_filename
) ) return false;
161 if(!gshadow_filename
.isEmpty()) {
163 if ( !backup(gshadow_filename
) ) return false;
170 if( !group_filename
.isEmpty() ) {
171 if((group_fd
= fopen(QFile::encodeName(new_group_filename
), "w")) == NULL
) {
172 mErrorString
= i18n("Error opening %1 for writing.", new_group_filename
);
177 if( !gshadow_filename
.isEmpty() ) {
178 if((gshadow_fd
= fopen(QFile::encodeName(new_gshadow_filename
), "w")) == NULL
) {
179 mErrorString
= i18n("Error opening %1 for writing.", new_gshadow_filename
);
180 if ( group_fd
) fclose ( group_fd
);
187 int groupsindex
= 0, addindex
= 0;
190 if ( groupsindex
== count() ) {
191 if ( addindex
== mAdd
.count() ) break;
192 group
= mAdd
.at(addindex
);
195 if ( mDel
.contains( groupsindex
) ) {
199 if ( mMod
.contains(groupsindex
) )
200 group
= mMod
.value(groupsindex
);
202 group
= at(groupsindex
);
207 if ( addindex
&& !mCfg
->gshadowsrc().isEmpty() )
211 tmpGe
= group
.getName();
212 tmpGe
.replace( ',', "_" );
213 tmpGe
.replace( ':', "_" );
214 group
.setName( tmpGe
);
216 tmp_gid
= group
.getGID();
218 group
.getPwd() + ':' +
219 QString::number( group
.getGID() ) + ':';
220 tmpSe
= group
.getName() + ":!::"; //krazy:exclude=doublequote_chars
221 for (uint j
=0; j
<group
.count(); j
++) {
226 group
.user( j
).replace( ',', "_" );
227 group
.user( j
).replace( ':', "_" );
228 tmpGe
+= group
.user( j
) ;
229 tmpSe
+= group
.user( j
);
231 tmpGe
+= '\n'; tmpSe
+= '\n';
234 fputs( tmpGe
.toLocal8Bit(), group_fd
);
235 if ( gshadow_fd
) fputs( tmpSe
.toLocal8Bit(), gshadow_fd
);
237 /***********************/
240 chmod(QFile::encodeName(new_group_filename
), mode
);
241 chown(QFile::encodeName(new_group_filename
), uid
, gid
);
242 rename(QFile::encodeName(new_group_filename
),
243 QFile::encodeName(group_filename
));
248 chmod(QFile::encodeName(new_gshadow_filename
), mode
);
249 chown(QFile::encodeName(new_gshadow_filename
), uid
, gid
);
250 rename(QFile::encodeName(new_gshadow_filename
),
251 QFile::encodeName(gshadow_filename
));
257 bool KU_GroupFiles::dbcommit()
262 kDebug() << "KU_GroupFiles dbcommit";
266 if ( mDel
.isEmpty() && mAdd
.isEmpty() && mMod
.isEmpty() )
272 if ( !ret
) return false;