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

tdesu

  • tdesu
kcookie.cpp
1 /*
2  *
3  * $Id$
4  *
5  * This file is part of the KDE project, module tdesu.
6  * Copyright (C) 1999,2000 Geert Jansen <jansen@kde.org>
7  *
8  * This is free software; you can use this library under the GNU Library
9  * General Public License, version 2. See the file "COPYING.LIB" for the
10  * exact licensing terms.
11  *
12  * kcookie.cpp: KDE authentication cookies.
13  */
14 
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <errno.h>
20 #include <signal.h>
21 
22 #include <tqstring.h>
23 #include <tqstringlist.h>
24 #include <tqglobal.h>
25 #include <tqfile.h>
26 
27 #include <dcopclient.h>
28 
29 #include <kdebug.h>
30 #include <kprocess.h>
31 #include "kcookie.h"
32 
33 
34 KCookie::KCookie()
35 {
36 #ifdef Q_WS_X11
37  getXCookie();
38 #endif
39  setDcopTransport("local");
40 }
41 
42 void KCookie::setDcopTransport(const TQCString &dcopTransport)
43 {
44  m_dcopTransport = dcopTransport;
45  m_bHaveDCOPCookies = false;
46  m_bHaveICECookies = false;
47  m_DCOPSrv = "";
48  m_DCOPAuth = "";
49  m_ICEAuth = "";
50 }
51 
52 QCStringList KCookie::split(const TQCString &line, char ch)
53 {
54  QCStringList result;
55 
56  int i=0, pos;
57  while ((pos = line.find(ch, i)) != -1)
58  {
59  result += line.mid(i, pos-i);
60  i = pos+1;
61  }
62  if (i < (int) line.length())
63  result += line.mid(i);
64  return result;
65 }
66 
67 void KCookie::blockSigChild()
68 {
69  sigset_t sset;
70  sigemptyset(&sset);
71  sigaddset(&sset, SIGCHLD);
72  sigprocmask(SIG_BLOCK, &sset, 0L);
73 }
74 
75 void KCookie::unblockSigChild()
76 {
77  sigset_t sset;
78  sigemptyset(&sset);
79  sigaddset(&sset, SIGCHLD);
80  sigprocmask(SIG_UNBLOCK, &sset, 0L);
81 }
82 
83 void KCookie::getXCookie()
84 {
85  char buf[1024];
86  FILE *f;
87 
88 #ifdef Q_WS_X11
89  m_Display = getenv("DISPLAY");
90 #else
91  m_Display = getenv("QWS_DISPLAY");
92 #endif
93  if (m_Display.isEmpty())
94  {
95  kdError(900) << k_lineinfo << "$DISPLAY is not set.\n";
96  return;
97  }
98 #ifdef Q_WS_X11 // No need to mess with X Auth stuff
99  TQCString disp = m_Display;
100  if (!memcmp(disp.data(), "localhost:", 10))
101  disp.remove(0, 9);
102 
103  TQString cmd = "xauth list "+TDEProcess::quote(disp);
104  blockSigChild(); // pclose uses waitpid()
105  if (!(f = popen(TQFile::encodeName(cmd), "r")))
106  {
107  kdError(900) << k_lineinfo << "popen(): " << perror << "\n";
108  unblockSigChild();
109  return;
110  }
111  TQCString output = fgets(buf, 1024, f);
112  if (pclose(f) < 0)
113  {
114  kdError(900) << k_lineinfo << "Could not run xauth.\n";
115  unblockSigChild();
116  return;
117  }
118  unblockSigChild();
119  output = output.simplifyWhiteSpace();
120  if (output.isEmpty())
121  {
122  kdWarning(900) << "No X authentication info set for display " <<
123  m_Display << endl; return;
124  }
125  QCStringList lst = split(output, ' ');
126  if (lst.count() != 3)
127  {
128  kdError(900) << k_lineinfo << "parse error.\n";
129  return;
130  }
131  m_DisplayAuth = (lst[1] + ' ' + lst[2]);
132 #endif
133 }
134 
135 void KCookie::getICECookie()
136 {
137  FILE *f;
138  char buf[1024];
139 
140  TQCString dcopsrv = getenv("DCOPSERVER");
141  if (dcopsrv.isEmpty())
142  {
143  TQCString dcopFile = DCOPClient::dcopServerFile();
144  if (!(f = fopen(dcopFile, "r")))
145  {
146  kdWarning(900) << k_lineinfo << "Cannot open " << dcopFile << ".\n";
147  return;
148  }
149  dcopsrv = fgets(buf, 1024, f);
150  dcopsrv = dcopsrv.stripWhiteSpace();
151  fclose(f);
152  }
153  QCStringList dcopServerList = split(dcopsrv, ',');
154  if (dcopServerList.isEmpty())
155  {
156  kdError(900) << k_lineinfo << "No DCOP servers found.\n";
157  return;
158  }
159 
160  QCStringList::Iterator it;
161  for (it=dcopServerList.begin(); it != dcopServerList.end(); ++it)
162  {
163  if (strncmp((*it).data(), m_dcopTransport.data(), m_dcopTransport.length()) != 0)
164  continue;
165  m_DCOPSrv = *it;
166  TQCString cmd = DCOPClient::iceauthPath()+" list netid="+TQFile::encodeName(TDEProcess::quote(m_DCOPSrv));
167  blockSigChild();
168  if (!(f = popen(cmd, "r")))
169  {
170  kdError(900) << k_lineinfo << "popen(): " << perror << "\n";
171  unblockSigChild();
172  break;
173  }
174  QCStringList output;
175  while (fgets(buf, 1024, f))
176  output += buf;
177  if (pclose(f) < 0)
178  {
179  kdError(900) << k_lineinfo << "Could not run iceauth.\n";
180  unblockSigChild();
181  break;
182  }
183  unblockSigChild();
184  QCStringList::Iterator it2;
185  for (it2=output.begin(); it2!=output.end(); ++it2)
186  {
187  QCStringList lst = split((*it2).simplifyWhiteSpace(), ' ');
188  if (lst.count() != 5)
189  {
190  kdError(900) << "parse error.\n";
191  break;
192  }
193  if (lst[0] == "DCOP")
194  m_DCOPAuth = (lst[3] + ' ' + lst[4]);
195  else if (lst[0] == "ICE")
196  m_ICEAuth = (lst[3] + ' ' + lst[4]);
197  else
198  kdError(900) << k_lineinfo << "unknown protocol: " << lst[0] << "\n";
199  }
200  break;
201  }
202  m_bHaveDCOPCookies = true;
203  m_bHaveICECookies = true;
204 }
205 
206 TQCString KCookie::dcopServer()
207 {
208  if (!m_bHaveDCOPCookies)
209  getICECookie();
210  return m_DCOPSrv;
211 }
212 
213 TQCString KCookie::dcopAuth()
214 {
215  if (!m_bHaveDCOPCookies)
216  getICECookie();
217  return m_DCOPAuth;
218 }
219 
220 TQCString KCookie::iceAuth()
221 {
222  if (!m_bHaveICECookies)
223  getICECookie();
224  return m_ICEAuth;
225 }
KCookie::dcopAuth
TQCString dcopAuth()
Returns a list of magic cookies for DCOP protocol authentication.
Definition: kcookie.cpp:213
KCookie::iceAuth
TQCString iceAuth()
Returns a list of magic cookies for the ICE protocol.
Definition: kcookie.cpp:220
KCookie::setDcopTransport
void setDcopTransport(const TQCString &dcopTransport)
Select the DCOP transport to look for.
Definition: kcookie.cpp:42
KCookie::dcopServer
TQCString dcopServer()
Returns the netid where the dcopserver is running.
Definition: kcookie.cpp:206

tdesu

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

tdesu

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