21 #include <sys/types.h>
28 #include <tqtextstream.h>
31 #include <dcopclient.h>
33 #include <tdecmdlineargs.h>
34 #include <tdeapplication.h>
35 #include <tdelocale.h>
36 #include <tdeaboutdata.h>
37 #include <tdeglobal.h>
38 #include <kstandarddirs.h>
39 #include <tdeprocess.h>
42 static TDECmdLineOptions options[] = {
43 {
"+old", I18N_NOOP(
"Old hostname"), 0 },
44 {
"+new", I18N_NOOP(
"New hostname"), 0 },
48 static const char appName[] =
"kdontchangethehostname";
49 static const char appVersion[] =
"1.1";
58 void changeStdDirs(
const TQCString &type);
59 void changeSessionManager();
68 KHostName::KHostName()
70 TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
71 if (args->count() != 2)
73 oldName = args->arg(0);
74 newName = args->arg(1);
75 if (oldName == newName)
78 home = ::getenv(
"HOME");
81 fprintf(stderr,
"%s", i18n(
"Error: HOME environment variable not set.\n").local8Bit().data());
85 display = ::getenv(
"DISPLAY");
87 display.replace(TQRegExp(
"\\.[0-9]+$"),
"");
88 if (display.isEmpty())
90 fprintf(stderr,
"%s", i18n(
"Error: DISPLAY environment variable not set.\n").local8Bit().data());
95 static QCStringList split(
const TQCString &str)
97 const char *s = str.data();
101 const char *i = strchr(s,
' ');
104 result.append(TQCString(s));
107 result.append(TQCString(s, i-s+1));
109 while (*s ==
' ') s++;
114 void KHostName::changeX()
116 const char* xauthlocalhostname = getenv(
"XAUTHLOCALHOSTNAME");
117 TQString cmd =
"xauth -n list";
118 FILE *xFile = popen(TQFile::encodeName(cmd),
"r");
121 fprintf(stderr,
"Warning: Can't run xauth.\n");
130 TQCString line = fgets(buf, 1024, xFile);
132 line.truncate(line.length()-1);
139 for(QCStringList::ConstIterator it = lines.begin();
140 it != lines.end(); ++it)
142 QCStringList entries = split(*it);
143 if (entries.count() != 3)
146 TQCString netId = entries[0];
147 TQCString authName = entries[1];
148 TQCString authKey = entries[2];
150 int i = netId.findRev(
':');
153 TQCString netDisplay = netId.mid(i);
154 if (netDisplay != display)
161 TQCString newNetId = newName+netId.mid(i);
162 TQCString oldNetId = netId.left(i);
164 if(oldNetId != oldName
165 && (!xauthlocalhostname || strcmp(xauthlocalhostname, oldNetId.data()) != 0))
169 if (!xauthlocalhostname || oldNetId != xauthlocalhostname)
171 cmd =
"xauth -n remove "+TDEProcess::quote(netId);
172 system(TQFile::encodeName(cmd));
174 cmd =
"xauth -n add ";
175 cmd += TDEProcess::quote(newNetId);
177 cmd += TDEProcess::quote(authName);
179 cmd += TDEProcess::quote(authKey);
180 system(TQFile::encodeName(cmd));
184 void KHostName::changeDcop()
186 TQCString origFNameOld = DCOPClient::dcopServerFileOld(oldName);
187 TQCString fname = DCOPClient::dcopServerFile(oldName);
188 TQCString origFName = fname;
189 FILE *dcopFile = fopen(fname.data(),
"r");
192 fprintf(stderr,
"Warning: Can't open '%s' for reading.\n", fname.data());
196 TQCString line1, line2;
199 line1 = fgets(buf, 1024, dcopFile);
201 line1.truncate(line1.length()-1);
203 line2 = fgets(buf, 1024, dcopFile);
205 line2.truncate(line2.length()-1);
209 TQCString oldNetId = line1;
211 if (!newName.isEmpty())
213 int i = line1.findRev(
':');
216 fprintf(stderr,
"Warning: File '%s' has unexpected format.\n", fname.data());
219 line1 =
"local/"+newName+line1.mid(i);
220 TQCString newNetId = line1;
221 fname = DCOPClient::dcopServerFile(newName);
222 unlink(fname.data());
223 dcopFile = fopen(fname.data(),
"w");
226 fprintf(stderr,
"Warning: Can't open '%s' for writing.\n", fname.data());
230 fputs(line1.data(), dcopFile);
231 fputc(
'\n', dcopFile);
232 fputs(line2.data(), dcopFile);
233 fputc(
'\n', dcopFile);
237 TQCString compatLink = DCOPClient::dcopServerFileOld(newName);
238 ::symlink(fname.data(), compatLink.data());
241 TQString cmd = ICEAUTH_COMMAND
" list "+TDEProcess::quote(
"netid="+oldNetId);
242 FILE *iceFile = popen(TQFile::encodeName(cmd),
"r");
245 fprintf(stderr,
"Warning: Can't run iceauth.\n");
251 while (!feof(iceFile))
253 TQCString line = fgets(buf, 1024, iceFile);
255 line.truncate(line.length()-1);
262 for(QCStringList::ConstIterator it = lines.begin();
263 it != lines.end(); ++it)
265 QCStringList entries = split(*it);
266 if (entries.count() != 5)
269 TQCString protName = entries[0];
270 TQCString netId = entries[2];
271 TQCString authName = entries[3];
272 TQCString authKey = entries[4];
273 if (netId != oldNetId)
276 cmd = ICEAUTH_COMMAND
" add ";
277 cmd += TDEProcess::quote(protName);
279 cmd += TDEProcess::quote(newNetId);
281 cmd += TDEProcess::quote(authName);
283 cmd += TDEProcess::quote(authKey);
284 system(TQFile::encodeName(cmd));
290 char* xauthlocalhostname = getenv(
"XAUTHLOCALHOSTNAME");
291 if (!xauthlocalhostname || !oldNetId.contains(xauthlocalhostname))
293 TQString cmd = ICEAUTH_COMMAND
" remove "+TDEProcess::quote(
"netid="+oldNetId);
294 system(TQFile::encodeName(cmd));
295 unlink(origFName.data());
296 origFName = DCOPClient::dcopServerFileOld(oldName);
297 unlink(origFName.data());
301 void KHostName::changeStdDirs(
const TQCString &type)
304 TQCString oldDir = TQFile::encodeName(TQString(
"%1%2-%3").arg(TDEGlobal::dirs()->localtdedir()).arg(type.data()).arg(oldName.data()));
305 TQCString newDir = TQFile::encodeName(TQString(
"%1%2-%3").arg(TDEGlobal::dirs()->localtdedir()).arg(type.data()).arg(newName.data()));
307 KDE_struct_stat st_buf;
309 int result = KDE_lstat(oldDir.data(), &st_buf);
312 if (S_ISLNK(st_buf.st_mode))
315 result = readlink(oldDir.data(), buf, 4096);
319 result = symlink(buf, newDir.data());
322 else if (S_ISDIR(st_buf.st_mode))
324 result = symlink(oldDir.data(), newDir.data());
333 system((
"lnusertemp "+type).data());
337 void KHostName::changeSessionManager()
339 TQCString sm = ::getenv(
"SESSION_MANAGER");
342 fprintf(stderr,
"Warning: No session management specified.\n");
345 int i = sm.findRev(
':');
346 if ((i == -1) || (sm.left(6) !=
"local/"))
348 fprintf(stderr,
"Warning: Session Management socket '%s' has unexpected format.\n", sm.data());
351 sm =
"local/"+newName+sm.mid(i);
352 TQCString name =
"SESSION_MANAGER";
354 TQDataStream stream(params, IO_WriteOnly);
355 stream << name << sm;
356 DCOPClient *client =
new DCOPClient();
357 if (!client->attach())
359 fprintf(stderr,
"Warning: DCOP communication problem, can't fix Session Management.\n");
363 TQCString launcher = TDEApplication::launcher();
364 client->send(launcher, launcher,
"setLaunchEnv(TQCString,TQCString)", params);
368 int main(
int argc,
char **argv)
370 TDELocale::setMainCatalogue(
"tdelibs");
371 TDEAboutData d(appName, I18N_NOOP(
"KDontChangeTheHostName"), appVersion,
372 I18N_NOOP(
"Informs TDE about a change in hostname"),
373 TDEAboutData::License_GPL,
"(c) 2001 Waldo Bastian");
374 d.addAuthor(
"Waldo Bastian", I18N_NOOP(
"Author"),
"bastian@kde.org");
376 TDECmdLineArgs::init(argc, argv, &d);
377 TDECmdLineArgs::addCmdLineOptions(options);
385 hn.changeStdDirs(
"socket");
386 hn.changeStdDirs(
"tmp");
387 hn.changeSessionManager();