File: khostname.cpp

package info (click to toggle)
kde-runtime 4%3A17.08.3-2.1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 25,204 kB
  • sloc: cpp: 111,675; ansic: 5,030; perl: 1,579; xml: 793; sh: 407; makefile: 42; python: 28
file content (238 lines) | stat: -rw-r--r-- 6,438 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*  This file is part of the KDE libraries
 *  Copyright (C) 2001 Waldo Bastian <bastian@kde.org>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License version 2 as published by the Free Software Foundation;
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 **/

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

#include <QtCore/QFile>
#include <QtCore/QRegExp>
#include <QtCore/Q_PID>

#include <kcmdlineargs.h>
#include <kapplication.h>
#include <klocale.h>
#include <kaboutdata.h>
#include <kglobal.h>
#include <kstandarddirs.h>
#include <ktoolinvocation.h>
#include <klauncher_iface.h>
#include <kde_file.h>
#include <QtDBus/QtDBus>

static const char appName[] = "kdontchangethehostname";
static const char appVersion[] = "1.1";

class KHostName
{
public:
   KHostName();

   void changeX();
   void changeStdDirs(const QByteArray &type);
   void changeSessionManager();

protected:
   QString oldName;
   QString newName;
   QString display;
   QByteArray home;
};

KHostName::KHostName()
{
   KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
   if (args->count() != 2)
      args->usage();
   oldName = args->arg(0);
   newName = args->arg(1);
   if (oldName == newName)
      exit(0);

   home = qgetenv("HOME");
   if (home.isEmpty())
   {
      fprintf(stderr, "%s", i18n("Error: HOME environment variable not set.\n").toLocal8Bit().data());
      exit(1);
   }

   display = QString::fromLocal8Bit(qgetenv("DISPLAY"));
   // strip the screen number from the display
   display.remove(QRegExp("\\.[0-9]+$"));
#if defined(Q_WS_X11) || defined(Q_WS_QWS)
   if (display.isEmpty())
   {
      fprintf(stderr, "%s", i18n("Error: DISPLAY environment variable not set.\n").toLocal8Bit().data());
      exit(1);
   }
#endif
}

static QList<QByteArray> split(const QByteArray &str)
{
   const char *s = str.data();
   QList<QByteArray> result;
   while (*s)
   {
      const char *i = strchr(s, ' ');
      if (!i)
      {
         result.append(QByteArray(s));
         return result;
      }
      result.append(QByteArray(s, i-s+1));
      s = i;
      while (*s == ' ') s++;
   }
   return result;
}

void KHostName::changeX()
{
   QProcess proc;
   proc.start("xauth", QStringList() << "-n" << "list");
   if (!proc.waitForFinished())
   {
      fprintf(stderr, "Warning: Can not run xauth.\n");
      return;
   }
   QList<QByteArray> lines;
   {
      while (!proc.atEnd())
      {
         QByteArray line = proc.readLine();
         if (line.length())
            line.truncate(line.length()-1); // Strip LF.
         if (!line.isEmpty())
            lines.append(line);
      }
   }

   foreach ( const QByteArray &it, lines )
   {
      QList<QByteArray> entries = split(it);
      if (entries.count() != 3)
         continue;

      QByteArray netId = entries[0];
      QByteArray authName = entries[1];
      QByteArray authKey = entries[2];

      int i = netId.lastIndexOf(':');
      if (i == -1)
         continue;
      QByteArray netDisplay = netId.mid(i);
      if (netDisplay != display)
         continue;

      i = netId.indexOf('/');
      if (i == -1)
         continue;

      QString newNetId = newName+netId.mid(i);
      QString oldNetId = netId.left(i);

      if (oldNetId != oldName)
	continue;

      QProcess::execute("xauth", QStringList() << "-n" << "remove" << netId);
      QProcess::execute("xauth", QStringList() << "-n" << "add" << newNetId << authName << authKey);
   }
}

void KHostName::changeStdDirs(const QByteArray &type)
{
   // We make links to the old dirs cause we can't delete the old dirs.
   QByteArray oldDir = QFile::encodeName(QString("%1%2-%3").arg(KGlobal::dirs()->localkdedir()).arg(QString( type )).arg(QString( oldName )));
   QByteArray newDir = QFile::encodeName(QString("%1%2-%3").arg(KGlobal::dirs()->localkdedir()).arg(QString( type )).arg(QString( newName )));

   KDE_struct_stat st_buf;

   int result = KDE_lstat(oldDir.data(), &st_buf);
   if (result == 0)
   {
      if (S_ISLNK(st_buf.st_mode))
      {
         char buf[4096+1];
         result = readlink(oldDir.data(), buf, 4096);
         if (result >= 0)
         {
            buf[result] = 0;
            result = symlink(buf, newDir.data());
         }
      }
      else if (S_ISDIR(st_buf.st_mode))
      {
         result = symlink(oldDir.data(), newDir.data());
      }
      else
      {
         result = -1;
      }
   }
   if (result != 0)
   {
       const QString lnusertemp = KGlobal::dirs()->findExe( "lnusertemp" );
       QProcess::execute( lnusertemp, QStringList() << type );
   }
}

void KHostName::changeSessionManager()
{
   QString sm = QString::fromLocal8Bit(qgetenv("SESSION_MANAGER"));
   if (sm.isEmpty())
   {
      fprintf(stderr, "Warning: No session management specified.\n");
      return;
   }
   int i = sm.lastIndexOf(':');
   if ((i == -1) || (sm.left(6) != "local/"))
   {
      fprintf(stderr, "Warning: Session Management socket '%s' has unexpected format.\n", sm.toLocal8Bit().constData());
      return;
   }
   sm = "local/"+newName+sm.mid(i);
   KToolInvocation::klauncher()->call(QDBus::NoBlock, "setLaunchEnv", QByteArray("SESSION_MANAGER"), sm);
}

int main(int argc, char **argv)
{
   KAboutData d(appName, "kdelibs4", ki18n("KDontChangeTheHostName"), appVersion,
                ki18n("Informs KDE about a change in hostname"),
                KAboutData::License_GPL, ki18n("(c) 2001 Waldo Bastian"));
   d.addAuthor(ki18n("Waldo Bastian"), ki18n("Author"), "bastian@kde.org");

   KCmdLineOptions options;
   options.add("+old", ki18n("Old hostname"));
   options.add("+new", ki18n("New hostname"));

   KCmdLineArgs::init(argc, argv, &d);
   KCmdLineArgs::addCmdLineOptions(options);

   KComponentData k(&d);

   KHostName hn;

   hn.changeX();
   hn.changeStdDirs("socket");
   hn.changeStdDirs("tmp");
   hn.changeSessionManager();
}