19 #include <sys/types.h> 26 #include <tqtextstream.h> 29 #include <dcopclient.h> 31 #include <tdecmdlineargs.h> 32 #include <tdeapplication.h> 33 #include <tdelocale.h> 34 #include <tdeaboutdata.h> 35 #include <tdeglobal.h> 36 #include <kstandarddirs.h> 40 static TDECmdLineOptions options[] = {
41 {
"+old", I18N_NOOP(
"Old hostname"), 0 },
42 {
"+new", I18N_NOOP(
"New hostname"), 0 },
46 static const char appName[] =
"kdontchangethehostname";
47 static const char appVersion[] =
"1.1";
56 void changeStdDirs(
const TQCString &type);
57 void changeSessionManager();
66 KHostName::KHostName()
68 TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
69 if (args->count() != 2)
71 oldName = args->arg(0);
72 newName = args->arg(1);
73 if (oldName == newName)
76 home = ::getenv(
"HOME");
79 fprintf(stderr,
"%s", i18n(
"Error: HOME environment variable not set.\n").local8Bit().data());
83 display = ::getenv(
"DISPLAY");
85 display.replace(TQRegExp(
"\\.[0-9]+$"),
"");
86 if (display.isEmpty())
88 fprintf(stderr,
"%s", i18n(
"Error: DISPLAY environment variable not set.\n").local8Bit().data());
93 static QCStringList split(
const TQCString &str)
95 const char *s = str.data();
99 const char *i = strchr(s,
' ');
102 result.append(TQCString(s));
105 result.append(TQCString(s, i-s+1));
107 while (*s ==
' ') s++;
112 void KHostName::changeX()
114 const char* xauthlocalhostname = getenv(
"XAUTHLOCALHOSTNAME");
115 TQString cmd =
"xauth -n list";
116 FILE *xFile = popen(TQFile::encodeName(cmd),
"r");
119 fprintf(stderr,
"Warning: Can't run xauth.\n");
128 TQCString line = fgets(buf, 1024, xFile);
130 line.truncate(line.length()-1);
137 for(QCStringList::ConstIterator it = lines.begin();
138 it != lines.end(); ++it)
140 QCStringList entries = split(*it);
141 if (entries.count() != 3)
144 TQCString netId = entries[0];
145 TQCString authName = entries[1];
146 TQCString authKey = entries[2];
148 int i = netId.findRev(
':');
151 TQCString netDisplay = netId.mid(i);
152 if (netDisplay != display)
159 TQCString newNetId = newName+netId.mid(i);
160 TQCString oldNetId = netId.left(i);
162 if(oldNetId != oldName
163 && (!xauthlocalhostname || strcmp(xauthlocalhostname, oldNetId.data()) != 0))
167 if (!xauthlocalhostname || oldNetId != xauthlocalhostname)
169 cmd =
"xauth -n remove "+TDEProcess::quote(netId);
170 system(TQFile::encodeName(cmd));
172 cmd =
"xauth -n add ";
173 cmd += TDEProcess::quote(newNetId);
175 cmd += TDEProcess::quote(authName);
177 cmd += TDEProcess::quote(authKey);
178 system(TQFile::encodeName(cmd));
182 void KHostName::changeDcop()
184 TQCString origFNameOld = DCOPClient::dcopServerFileOld(oldName);
185 TQCString fname = DCOPClient::dcopServerFile(oldName);
186 TQCString origFName = fname;
187 FILE *dcopFile = fopen(fname.data(),
"r");
190 fprintf(stderr,
"Warning: Can't open '%s' for reading.\n", fname.data());
194 TQCString line1, line2;
197 line1 = fgets(buf, 1024, dcopFile);
199 line1.truncate(line1.length()-1);
201 line2 = fgets(buf, 1024, dcopFile);
203 line2.truncate(line2.length()-1);
207 TQCString oldNetId = line1;
209 if (!newName.isEmpty())
211 int i = line1.findRev(
':');
214 fprintf(stderr,
"Warning: File '%s' has unexpected format.\n", fname.data());
217 line1 =
"local/"+newName+line1.mid(i);
218 TQCString newNetId = line1;
219 fname = DCOPClient::dcopServerFile(newName);
220 unlink(fname.data());
221 dcopFile = fopen(fname.data(),
"w");
224 fprintf(stderr,
"Warning: Can't open '%s' for writing.\n", fname.data());
228 fputs(line1.data(), dcopFile);
229 fputc(
'\n', dcopFile);
230 fputs(line2.data(), dcopFile);
231 fputc(
'\n', dcopFile);
235 TQCString compatLink = DCOPClient::dcopServerFileOld(newName);
236 ::symlink(fname.data(), compatLink.data());
239 TQString cmd =
"iceauth list "+TDEProcess::quote(
"netid="+oldNetId);
240 FILE *iceFile = popen(TQFile::encodeName(cmd),
"r");
243 fprintf(stderr,
"Warning: Can't run iceauth.\n");
249 while (!feof(iceFile))
251 TQCString line = fgets(buf, 1024, iceFile);
253 line.truncate(line.length()-1);
260 for(QCStringList::ConstIterator it = lines.begin();
261 it != lines.end(); ++it)
263 QCStringList entries = split(*it);
264 if (entries.count() != 5)
267 TQCString protName = entries[0];
268 TQCString netId = entries[2];
269 TQCString authName = entries[3];
270 TQCString authKey = entries[4];
271 if (netId != oldNetId)
274 cmd =
"iceauth add ";
275 cmd += TDEProcess::quote(protName);
277 cmd += TDEProcess::quote(newNetId);
279 cmd += TDEProcess::quote(authName);
281 cmd += TDEProcess::quote(authKey);
282 system(TQFile::encodeName(cmd));
288 char* xauthlocalhostname = getenv(
"XAUTHLOCALHOSTNAME");
289 if (!xauthlocalhostname || !oldNetId.contains(xauthlocalhostname))
291 TQString cmd =
"iceauth remove "+TDEProcess::quote(
"netid="+oldNetId);
292 system(TQFile::encodeName(cmd));
293 unlink(origFName.data());
294 origFName = DCOPClient::dcopServerFileOld(oldName);
295 unlink(origFName.data());
299 void KHostName::changeStdDirs(
const TQCString &type)
302 TQCString oldDir = TQFile::encodeName(TQString(
"%1%2-%3").arg(TDEGlobal::dirs()->localtdedir()).arg(type.data()).arg(oldName.data()));
303 TQCString newDir = TQFile::encodeName(TQString(
"%1%2-%3").arg(TDEGlobal::dirs()->localtdedir()).arg(type.data()).arg(newName.data()));
305 KDE_struct_stat st_buf;
307 int result = KDE_lstat(oldDir.data(), &st_buf);
310 if (S_ISLNK(st_buf.st_mode))
313 result = readlink(oldDir.data(), buf, 4096);
317 result = symlink(buf, newDir.data());
320 else if (S_ISDIR(st_buf.st_mode))
322 result = symlink(oldDir.data(), newDir.data());
331 system((
"lnusertemp "+type).data());
335 void KHostName::changeSessionManager()
337 TQCString sm = ::getenv(
"SESSION_MANAGER");
340 fprintf(stderr,
"Warning: No session management specified.\n");
343 int i = sm.findRev(
':');
344 if ((i == -1) || (sm.left(6) !=
"local/"))
346 fprintf(stderr,
"Warning: Session Management socket '%s' has unexpected format.\n", sm.data());
349 sm =
"local/"+newName+sm.mid(i);
350 TQCString name =
"SESSION_MANAGER";
352 TQDataStream stream(params, IO_WriteOnly);
353 stream << name << sm;
354 DCOPClient *client =
new DCOPClient();
355 if (!client->attach())
357 fprintf(stderr,
"Warning: DCOP communication problem, can't fix Session Management.\n");
361 TQCString launcher = TDEApplication::launcher();
362 client->send(launcher, launcher,
"setLaunchEnv(TQCString,TQCString)", params);
366 int main(
int argc,
char **argv)
368 TDELocale::setMainCatalogue(
"tdelibs");
369 TDEAboutData d(appName, I18N_NOOP(
"KDontChangeTheHostName"), appVersion,
370 I18N_NOOP(
"Informs TDE about a change in hostname"),
371 TDEAboutData::License_GPL,
"(c) 2001 Waldo Bastian");
372 d.addAuthor(
"Waldo Bastian", I18N_NOOP(
"Author"),
"bastian@kde.org");
374 TDECmdLineArgs::init(argc, argv, &d);
375 TDECmdLineArgs::addCmdLineOptions(options);
383 hn.changeStdDirs(
"socket");
384 hn.changeStdDirs(
"tmp");
385 hn.changeSessionManager();