summaryrefslogtreecommitdiff
authorwimpie <wimpie>2004-04-05 01:13:11 (UTC)
committer wimpie <wimpie>2004-04-05 01:13:11 (UTC)
commit6be3d148fc1d610ebfa193012657b3b77d9368e3 (patch) (unidiff)
treed7bbd308d17ffe7557c7749776f4945e499d7c10
parentb0ceb8843ebe9ee3054faa98f08bd255df955aa3 (diff)
downloadopie-6be3d148fc1d610ebfa193012657b3b77d9368e3.zip
opie-6be3d148fc1d610ebfa193012657b3b77d9368e3.tar.gz
opie-6be3d148fc1d610ebfa193012657b3b77d9368e3.tar.bz2
For usb this seems to be working
Also for non-root users (requires using SUDO)
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/settings/networksettings2/activateprofile.cpp2
-rw-r--r--noncore/settings/networksettings2/activateprofileGUI.ui4
-rw-r--r--noncore/settings/networksettings2/main.cpp15
-rw-r--r--noncore/settings/networksettings2/network/network_NNI.cpp47
-rw-r--r--noncore/settings/networksettings2/network/networkrun.cpp4
-rw-r--r--noncore/settings/networksettings2/networksettings2/resources.cpp137
-rw-r--r--noncore/settings/networksettings2/networksettings2/resources.h18
-rw-r--r--noncore/settings/networksettings2/networksettings2/system.cpp43
-rw-r--r--noncore/settings/networksettings2/networksettings2/system.h7
-rw-r--r--noncore/settings/networksettings2/nsdata.cpp26
-rw-r--r--noncore/settings/networksettings2/nsdata.h3
11 files changed, 270 insertions, 36 deletions
diff --git a/noncore/settings/networksettings2/activateprofile.cpp b/noncore/settings/networksettings2/activateprofile.cpp
index ba726bb..e4064e3 100644
--- a/noncore/settings/networksettings2/activateprofile.cpp
+++ b/noncore/settings/networksettings2/activateprofile.cpp
@@ -1,2 +1,3 @@
1#include <qlistbox.h> 1#include <qlistbox.h>
2#include <qlabel.h>
2#include "activateprofile.h" 3#include "activateprofile.h"
@@ -8,2 +9,3 @@ ActivateProfile::ActivateProfile( const char * Interface ) :
8 9
10 DeviceName_LBL->setText( Interface );
9 Profiles_LB->clear(); 11 Profiles_LB->clear();
diff --git a/noncore/settings/networksettings2/activateprofileGUI.ui b/noncore/settings/networksettings2/activateprofileGUI.ui
index 296e044..12ab051 100644
--- a/noncore/settings/networksettings2/activateprofileGUI.ui
+++ b/noncore/settings/networksettings2/activateprofileGUI.ui
@@ -19,3 +19,3 @@
19 <name>caption</name> 19 <name>caption</name>
20 <string>Activate Profile</string> 20 <string>Activate Network</string>
21 </property> 21 </property>
@@ -59,3 +59,3 @@
59 <name>text</name> 59 <name>text</name>
60 <string>Select profile for device :</string> 60 <string>Select profile to activate for </string>
61 </property> 61 </property>
diff --git a/noncore/settings/networksettings2/main.cpp b/noncore/settings/networksettings2/main.cpp
index 7ec26a7..e2c00f6 100644
--- a/noncore/settings/networksettings2/main.cpp
+++ b/noncore/settings/networksettings2/main.cpp
@@ -3,2 +3,3 @@
3#include "networksettings.h" 3#include "networksettings.h"
4
4#include <qpe/qpeapplication.h> 5#include <qpe/qpeapplication.h>
@@ -78,3 +79,15 @@ int main( int argc, char * argv[] ) {
78 { NetworkSettingsData NS; 79 { NetworkSettingsData NS;
79 NS.canStart( argv[1] ); 80 if( NS.canStart( argv[1] ) ) {
81 QString S;
82 S.sprintf( QPEApplication::qpeDir()+
83 "/bin/networksettings2" );
84 char * MyArgv[4];
85 MyArgv[0] = "networksettings2";
86 MyArgv[1] = "--prompt";
87 MyArgv[2] = argv[1];
88 MyArgv[3] = NULL;
89 NSResources->system().execAsUser( S, MyArgv );
90 // if we come here , failed
91 printf( "%s-cNN-disallowed", argv[1] );
92 }
80 } 93 }
diff --git a/noncore/settings/networksettings2/network/network_NNI.cpp b/noncore/settings/networksettings2/network/network_NNI.cpp
index 054385a..3e368a2 100644
--- a/noncore/settings/networksettings2/network/network_NNI.cpp
+++ b/noncore/settings/networksettings2/network/network_NNI.cpp
@@ -111,19 +111,40 @@ bool ANetwork::generateDataForCommonFile( SystemFile & S, long DevNr ) {
111 if( Data.UseDHCP ) { 111 if( Data.UseDHCP ) {
112 S << "iface " << NIC << "-c" << connection()->number() << 112 S << "iface "
113 "-allowed inet dhcp" << endl; 113 << NIC
114 S << " up echo \"" << NIC << "\" > /tmp/profile-" << connection()->number() << 114 << "-c"
115 ".up" << Data.IPAddress << endl; 115 << connection()->number()
116 << "-allowed inet dhcp"
117 << endl;
118 S << " up echo \""
119 << NIC
120 << "\" > /tmp/profile-"
121 << connection()->number()
122 << ".up"
123 << endl;
116 if( Data.SendHostname ) { 124 if( Data.SendHostname ) {
117 S << " hostname "<< Data.Hostname << endl; 125 S << " hostname "
126 << Data.Hostname
127 << endl;
118 } 128 }
119 129
120 S << " down rm -f /tmp/profile-" << connection()->number() << 130 S << " down rm -f /tmp/profile-"
121 ".up" << Data.IPAddress << endl; 131 << connection()->number()
132 << ".up"
133 << endl;
122 } else { 134 } else {
123 S << "iface " << NIC << "-c" << connection()->number() << 135 S << "iface "
124 "-allowed inet static" << endl; 136 << NIC << "-c"
125 S << " up echo \"" << NIC << "\" > /tmp/profile-" << connection()->number() << 137 << connection()->number()
126 ".up" << Data.IPAddress << endl; 138 << "-allowed inet static"
127 S << " down rm -f /tmp/profile-" << connection()->number() << 139 << endl;
128 ".up" << Data.IPAddress << endl; 140 S << " up echo \""
141 << NIC
142 << "\" > /tmp/profile-"
143 << connection()->number()
144 << ".up"
145 << endl;
146 S << " down rm -f /tmp/profile-"
147 << connection()->number()
148 << ".up"
149 << endl;
129 S << " address " << Data.IPAddress << endl; 150 S << " address " << Data.IPAddress << endl;
diff --git a/noncore/settings/networksettings2/network/networkrun.cpp b/noncore/settings/networksettings2/network/networkrun.cpp
index ddb9a5f..c19235a 100644
--- a/noncore/settings/networksettings2/network/networkrun.cpp
+++ b/noncore/settings/networksettings2/network/networkrun.cpp
@@ -32,3 +32,3 @@ bool NetworkRun::setState( NodeCollection * NC, Action_t A ) {
32 connection()->number() ); 32 connection()->number() );
33 NSResources->system().execute( S ); 33 NSResources->system().runAsRoot( S );
34 } 34 }
@@ -41,3 +41,3 @@ bool NetworkRun::setState( NodeCollection * NC, Action_t A ) {
41 connection()->number() ); 41 connection()->number() );
42 NSResources->system().execute( S ); 42 NSResources->system().runAsRoot( S );
43 } 43 }
diff --git a/noncore/settings/networksettings2/networksettings2/resources.cpp b/noncore/settings/networksettings2/networksettings2/resources.cpp
index 0301361..e6ce2b7 100644
--- a/noncore/settings/networksettings2/networksettings2/resources.cpp
+++ b/noncore/settings/networksettings2/networksettings2/resources.cpp
@@ -1,5 +1,9 @@
1#include <unistd.h>
2#include <errno.h>
3#include <fcntl.h>
4#include <pwd.h>
1#include <qpixmap.h> 5#include <qpixmap.h>
6#include <qdir.h>
2#include <qpe/qlibrary.h> 7#include <qpe/qlibrary.h>
3#include <qpe/qpeapplication.h> 8#include <qpe/qpeapplication.h>
4#include <qdir.h>
5#include <opie2/odebug.h> 9#include <opie2/odebug.h>
@@ -77,2 +81,4 @@ TheNSResources::TheNSResources( void ) : NodeTypeNameMap(),
77 TheSystem = new System(); 81 TheSystem = new System();
82
83 detectCurrentUser();
78} 84}
@@ -234 +240,130 @@ void TheNSResources::renumberConnections( void ) {
234} 240}
241
242typedef struct EnvVars {
243 char * Name;
244 int Len;
245} EnvVar_t;
246
247#define AnEV(x) x, sizeof(x)-1
248
249static EnvVar_t EV[] = {
250 // AnEV( "HOME=" ), -> SPECIAL
251 // AnEV( "LOGNAME=" ), -> SPECIAL
252 AnEV( "USER=" ),
253 AnEV( "LD_LIBRARY_PATH=" ),
254 AnEV( "PATH=" ),
255 AnEV( "QTDIR=" ),
256 AnEV( "OPIEDIR=" ),
257 AnEV( "SHELL=" ),
258 { NULL, 0 }
259};
260
261void TheNSResources::detectCurrentUser( void ) {
262 // find current running qpe
263 QString QPEEnvFile = "";
264
265 // open proc dir and find all dirs in it
266 { QRegExp R("[0-9]+");
267 QDir ProcDir( "/proc" );
268 QString QPELoc = QPEApplication::qpeDir() + "bin/qpe";
269 QFileInfo FI;
270 QStringList EL = ProcDir.entryList( QDir::Dirs );
271
272 // print it out
273 for ( QStringList::Iterator it = EL.begin();
274 it != EL.end();
275 ++it ) {
276 if( R.match( (*it) ) >= 0 ) {
277 QString S = ProcDir.path()+"/"+ (*it);
278 S.append( "/exe" );
279 FI.setFile( S );
280 // get the linke
281 S = FI.readLink();
282 if( S == QPELoc ) {
283 // found running qpe
284 QPEEnvFile.sprintf( ProcDir.path()+ "/" + (*it) + "/environ" );
285 break;
286 }
287 }
288 }
289 }
290
291 if( QPEEnvFile.isEmpty() ) {
292 // could not find qpe
293 fprintf( stderr, "Could not find qpe\n" );
294 return;
295 }
296
297 // FI now contains path ProcDir to the cmd dir
298 { char * Buf = 0;
299 char TB[1024];
300 long BufSize = 0;
301 int fd;
302 int rd;
303
304 fd = open( QPEEnvFile.latin1(), O_RDONLY );
305 if( fd < 0 ) {
306 fprintf( stderr, "Could not open %s : %d\n",
307 QPEEnvFile.latin1(), errno );
308 return;
309 }
310
311 while( (rd = read( fd, TB, sizeof(TB) ) ) > 0 ) {
312 Buf = (char *)realloc( Buf, BufSize+rd );
313 memcpy( Buf+BufSize, TB, rd );
314 BufSize += rd;
315 }
316
317 char * Data = Buf;
318 char * DataEnd = Data+BufSize-1;
319
320 // get env items out of list
321 while( Data < DataEnd ) {
322 if( strncmp( Data, "LOGNAME=", 8 ) == 0 ) {
323 CurrentUser.UserName = Data+8;
324 CurrentUser.EnvList.resize( CurrentUser.EnvList.size()+1 );
325 CurrentUser.EnvList[CurrentUser.EnvList.size()-1] =
326 strdup( Data );
327 } else if( strncmp( Data, "HOME=", 5 ) == 0 ) {
328 CurrentUser.HomeDir = Data+5;
329 CurrentUser.EnvList.resize( CurrentUser.EnvList.size()+1 );
330 CurrentUser.EnvList[CurrentUser.EnvList.size()-1] =
331 strdup( Data );
332 } else {
333 EnvVar_t * Run = EV;
334 while( Run->Name ) {
335 if( strncmp( Data, Run->Name, Run->Len ) == 0 ) {
336 CurrentUser.EnvList.resize( CurrentUser.EnvList.size()+1 );
337 CurrentUser.EnvList[CurrentUser.EnvList.size()-1] =
338 strdup( Data );
339 break;
340 }
341 Run ++;
342 }
343 }
344
345 Data += strlen( Data )+1;
346 }
347
348 free( Buf );
349
350 if( ! CurrentUser.UserName.isEmpty() ) {
351 // find user info
352 struct passwd pwd;
353 struct passwd * pwdres;
354
355 if( getpwnam_r( CurrentUser.UserName.latin1(),
356 &pwd, TB, sizeof(TB), &pwdres ) ||
357 pwdres == 0 ) {
358 fprintf( stderr, "Could not determine user %s : %d\n",
359 CurrentUser.UserName.latin1(), errno );
360 return;
361 }
362 CurrentUser.Uid = pwd.pw_uid;
363 CurrentUser.Gid = pwd.pw_gid;
364 } else{
365 CurrentUser.Uid =
366 CurrentUser.Gid = -1;
367 }
368 }
369}
diff --git a/noncore/settings/networksettings2/networksettings2/resources.h b/noncore/settings/networksettings2/networksettings2/resources.h
index 4df3ce3..3048fb3 100644
--- a/noncore/settings/networksettings2/networksettings2/resources.h
+++ b/noncore/settings/networksettings2/networksettings2/resources.h
@@ -24,2 +24,14 @@ typedef struct NetNode_S {
24 24
25class CurrentQPEUser {
26
27public :
28 CurrentQPEUser() : UserName(), HomeDir(), EnvList() {}
29
30 QString UserName;
31 QString HomeDir;
32 int Uid;
33 int Gid;
34 QArray<char *> EnvList;
35};
36
25typedef QDict<NetNode_t> Name2NetNode_t; 37typedef QDict<NetNode_t> Name2NetNode_t;
@@ -85,4 +97,8 @@ public :
85 97
98 CurrentQPEUser & currentUser( void )
99 { return CurrentUser; }
100
86private : 101private :
87 102
103 void detectCurrentUser( void );
88 QString tr( const char * path ); 104 QString tr( const char * path );
@@ -104,2 +120,4 @@ private :
104 Name2Instance_t AllNodes; 120 Name2Instance_t AllNodes;
121
122 CurrentQPEUser CurrentUser;
105}; 123};
diff --git a/noncore/settings/networksettings2/networksettings2/system.cpp b/noncore/settings/networksettings2/networksettings2/system.cpp
index a68f3c0..2133d34 100644
--- a/noncore/settings/networksettings2/networksettings2/system.cpp
+++ b/noncore/settings/networksettings2/networksettings2/system.cpp
@@ -1,6 +1,4 @@
1#include <stdlib.h>
2#include <sys/types.h> 1#include <sys/types.h>
3#include <sys/wait.h> 2#include <sys/wait.h>
4#include <qfile.h> 3
5#include <qtextstream.h>
6#include <net/if.h> 4#include <net/if.h>
@@ -11,2 +9,12 @@
11#include <sys/socket.h> 9#include <sys/socket.h>
10#include <stdlib.h>
11#include <fcntl.h>
12#include <errno.h>
13#include <unistd.h>
14
15#include <qdir.h>
16#include <qregexp.h>
17#include <qstringlist.h>
18#include <qfile.h>
19#include <qtextstream.h>
12 20
@@ -38,3 +46,3 @@ System::~System( void ) {
38 46
39int System::execute( const QString & S ) { 47int System::runAsRoot( const QString & S ) {
40 QString MyS = S; 48 QString MyS = S;
@@ -320 +328,28 @@ void System::probeInterfaces( void ) {
320} 328}
329
330void System::execAsUser( QString & Cmd, char * argv[] ) {
331 CurrentQPEUser CU = NSResources->currentUser();
332
333 if( CU.UserName.isEmpty() ) {
334 // if we come here, the exec was not successfull
335 fprintf( stderr, "User not known \n" );
336 return;
337 }
338
339 // now we are ready to exec the requested command
340 setuid( CU.Uid );
341 setgid( CU.Gid );
342
343 char ** envp = (char **)alloca( sizeof( char *) *
344 (CU.EnvList.count()+1) );
345
346 for( unsigned int i = 0 ; i < CU.EnvList.count() ; i ++ ) {
347 *(envp+i) = CU.EnvList[i];
348 }
349 envp[CU.EnvList.count()]=NULL;
350
351 execve( Cmd.latin1(), argv, envp );
352
353 // if we come here, the exec was not successfull
354 fprintf( stderr, "Could not exec : %d\n", errno );
355}
diff --git a/noncore/settings/networksettings2/networksettings2/system.h b/noncore/settings/networksettings2/networksettings2/system.h
index f89fe5d..96ee9bd 100644
--- a/noncore/settings/networksettings2/networksettings2/system.h
+++ b/noncore/settings/networksettings2/networksettings2/system.h
@@ -61,3 +61,8 @@ public :
61 61
62 int execute( const QString & S ); 62 // exec command as root
63 int runAsRoot( const QString & S );
64
65 // exec command as user
66 void execAsUser( QString & Cmd, char * argv[] );
67
63 // refresh stats for this interface 68 // refresh stats for this interface
diff --git a/noncore/settings/networksettings2/nsdata.cpp b/noncore/settings/networksettings2/nsdata.cpp
index eb63e02..b4d9aaa 100644
--- a/noncore/settings/networksettings2/nsdata.cpp
+++ b/noncore/settings/networksettings2/nsdata.cpp
@@ -17,3 +17,5 @@ NetworkSettingsData::NetworkSettingsData( void ) {
17 17
18 CfgFile.sprintf( "%s/NETCONFIG", getenv("HOME") ); 18 CfgFile.sprintf( "%s/NETCONFIG",
19 NSResources->currentUser().HomeDir.latin1() );
20 fprintf( stderr, "Cfg from %s\n", CfgFile.latin1() );
19 21
@@ -355,2 +357,3 @@ QList<NodeCollection> NetworkSettingsData::collectPossible( const char * Interfa
355 // check if this profile handles the requested interface 357 // check if this profile handles the requested interface
358 fprintf( stderr, "check %s\n", NC->name().latin1() );
356 if( NC->handlesInterface( Interface ) && // if different Intf. 359 if( NC->handlesInterface( Interface ) && // if different Intf.
@@ -359,2 +362,3 @@ QList<NodeCollection> NetworkSettingsData::collectPossible( const char * Interfa
359 ) { 362 ) {
363 fprintf( stderr, "Append %s\n", NC->name().latin1() );
360 PossibleConnections.append( NC ); 364 PossibleConnections.append( NC );
@@ -372,3 +376,3 @@ QList<NodeCollection> NetworkSettingsData::collectPossible( const char * Interfa
372 376
373void NetworkSettingsData::canStart( const char * Interface ) { 377bool NetworkSettingsData::canStart( const char * Interface ) {
374 // load situation 378 // load situation
@@ -379,2 +383,4 @@ void NetworkSettingsData::canStart( const char * Interface ) {
379 383
384 fprintf( stderr, "Possiblilies %d\n",
385 PossibleConnections.count() );
380 switch( PossibleConnections.count() ) { 386 switch( PossibleConnections.count() ) {
@@ -386,6 +392,3 @@ void NetworkSettingsData::canStart( const char * Interface ) {
386 default : // need to ask user ? 392 default : // need to ask user ?
387 // are we connected to a server 393 return 1;
388 // system( "su %d networksettings2 --prompt %s\n",
389 // "", Interface );
390 break;
391 } 394 }
@@ -412,8 +415,9 @@ void NetworkSettingsData::canStart( const char * Interface ) {
412 printf( "%s-c%d-allowed\n", Interface, NC->number() ); 415 printf( "%s-c%d-allowed\n", Interface, NC->number() );
413 return; 416 return 0;
414 } 417 }
415 } else { 418 }
416 // if we come here no alternatives are possible 419
417 printf( "%s-cnn-disallowed\n", Interface ); 420 // if we come here no alternatives are possible
418 } 421 printf( "%s-cnn-disallowed\n", Interface );
422 return 0;
419} 423}
diff --git a/noncore/settings/networksettings2/nsdata.h b/noncore/settings/networksettings2/nsdata.h
index b54df24..eb96930 100644
--- a/noncore/settings/networksettings2/nsdata.h
+++ b/noncore/settings/networksettings2/nsdata.h
@@ -23,3 +23,4 @@ public :
23 QList<NodeCollection> collectPossible( const char * Interface ); 23 QList<NodeCollection> collectPossible( const char * Interface );
24 void canStart( const char * Interface ); 24 // return TRUE if we need gui to decide
25 bool canStart( const char * Interface );
25 bool regenerate( void ); 26 bool regenerate( void );