Proof-reading - fixed one usage of the i18n plural form (it wasn't doing before,...
[kdeadmin.git] / kuser / ku_groupfiles.cpp
blob563c25c1570b26ad68a668866b008508c78ea0e5
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 "ku_groupfiles.h"
24 #include "globals.h"
26 #include <ku_config.h>
27 #include <errno.h>
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31 #include <sys/types.h>
32 #include <sys/file.h>
33 #include <sys/stat.h>
34 #include <grp.h>
35 #include <stdio.h>
36 #include <stdlib.h>
39 #include <kdebug.h>
40 #include <kstandarddirs.h>
41 #include <klocale.h>
43 #include "ku_misc.h"
45 KU_GroupFiles::KU_GroupFiles( KU_PrefsBase *cfg ) : KU_Groups( cfg )
47 gs_backuped = false;
48 gr_backuped = false;
50 smode = 0400;
51 mode = 0644;
52 uid = 0;
53 gid = 0;
55 caps = Cap_Passwd;
58 KU_GroupFiles::~KU_GroupFiles()
62 bool KU_GroupFiles::reload()
64 struct group *p;
65 KU_Group group;
66 struct stat st;
67 QString group_filename;
68 int rc = 0;
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'");
75 return false;
78 // Start reading group file(s)
80 rc = stat(QFile::encodeName(group_filename), &st);
81 if(rc != 0) {
82 mErrorString = i18n("stat() call on file %1 failed: %2\nCheck KUser settings.",
83 group_filename, QString::fromLocal8Bit(strerror(errno)));
84 return false;
87 mode = st.st_mode;
88 uid = st.st_uid;
89 gid = st.st_gid;
91 // We are reading our configuration specified group file
92 #ifdef HAVE_FGETGRENT
93 FILE *fgrp = fopen(QFile::encodeName(group_filename), "r");
94 QString tmp;
95 if (fgrp == NULL) {
96 mErrorString = i18n("Error opening %1 for reading.", group_filename);
97 return false;
100 while ((p = fgetgrent(fgrp)) != NULL) {
101 #else
102 setgrent();
103 while ((p = getgrent()) != NULL) {
104 #endif
105 group = KU_Group();
106 group.setGID(p->gr_gid);
107 group.setName(QString::fromLocal8Bit(p->gr_name));
108 group.setPwd(QString::fromLocal8Bit(p->gr_passwd));
110 char *u_name;
111 int i = 0;
112 while ((u_name = p->gr_mem[i])!=0) {
113 group.addUser(QString::fromLocal8Bit(u_name));
114 i++;
117 append(group);
120 // End reading filename
122 #ifdef HAVE_FGETGRENT
123 fclose(fgrp);
124 #else
125 endgrent();
126 #endif
128 return true;
131 bool KU_GroupFiles::save()
133 kDebug() << "KU_GroupFiles::save() ";
134 FILE *group_fd = NULL;
135 FILE *gshadow_fd = NULL;
136 gid_t tmp_gid = 0;
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);
145 #ifdef HAVE_SHADOW_H
146 gshadow_filename = mCfg->gshadowsrc();
147 if ( !KStandardDirs::exists( gshadow_filename ) )
148 gshadow_filename = QString();
149 else
150 new_gshadow_filename = gshadow_filename + QString::fromLatin1(KU_CREATE_EXT);
151 #endif
153 // Backup file(s)
155 if(!group_filename.isEmpty()) {
156 if (!gr_backuped) {
157 if ( !backup(group_filename) ) return false;
158 gr_backuped = true;
161 if(!gshadow_filename.isEmpty()) {
162 if (!gs_backuped) {
163 if ( !backup(gshadow_filename) ) return false;
164 gs_backuped = true;
168 // Open file(s)
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);
173 return false;
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 );
181 return false;
185 /******************/
186 KU_Group group;
187 int groupsindex = 0, addindex = 0;
188 while (true) {
190 if ( groupsindex == count() ) {
191 if ( addindex == mAdd.count() ) break;
192 group = mAdd.at(addindex);
193 addindex++;
194 } else {
195 if ( mDel.contains( groupsindex ) ) {
196 groupsindex++;
197 continue;
199 if ( mMod.contains(groupsindex) )
200 group = mMod.value(groupsindex);
201 else
202 group = at(groupsindex);
203 groupsindex++;
206 #ifdef HAVE_SHADOW_H
207 if ( addindex && !mCfg->gshadowsrc().isEmpty() )
208 group.setPwd("x");
209 #endif
211 tmpGe = group.getName();
212 tmpGe.replace( ',', "_" );
213 tmpGe.replace( ':', "_" );
214 group.setName( tmpGe );
216 tmp_gid = group.getGID();
217 tmpGe += ':' +
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++) {
222 if (j != 0) {
223 tmpGe += ',';
224 tmpSe += ',';
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';
233 if ( group_fd )
234 fputs( tmpGe.toLocal8Bit(), group_fd );
235 if ( gshadow_fd ) fputs( tmpSe.toLocal8Bit(), gshadow_fd );
237 /***********************/
238 if(group_fd) {
239 fclose(group_fd);
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));
246 if(gshadow_fd) {
247 fclose(gshadow_fd);
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));
254 return true;
257 bool KU_GroupFiles::dbcommit()
259 bool ret;
260 mode_t mode;
262 kDebug() << "KU_GroupFiles dbcommit";
263 mAddSucc.clear();
264 mDelSucc.clear();
265 mModSucc.clear();
266 if ( mDel.isEmpty() && mAdd.isEmpty() && mMod.isEmpty() )
267 return true;
269 mode = umask(0077);
270 ret = save();
271 umask( mode );
272 if ( !ret ) return false;
274 mDelSucc = mDel;
275 mAddSucc = mAdd;
276 mModSucc = mMod;
277 mDel.clear();
278 mAdd.clear();
279 mMod.clear();
281 return true;