summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings2/networksettings2/system.cpp
Unidiff
Diffstat (limited to 'noncore/settings/networksettings2/networksettings2/system.cpp') (more/less context) (show whitespace changes)
-rw-r--r--noncore/settings/networksettings2/networksettings2/system.cpp100
1 files changed, 81 insertions, 19 deletions
diff --git a/noncore/settings/networksettings2/networksettings2/system.cpp b/noncore/settings/networksettings2/networksettings2/system.cpp
index 2133d34..a579396 100644
--- a/noncore/settings/networksettings2/networksettings2/system.cpp
+++ b/noncore/settings/networksettings2/networksettings2/system.cpp
@@ -5,21 +5,23 @@
5#include <net/if_arp.h> 5#include <net/if_arp.h>
6#include <netinet/in.h> 6#include <netinet/in.h>
7#include <arpa/inet.h> 7#include <arpa/inet.h>
8#include <sys/ioctl.h> 8#include <sys/ioctl.h>
9#include <sys/socket.h> 9#include <sys/socket.h>
10#include <stdlib.h> 10#include <stdlib.h>
11#include <stdio.h>
11#include <fcntl.h> 12#include <fcntl.h>
12#include <errno.h> 13#include <errno.h>
13#include <unistd.h> 14#include <unistd.h>
14 15
15#include <qdir.h> 16#include <qdir.h>
16#include <qregexp.h> 17#include <qregexp.h>
17#include <qstringlist.h> 18#include <qstringlist.h>
18#include <qfile.h> 19#include <qfile.h>
19#include <qtextstream.h> 20#include <qtextstream.h>
21#include <qapplication.h>
20 22
21#include "resources.h" 23#include "resources.h"
22#include "system.h" 24#include "system.h"
23 25
24#define PROCNETDEV "/proc/net/dev" 26#define PROCNETDEV "/proc/net/dev"
25 27
@@ -32,51 +34,76 @@ static char Dig2Hex[] = {
32 34
33// get HIGH nibble of byte 35// get HIGH nibble of byte
34#define HN(x) Dig2Hex[(((x)&0xf0)>>4)] 36#define HN(x) Dig2Hex[(((x)&0xf0)>>4)]
35// get LOW nibble of byte 37// get LOW nibble of byte
36#define LN(x) Dig2Hex[((x)&0x0f)] 38#define LN(x) Dig2Hex[((x)&0x0f)]
37 39
38System::System( void ) : ProbedInterfaces() { 40System::System( void ) : QObject(), ProbedInterfaces() {
39 probeInterfaces(); 41 probeInterfaces();
40} 42}
41 43
42System::~System( void ) { 44System::~System( void ) {
43 if( ProcDevNet ) 45 if( ProcDevNet )
44 delete ProcDevNet; 46 delete ProcDevNet;
45} 47}
46 48
47int System::runAsRoot( const QString & S ) { 49int System::runAsRoot( const QString & S ) {
48 QString MyS = S; 50 QString MyS = S;
49 char * usr = getenv("USER"); 51 char * usr = getenv("USER");
50 int rv; 52 char ch;
51 53
52 if( S.isEmpty() ) { 54 if( S.isEmpty() ) {
53 // loophole to start shell 55 // loophole to start shell
54 return 8888; 56 return 8888;
55 } 57 }
56 if( usr == 0 || strcmp( usr, "root" ) ) { 58 if( usr == 0 || strcmp( usr, "root" ) ) {
57 // unknown or non-root user -> use SUDO 59 // unknown or non-root user -> use SUDO
58 MyS.prepend( "sudo " ); 60 MyS.prepend( "sudo " );
59 } 61 }
60 62
61 fprintf( stderr, "Executing %s\n", MyS.latin1() ); 63 Log(("Executing %s\n", MyS.latin1() ));
62 64
63 rv = system( MyS.latin1() ) ; 65 emit lineFromCommand( tr("Command : ") + MyS );
64 switch( rv ) { 66 emit lineFromCommand( "---------------" );
65 case -1 : 67 Log(( "Command : %s\n", MyS.latin1() ) );
68 MyS += " 2>&1 ";
69 OutputOfCmd = popen( MyS.latin1(), "r" ) ;
70 if( ! OutputOfCmd ) {
66 // cannot fork 71 // cannot fork
67 return 1; 72 return 1;
68 case 127 : 73 }
69 // cannot start shell 74
70 return 2; 75 // read all data
71 default : 76 QString Line = "";
72 if( WEXITSTATUS(rv) != 0 ) { 77 while( 1 ) {
78 if( fread( &ch, 1, 1, OutputOfCmd ) < 1 )
79 // eof
80 break;
81 if( ch == '\n' || ch == '\r' ) {
82 if( ! Line.isEmpty() ) {
83 Log(( "read cmd output : **%s**\n", Line.latin1() ) );
84 emit lineFromCommand( Line );
85 Line = "";
86 qApp->processEvents();
87 }
88 } else {
89 Line += ch;
90 }
91 }
92
93 if( ! Line.isEmpty() ) {
94 emit lineFromCommand( Line );
95 Log(( "read cmd output : **%s**\n", Line.latin1() ) );
96 }
97 Log(( "End of command\n", Line.latin1() ) );
98
99 if( pclose( OutputOfCmd ) < 0 ) {
73 // error in command 100 // error in command
74 return 3; 101 return 3;
75 } 102 }
76 } 103
77 // all is fine 104 // all is fine
78 return 0; 105 return 0;
79} 106}
80 107
81void System::refreshStatistics( InterfaceInfo & I ) { 108void System::refreshStatistics( InterfaceInfo & I ) {
82 if( ! ProcDevNet ) { 109 if( ! ProcDevNet ) {
@@ -195,13 +222,13 @@ void System::probeInterfaces( void ) {
195 222
196 // set name for ioctl 223 // set name for ioctl
197 strcpy( ifrs.ifr_name, NicName.latin1() ); 224 strcpy( ifrs.ifr_name, NicName.latin1() );
198 225
199 if ( ! ( IFI = ProbedInterfaces.find( NicName ) ) ) { 226 if ( ! ( IFI = ProbedInterfaces.find( NicName ) ) ) {
200 // new nic 227 // new nic
201 fprintf( stderr, "NEWNIC %s\n", NicName.latin1()); 228 Log(("NEWNIC %s\n", NicName.latin1()));
202 IFI = new InterfaceInfo; 229 IFI = new InterfaceInfo;
203 IFI->Name = line.left(loc); 230 IFI->Name = line.left(loc);
204 IFI->NetNode = 0; 231 IFI->NetNode = 0;
205 ProbedInterfaces.insert( IFI->Name, IFI ); 232 ProbedInterfaces.insert( IFI->Name, IFI );
206 233
207 // get dynamic info 234 // get dynamic info
@@ -222,14 +249,14 @@ void System::probeInterfaces( void ) {
222 } 249 }
223 250
224 IFI->CardType = 999999; 251 IFI->CardType = 999999;
225 IFI->MACAddress = ""; 252 IFI->MACAddress = "";
226 253
227 if( ioctl(sockfd, SIOCGIFHWADDR, &ifrs) >= 0 ) { 254 if( ioctl(sockfd, SIOCGIFHWADDR, &ifrs) >= 0 ) {
228 fprintf( stderr, "%s = %d\n", IFI->Name.latin1(), 255 Log(("%s = %d\n", IFI->Name.latin1(),
229 ifrs.ifr_hwaddr.sa_family ); 256 ifrs.ifr_hwaddr.sa_family ));
230 257
231 IFI->CardType = ifrs.ifr_hwaddr.sa_family; 258 IFI->CardType = ifrs.ifr_hwaddr.sa_family;
232 switch( ifrs.ifr_hwaddr.sa_family ) { 259 switch( ifrs.ifr_hwaddr.sa_family ) {
233 case ARPHRD_ETHER : // regular MAC address 260 case ARPHRD_ETHER : // regular MAC address
234 // valid address -> convert to regular ::: format 261 // valid address -> convert to regular ::: format
235 // length = 6 bytes = 12 DIGITS -> 6 : 262 // length = 6 bytes = 12 DIGITS -> 6 :
@@ -290,13 +317,13 @@ void System::probeInterfaces( void ) {
290 break; 317 break;
291 case ARPHRD_IRDA : // IRDA 318 case ARPHRD_IRDA : // IRDA
292 break; 319 break;
293 } 320 }
294 } 321 }
295 } else // else already probed before -> just update 322 } else // else already probed before -> just update
296 fprintf( stderr, "OLDNIC %s\n", NicName.latin1()); 323 Log(("OLDNIC %s\n", NicName.latin1()));
297 324
298 // get dynamic info 325 // get dynamic info
299 if( ioctl(sockfd, SIOCGIFFLAGS, &ifrs) >= 0 ) { 326 if( ioctl(sockfd, SIOCGIFFLAGS, &ifrs) >= 0 ) {
300 IFI->IsUp = ((ifrs.ifr_flags & IFF_UP) == IFF_UP); 327 IFI->IsUp = ((ifrs.ifr_flags & IFF_UP) == IFF_UP);
301 IFI->HasMulticast = ((ifrs.ifr_flags & IFF_MULTICAST) == IFF_MULTICAST); 328 IFI->HasMulticast = ((ifrs.ifr_flags & IFF_MULTICAST) == IFF_MULTICAST);
302 } else { 329 } else {
@@ -320,22 +347,22 @@ void System::probeInterfaces( void ) {
320 if( ioctl(sockfd, SIOCGIFNETMASK, &ifrs) >= 0 ) { 347 if( ioctl(sockfd, SIOCGIFNETMASK, &ifrs) >= 0 ) {
321 IFI->Netmask = 348 IFI->Netmask =
322 inet_ntoa(((struct sockaddr_in*)&ifrs.ifr_netmask)->sin_addr); 349 inet_ntoa(((struct sockaddr_in*)&ifrs.ifr_netmask)->sin_addr);
323 } else { 350 } else {
324 IFI->Netmask = ""; 351 IFI->Netmask = "";
325 } 352 }
326 fprintf( stderr, "NIC %s UP %d\n", NicName.latin1(), IFI->IsUp ); 353 Log(("NIC %s UP %d\n", NicName.latin1(), IFI->IsUp ));
327 } 354 }
328} 355}
329 356
330void System::execAsUser( QString & Cmd, char * argv[] ) { 357void System::execAsUser( QString & Cmd, char * argv[] ) {
331 CurrentQPEUser CU = NSResources->currentUser(); 358 CurrentQPEUser CU = NSResources->currentUser();
332 359
333 if( CU.UserName.isEmpty() ) { 360 if( CU.UserName.isEmpty() ) {
334 // if we come here, the exec was not successfull 361 // if we come here, the exec was not successfull
335 fprintf( stderr, "User not known \n" ); 362 Log(("User not known \n" ));
336 return; 363 return;
337 } 364 }
338 365
339 // now we are ready to exec the requested command 366 // now we are ready to exec the requested command
340 setuid( CU.Uid ); 367 setuid( CU.Uid );
341 setgid( CU.Gid ); 368 setgid( CU.Gid );
@@ -348,8 +375,43 @@ void System::execAsUser( QString & Cmd, char * argv[] ) {
348 } 375 }
349 envp[CU.EnvList.count()]=NULL; 376 envp[CU.EnvList.count()]=NULL;
350 377
351 execve( Cmd.latin1(), argv, envp ); 378 execve( Cmd.latin1(), argv, envp );
352 379
353 // if we come here, the exec was not successfull 380 // if we come here, the exec was not successfull
354 fprintf( stderr, "Could not exec : %d\n", errno ); 381 Log(("Could not exec : %d\n", errno ));
382}
383
384#include <stdarg.h>
385static FILE * logf = 0;
386
387void VLog( char * Format, ... ) {
388 va_list l;
389
390 va_start(l, Format );
391
392 if( logf == (FILE *)0 ) {
393 // logf = fopen( "/tmp/ns2log", "a" );
394 logf = stderr;
395 if( ! logf ) {
396 fprintf( stderr, "Cannot open logfile /tmp/ns2log %d\n",
397 errno );
398 logf = (FILE *)1;
399 } else {
400 fprintf( logf, "____ OPEN LOGFILE ____\n");
401 }
402 }
403
404 if( (long)logf > 1 ) {
405 vfprintf( logf, Format, l );
406 }
407 va_end( l );
408
409}
410
411void LogClose( void ) {
412 if( (long)logf > 1 ) {
413 fprintf( logf, "____ CLOSE LOGFILE ____\n");
414 fclose( logf );
415 logf = 0;
416 }
355} 417}