• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kded
 

kded

  • kded
khostname.cpp
1 /* This file is part of the KDE libraries
2  * Copyright (C) 2001 Waldo Bastian <bastian@kde.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License version 2 as published by the Free Software Foundation;
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Library General Public License for more details.
12  *
13  * You should have received a copy of the GNU Library General Public License
14  * along with this library; see the file COPYING.LIB. If not, write to
15  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  * Boston, MA 02110-1301, USA.
17  **/
18 
19 #include <config.h>
20 
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 
27 #include <tqfile.h>
28 #include <tqtextstream.h>
29 #include <tqregexp.h>
30 
31 #include <dcopclient.h>
32 
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>
40 #include <kde_file.h>
41 
42 static TDECmdLineOptions options[] = {
43  { "+old", I18N_NOOP("Old hostname"), 0 },
44  { "+new", I18N_NOOP("New hostname"), 0 },
45  TDECmdLineLastOption
46 };
47 
48 static const char appName[] = "kdontchangethehostname";
49 static const char appVersion[] = "1.1";
50 
51 class KHostName
52 {
53 public:
54  KHostName();
55 
56  void changeX();
57  void changeDcop();
58  void changeStdDirs(const TQCString &type);
59  void changeSessionManager();
60 
61 protected:
62  TQCString oldName;
63  TQCString newName;
64  TQCString display;
65  TQCString home;
66 };
67 
68 KHostName::KHostName()
69 {
70  TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
71  if (args->count() != 2)
72  args->usage();
73  oldName = args->arg(0);
74  newName = args->arg(1);
75  if (oldName == newName)
76  exit(0);
77 
78  home = ::getenv("HOME");
79  if (home.isEmpty())
80  {
81  fprintf(stderr, "%s", i18n("Error: HOME environment variable not set.\n").local8Bit().data());
82  exit(1);
83  }
84 
85  display = ::getenv("DISPLAY");
86  // strip the screen number from the display
87  display.replace(TQRegExp("\\.[0-9]+$"), "");
88  if (display.isEmpty())
89  {
90  fprintf(stderr, "%s", i18n("Error: DISPLAY environment variable not set.\n").local8Bit().data());
91  exit(1);
92  }
93 }
94 
95 static QCStringList split(const TQCString &str)
96 {
97  const char *s = str.data();
98  QCStringList result;
99  while (*s)
100  {
101  const char *i = strchr(s, ' ');
102  if (!i)
103  {
104  result.append(TQCString(s));
105  return result;
106  }
107  result.append(TQCString(s, i-s+1));
108  s = i;
109  while (*s == ' ') s++;
110  }
111  return result;
112 }
113 
114 void KHostName::changeX()
115 {
116  const char* xauthlocalhostname = getenv("XAUTHLOCALHOSTNAME");
117  TQString cmd = "xauth -n list";
118  FILE *xFile = popen(TQFile::encodeName(cmd), "r");
119  if (!xFile)
120  {
121  fprintf(stderr, "Warning: Can't run xauth.\n");
122  return;
123  }
124  QCStringList lines;
125  {
126  char buf[1024+1];
127  while (!feof(xFile))
128  {
129  buf[1024]='\0';
130  TQCString line = fgets(buf, 1024, xFile);
131  if (line.length())
132  line.truncate(line.length()-1); // Strip LF.
133  if (!line.isEmpty())
134  lines.append(line);
135  }
136  }
137  pclose(xFile);
138 
139  for(QCStringList::ConstIterator it = lines.begin();
140  it != lines.end(); ++it)
141  {
142  QCStringList entries = split(*it);
143  if (entries.count() != 3)
144  continue;
145 
146  TQCString netId = entries[0];
147  TQCString authName = entries[1];
148  TQCString authKey = entries[2];
149 
150  int i = netId.findRev(':');
151  if (i == -1)
152  continue;
153  TQCString netDisplay = netId.mid(i);
154  if (netDisplay != display)
155  continue;
156 
157  i = netId.find('/');
158  if (i == -1)
159  continue;
160 
161  TQCString newNetId = newName+netId.mid(i);
162  TQCString oldNetId = netId.left(i);
163 
164  if(oldNetId != oldName
165  && (!xauthlocalhostname || strcmp(xauthlocalhostname, oldNetId.data()) != 0))
166  continue;
167 
168  // don't nuke the xauth when XAUTHLOCALHOSTNAME points to it
169  if (!xauthlocalhostname || oldNetId != xauthlocalhostname)
170  {
171  cmd = "xauth -n remove "+TDEProcess::quote(netId);
172  system(TQFile::encodeName(cmd));
173  }
174  cmd = "xauth -n add ";
175  cmd += TDEProcess::quote(newNetId);
176  cmd += " ";
177  cmd += TDEProcess::quote(authName);
178  cmd += " ";
179  cmd += TDEProcess::quote(authKey);
180  system(TQFile::encodeName(cmd));
181  }
182 }
183 
184 void KHostName::changeDcop()
185 {
186  TQCString origFNameOld = DCOPClient::dcopServerFileOld(oldName);
187  TQCString fname = DCOPClient::dcopServerFile(oldName);
188  TQCString origFName = fname;
189  FILE *dcopFile = fopen(fname.data(), "r");
190  if (!dcopFile)
191  {
192  fprintf(stderr, "Warning: Can't open '%s' for reading.\n", fname.data());
193  return;
194  }
195 
196  TQCString line1, line2;
197  {
198  char buf[1024+1];
199  line1 = fgets(buf, 1024, dcopFile);
200  if (line1.length())
201  line1.truncate(line1.length()-1); // Strip LF.
202 
203  line2 = fgets(buf, 1024, dcopFile);
204  if (line2.length())
205  line2.truncate(line2.length()-1); // Strip LF.
206  }
207  fclose(dcopFile);
208 
209  TQCString oldNetId = line1;
210 
211  if (!newName.isEmpty())
212  {
213  int i = line1.findRev(':');
214  if (i == -1)
215  {
216  fprintf(stderr, "Warning: File '%s' has unexpected format.\n", fname.data());
217  return;
218  }
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");
224  if (!dcopFile)
225  {
226  fprintf(stderr, "Warning: Can't open '%s' for writing.\n", fname.data());
227  return;
228  }
229 
230  fputs(line1.data(), dcopFile);
231  fputc('\n', dcopFile);
232  fputs(line2.data(), dcopFile);
233  fputc('\n', dcopFile);
234 
235  fclose(dcopFile);
236 
237  TQCString compatLink = DCOPClient::dcopServerFileOld(newName);
238  ::symlink(fname.data(), compatLink.data()); // Compatibility link
239 
240  // Update .ICEauthority
241  TQString cmd = ICEAUTH_COMMAND " list "+TDEProcess::quote("netid="+oldNetId);
242  FILE *iceFile = popen(TQFile::encodeName(cmd), "r");
243  if (!iceFile)
244  {
245  fprintf(stderr, "Warning: Can't run iceauth.\n");
246  return;
247  }
248  QCStringList lines;
249  {
250  char buf[1024+1];
251  while (!feof(iceFile))
252  {
253  TQCString line = fgets(buf, 1024, iceFile);
254  if (line.length())
255  line.truncate(line.length()-1); // Strip LF.
256  if (!line.isEmpty())
257  lines.append(line);
258  }
259  }
260  pclose(iceFile);
261 
262  for(QCStringList::ConstIterator it = lines.begin();
263  it != lines.end(); ++it)
264  {
265  QCStringList entries = split(*it);
266  if (entries.count() != 5)
267  continue;
268 
269  TQCString protName = entries[0];
270  TQCString netId = entries[2];
271  TQCString authName = entries[3];
272  TQCString authKey = entries[4];
273  if (netId != oldNetId)
274  continue;
275 
276  cmd = ICEAUTH_COMMAND " add ";
277  cmd += TDEProcess::quote(protName);
278  cmd += " '' ";
279  cmd += TDEProcess::quote(newNetId);
280  cmd += " ";
281  cmd += TDEProcess::quote(authName);
282  cmd += " ";
283  cmd += TDEProcess::quote(authKey);
284  system(TQFile::encodeName(cmd));
285  }
286  }
287 
288  // Remove old entries, but only if XAUTHLOCALHOSTNAME doesn't point
289  // to it
290  char* xauthlocalhostname = getenv("XAUTHLOCALHOSTNAME");
291  if (!xauthlocalhostname || !oldNetId.contains(xauthlocalhostname))
292  {
293  TQString cmd = ICEAUTH_COMMAND " remove "+TDEProcess::quote("netid="+oldNetId);
294  system(TQFile::encodeName(cmd));
295  unlink(origFName.data());
296  origFName = DCOPClient::dcopServerFileOld(oldName); // Compatibility link
297  unlink(origFName.data());
298  }
299 }
300 
301 void KHostName::changeStdDirs(const TQCString &type)
302 {
303  // We make links to the old dirs cause we can't delete the old dirs.
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()));
306 
307  KDE_struct_stat st_buf;
308 
309  int result = KDE_lstat(oldDir.data(), &st_buf);
310  if (result == 0)
311  {
312  if (S_ISLNK(st_buf.st_mode))
313  {
314  char buf[4096+1];
315  result = readlink(oldDir.data(), buf, 4096);
316  if (result >= 0)
317  {
318  buf[result] = 0;
319  result = symlink(buf, newDir.data());
320  }
321  }
322  else if (S_ISDIR(st_buf.st_mode))
323  {
324  result = symlink(oldDir.data(), newDir.data());
325  }
326  else
327  {
328  result = -1;
329  }
330  }
331  if (result != 0)
332  {
333  system(("lnusertemp "+type).data());
334  }
335 }
336 
337 void KHostName::changeSessionManager()
338 {
339  TQCString sm = ::getenv("SESSION_MANAGER");
340  if (sm.isEmpty())
341  {
342  fprintf(stderr, "Warning: No session management specified.\n");
343  return;
344  }
345  int i = sm.findRev(':');
346  if ((i == -1) || (sm.left(6) != "local/"))
347  {
348  fprintf(stderr, "Warning: Session Management socket '%s' has unexpected format.\n", sm.data());
349  return;
350  }
351  sm = "local/"+newName+sm.mid(i);
352  TQCString name = "SESSION_MANAGER";
353  TQByteArray params;
354  TQDataStream stream(params, IO_WriteOnly);
355  stream << name << sm;
356  DCOPClient *client = new DCOPClient();
357  if (!client->attach())
358  {
359  fprintf(stderr, "Warning: DCOP communication problem, can't fix Session Management.\n");
360  delete client;
361  return;
362  }
363  TQCString launcher = TDEApplication::launcher();
364  client->send(launcher, launcher, "setLaunchEnv(TQCString,TQCString)", params);
365  delete client;
366 }
367 
368 int main(int argc, char **argv)
369 {
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");
375 
376  TDECmdLineArgs::init(argc, argv, &d);
377  TDECmdLineArgs::addCmdLineOptions(options);
378 
379  TDEInstance k(&d);
380 
381  KHostName hn;
382 
383  hn.changeX();
384  hn.changeDcop();
385  hn.changeStdDirs("socket");
386  hn.changeStdDirs("tmp");
387  hn.changeSessionManager();
388 }
389 

kded

Skip menu "kded"
  • Main Page
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kded

Skip menu "kded"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for kded by doxygen 1.9.1
This website is maintained by Timothy Pearson.