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

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.8.13
This website is maintained by Timothy Pearson.