summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings2/networksettings2/system.cpp
blob: 2133d34b5a8c06d68893786e0716155b8b844080 (plain)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include <sys/types.h>
#include <sys/wait.h>

#include <net/if.h>
#include <net/if_arp.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>

#include <qdir.h>
#include <qregexp.h>
#include <qstringlist.h>
#include <qfile.h>
#include <qtextstream.h>

#include "resources.h"
#include "system.h"

#define PROCNETDEV "/proc/net/dev"

static char Dig2Hex[] = { 
  '0', '1', '2', '3', 
  '4', '5', '6', '7', 
  '8', '9', 'A', 'B', 
  'C', 'D', 'E', 'F'
};

// get HIGH nibble of byte
#define HN(x)           Dig2Hex[(((x)&0xf0)>>4)]
// get LOW nibble of byte
#define LN(x)           Dig2Hex[((x)&0x0f)]

System::System( void ) : ProbedInterfaces() {
    probeInterfaces();
}

System::~System( void ) {
    if( ProcDevNet )
      delete ProcDevNet;
}

int System::runAsRoot( const QString & S ) {
    QString MyS = S;
    char * usr = getenv("USER");
    int rv;

    if( S.isEmpty() ) {
      // loophole to start shell
      return 8888;
    }
    if( usr == 0 || strcmp( usr, "root" ) ) {
      // unknown or non-root user -> use SUDO
      MyS.prepend( "sudo " );
    }

    fprintf( stderr, "Executing %s\n", MyS.latin1() );

    rv = system( MyS.latin1() ) ;
    switch( rv ) {
      case -1 :
        // cannot fork
        return 1;
      case 127 :
        // cannot start shell
        return 2;
      default :
        if( WEXITSTATUS(rv) != 0 ) {
          // error in command
          return 3;
        }
    }
    // all is fine
    return 0;
}

void System::refreshStatistics( InterfaceInfo & I ) {
    if( ! ProcDevNet ) {
      return;
    }
    // cannot seek on dev
    ProcDevNet->close();
    ProcDevNet->open( IO_ReadOnly );

    QString line;
    QTextStream procTs(ProcDevNet);
    QStringList SL;
    int loc = -1;
    int version;

    procTs.readLine();
    line = procTs.readLine();
    // get version
    if( line.find("compressed") )
      version = 3;
    else if( line.find( "bytes" ) )
      version = 2;
    else 
      version = 1;
    while((line = procTs.readLine().simplifyWhiteSpace()) != QString::null) {
      if( (loc = line.find(":") ) == -1) {
        continue;
      }

      if( I.Name != line.left(loc) ) 
        continue;

      // tokenize
      SL = QStringList::split( ' ', line, FALSE );

      // update data
      switch( version ) {
        case 1 :
          I.RcvBytes = SL[1];
          I.RcvErrors = SL[3];
          I.RcvDropped = SL[4];
          I.SndBytes = SL[6];
          I.SndErrors = SL[8];
          I.SndDropped = SL[9];
          I.Collisions = SL[11];
          break;
        case 2 :
          I.RcvBytes = SL[1];
          I.RcvErrors = SL[3];
          I.RcvDropped = SL[4];
          I.SndBytes = SL[7];
          I.SndErrors = SL[9];
          I.SndDropped = SL[10];
          I.Collisions = SL[12];
          break;
        case 3 :
          I.RcvBytes = SL[1];
          I.RcvErrors = SL[3];
          I.RcvDropped = SL[4];
          I.SndBytes = SL[9];
          I.SndErrors = SL[11];
          I.SndDropped = SL[12];
          I.Collisions = SL[14];
          break;
      }
      break;
    }
}

//
// THIS UPDATES THE LIST -> INTERFACES ARE NOT DELETED BUT
// FLAGGED AS ! 'IsUp' IF NO LONGER PRESENT
//

void System::probeInterfaces( void ) {

    // probe interfaces
    int sockfd;
    // get list of all interfaces
    struct ifreq ifrs;
    InterfaceInfo * IFI;

    // flag all as 'down'
    for( QDictIterator<InterfaceInfo> it( ProbedInterfaces ); 
         it.current();
         ++it ) {
      it.current()->IsUp = 0;
    }

    sockfd = socket(PF_INET, SOCK_DGRAM, 0);
    if(sockfd == -1)
      return;

    // read interfaces from /proc/dev/net
    // SIOCGIFCONF does not return ALL interfaces ???!?
    ProcDevNet = new QFile(PROCNETDEV);
    if( ! ProcDevNet->open(IO_ReadOnly) ) {
      delete ProcDevNet;
      ProcDevNet =0;
      return;
    }

    QString line;
    QString NicName;
    QTextStream procTs(ProcDevNet);
    int loc = -1;

    procTs.readLine(); // eat a line
    procTs.readLine(); // eat a line
    while((line = procTs.readLine().simplifyWhiteSpace()) != QString::null) {
      if((loc = line.find(":")) == -1) {
        continue;
      }

      NicName = line.left(loc);

      // set name for ioctl
      strcpy( ifrs.ifr_name, NicName.latin1() );

      if ( ! ( IFI = ProbedInterfaces.find( NicName ) ) ) {
        // new nic
        fprintf( stderr, "NEWNIC %s\n", NicName.latin1());
        IFI = new InterfaceInfo;
        IFI->Name = line.left(loc);
        IFI->NetNode = 0;
        ProbedInterfaces.insert( IFI->Name, IFI );

        // get dynamic info
        if( ioctl(sockfd, SIOCGIFFLAGS, &ifrs) >= 0 ) {
          IFI->IsPointToPoint = ((ifrs.ifr_flags & IFF_POINTOPOINT) == IFF_POINTOPOINT);
        } else {
          IFI->IsPointToPoint = 0;
        }

        // settings that never change
        IFI->DstAddress = "";

        if( IFI->IsPointToPoint ) {
          if( ioctl(sockfd, SIOCGIFDSTADDR, &ifrs) >= 0 ) {
            IFI->DstAddress = 
             inet_ntoa(((struct sockaddr_in*)&ifrs.ifr_dstaddr)->sin_addr);
          }
        }

        IFI->CardType = 999999;
        IFI->MACAddress = "";

        if( ioctl(sockfd, SIOCGIFHWADDR, &ifrs) >= 0 ) {
          fprintf( stderr, "%s = %d\n", IFI->Name.latin1(), 
              ifrs.ifr_hwaddr.sa_family );

          IFI->CardType = ifrs.ifr_hwaddr.sa_family;
          switch( ifrs.ifr_hwaddr.sa_family ) {
            case ARPHRD_ETHER : // regular MAC address
              // valid address -> convert to regular ::: format
              // length = 6 bytes = 12 DIGITS -> 6 :
              IFI->MACAddress.sprintf(
                "%c%c:%c%c:%c%c:%c%c:%c%c:%c%c",
                HN( ifrs.ifr_hwaddr.sa_data[0] ),
                LN( ifrs.ifr_hwaddr.sa_data[0] ),
                HN( ifrs.ifr_hwaddr.sa_data[1] ),
                LN( ifrs.ifr_hwaddr.sa_data[1] ),
                HN( ifrs.ifr_hwaddr.sa_data[2] ),
                LN( ifrs.ifr_hwaddr.sa_data[2] ),
                HN( ifrs.ifr_hwaddr.sa_data[3] ),
                LN( ifrs.ifr_hwaddr.sa_data[3] ),
                HN( ifrs.ifr_hwaddr.sa_data[4] ),
                LN( ifrs.ifr_hwaddr.sa_data[4] ),
                HN( ifrs.ifr_hwaddr.sa_data[5] ),
                LN( ifrs.ifr_hwaddr.sa_data[5] )
              );
              break;
#ifdef ARPHRD_IEEE1394
            case ARPHRD_IEEE1394 : // Firewire Eth address
              IFI->MACAddress.sprintf(
                "%c%c-%c%c-%c%c-%c%c-%c%c-%c%c-%c%c-%c%c-%c%c-%c%c-%c%c-%c%c-%c%c-%c%c-00-00",
                HN( ifrs.ifr_hwaddr.sa_data[0] ),
                LN( ifrs.ifr_hwaddr.sa_data[0] ),
                HN( ifrs.ifr_hwaddr.sa_data[1] ),
                LN( ifrs.ifr_hwaddr.sa_data[1] ),
                HN( ifrs.ifr_hwaddr.sa_data[2] ),
                LN( ifrs.ifr_hwaddr.sa_data[2] ),
                HN( ifrs.ifr_hwaddr.sa_data[3] ),
                LN( ifrs.ifr_hwaddr.sa_data[3] ),
                HN( ifrs.ifr_hwaddr.sa_data[4] ),
                LN( ifrs.ifr_hwaddr.sa_data[4] ),
                HN( ifrs.ifr_hwaddr.sa_data[5] ),
                LN( ifrs.ifr_hwaddr.sa_data[5] ),
                HN( ifrs.ifr_hwaddr.sa_data[6] ),
                LN( ifrs.ifr_hwaddr.sa_data[6] ),
                HN( ifrs.ifr_hwaddr.sa_data[7] ),
                LN( ifrs.ifr_hwaddr.sa_data[7] ),
                HN( ifrs.ifr_hwaddr.sa_data[8] ),
                LN( ifrs.ifr_hwaddr.sa_data[8] ),
                HN( ifrs.ifr_hwaddr.sa_data[9] ),
                LN( ifrs.ifr_hwaddr.sa_data[9] ),
                HN( ifrs.ifr_hwaddr.sa_data[10] ),
                LN( ifrs.ifr_hwaddr.sa_data[10] ),
                HN( ifrs.ifr_hwaddr.sa_data[11] ),
                LN( ifrs.ifr_hwaddr.sa_data[11] ),
                HN( ifrs.ifr_hwaddr.sa_data[12] ),
                LN( ifrs.ifr_hwaddr.sa_data[12] ),
                HN( ifrs.ifr_hwaddr.sa_data[13] ),
                LN( ifrs.ifr_hwaddr.sa_data[13] )
              );
              break;
#endif
            case ARPHRD_PPP : // PPP
              break;
            case ARPHRD_IEEE80211 : // WLAN
              break;
            case ARPHRD_IRDA : // IRDA
              break;
          }
        }
      } else // else already probed before -> just update
        fprintf( stderr, "OLDNIC %s\n", NicName.latin1());

      // get dynamic info
      if( ioctl(sockfd, SIOCGIFFLAGS, &ifrs) >= 0 ) {
        IFI->IsUp = ((ifrs.ifr_flags & IFF_UP) == IFF_UP);
        IFI->HasMulticast = ((ifrs.ifr_flags & IFF_MULTICAST) == IFF_MULTICAST);
      } else {
        IFI->IsUp = 0;
        IFI->HasMulticast = 0;
      }

      if( ioctl(sockfd, SIOCGIFADDR, &ifrs) >= 0 ) {
        IFI->Address = 
           inet_ntoa(((struct sockaddr_in*)&ifrs.ifr_addr)->sin_addr);
      } else {
        IFI->Address = "";
        IFI->IsUp = 0;
      }
      if( ioctl(sockfd, SIOCGIFBRDADDR, &ifrs) >= 0 ) {
        IFI->BCastAddress = 
           inet_ntoa(((struct sockaddr_in*)&ifrs.ifr_broadaddr)->sin_addr);
      } else {
        IFI->BCastAddress = "";
      }
      if( ioctl(sockfd, SIOCGIFNETMASK, &ifrs) >= 0 ) {
        IFI->Netmask = 
           inet_ntoa(((struct sockaddr_in*)&ifrs.ifr_netmask)->sin_addr);
      } else {
        IFI->Netmask = "";
      }
      fprintf( stderr, "NIC %s UP %d\n", NicName.latin1(), IFI->IsUp );
    }
}

void System::execAsUser( QString & Cmd, char * argv[] ) {
      CurrentQPEUser CU = NSResources->currentUser();

      if( CU.UserName.isEmpty() ) {
        // if we come here, the exec was not successfull
        fprintf( stderr, "User not known \n" );
        return;
      }

      // now we are ready to exec the requested command
      setuid( CU.Uid );
      setgid( CU.Gid );

      char ** envp = (char **)alloca( sizeof( char *) *
            (CU.EnvList.count()+1) );

      for( unsigned int i = 0 ; i < CU.EnvList.count() ; i ++ ) {
        *(envp+i) = CU.EnvList[i];
      }
      envp[CU.EnvList.count()]=NULL;

      execve( Cmd.latin1(), argv, envp );

      // if we come here, the exec was not successfull
      fprintf( stderr, "Could not exec : %d\n", errno );
}