author | llornkcor <llornkcor> | 2005-08-15 09:58:06 (UTC) |
---|---|---|
committer | llornkcor <llornkcor> | 2005-08-15 09:58:06 (UTC) |
commit | e7c0016a970ab1bdf5870a648a7bf07b857fbb11 (patch) (unidiff) | |
tree | 3180a13bf982fbb3d908a6a0c5d824bb63a22936 | |
parent | 75eb12d42636cbd837d49f9c1347387e96e4174b (diff) | |
download | opie-e7c0016a970ab1bdf5870a648a7bf07b857fbb11.zip opie-e7c0016a970ab1bdf5870a648a7bf07b857fbb11.tar.gz opie-e7c0016a970ab1bdf5870a648a7bf07b857fbb11.tar.bz2 |
scan better
-rw-r--r-- | noncore/net/opie-smb/qsmb.cpp | 80 |
1 files changed, 25 insertions, 55 deletions
diff --git a/noncore/net/opie-smb/qsmb.cpp b/noncore/net/opie-smb/qsmb.cpp index d6d1b23..c1ee9a9 100644 --- a/noncore/net/opie-smb/qsmb.cpp +++ b/noncore/net/opie-smb/qsmb.cpp | |||
@@ -1,270 +1,240 @@ | |||
1 | #include "qsmb.h" | 1 | #include "qsmb.h" |
2 | #include <qpushbutton.h> | 2 | #include <qpushbutton.h> |
3 | #include <qpe/qpeapplication.h> | 3 | #include <qpe/qpeapplication.h> |
4 | 4 | ||
5 | #include <string.h> | 5 | #include <string.h> |
6 | #include <qstring.h> | 6 | #include <qstring.h> |
7 | #include <qstringlist.h> | 7 | #include <qstringlist.h> |
8 | #include <qdir.h> | 8 | #include <qdir.h> |
9 | #include <qfileinfo.h> | 9 | #include <qfileinfo.h> |
10 | 10 | ||
11 | #include <qpe/process.h> | 11 | #include <qpe/process.h> |
12 | #include <qlabel.h> | 12 | #include <qlabel.h> |
13 | #include <qlineedit.h> | 13 | #include <qlineedit.h> |
14 | #include <qcombobox.h> | 14 | #include <qcombobox.h> |
15 | #include <qstringlist.h> | 15 | #include <qstringlist.h> |
16 | #include <qcheckbox.h> | 16 | #include <qcheckbox.h> |
17 | #include <qtextview.h> | 17 | #include <qtextview.h> |
18 | #include <qmessagebox.h> | 18 | #include <qmessagebox.h> |
19 | #include <qtextstream.h> | ||
19 | 20 | ||
20 | 21 | ||
21 | #include <pthread.h> | 22 | #include <pthread.h> |
22 | #include <signal.h> | 23 | #include <signal.h> |
23 | #include <ctype.h> | 24 | #include <ctype.h> |
24 | 25 | ||
25 | 26 | ||
26 | #include <netinet/in.h> | 27 | #include <netinet/in.h> |
27 | #include <arpa/inet.h> | 28 | #include <arpa/inet.h> |
28 | 29 | ||
29 | #include <rpc/clnt.h> | 30 | #include <rpc/clnt.h> |
30 | 31 | ||
31 | 32 | ||
32 | #include <opie2/odebug.h> | 33 | #include <opie2/odebug.h> |
33 | using namespace Opie::Core; | 34 | using namespace Opie::Core; |
34 | 35 | ||
35 | 36 | ||
36 | Qsmb::Qsmb( QWidget* parent, const char* name, WFlags fl ) | 37 | Qsmb::Qsmb( QWidget* parent, const char* name, WFlags fl ) |
37 | : FormQPESMBBase( parent, name, fl ) | 38 | : FormQPESMBBase( parent, name, fl ) |
38 | { | 39 | { |
39 | connect(CBHost, SIGNAL(activated(int)), this, SLOT(hostSelected(int))); | 40 | connect(CBHost, SIGNAL(activated(int)), this, SLOT(hostSelected(int))); |
40 | connect(DoItBtn, SIGNAL(clicked()), this, SLOT(DoItClicked())); | 41 | connect(DoItBtn, SIGNAL(clicked()), this, SLOT(DoItClicked())); |
41 | connect(BtnScan, SIGNAL(clicked()), this, SLOT(scanClicked())); | 42 | connect(BtnScan, SIGNAL(clicked()), this, SLOT(scanClicked())); |
42 | connect(BtnClear, SIGNAL(clicked()), this, SLOT(clear())); | 43 | connect(BtnClear, SIGNAL(clicked()), this, SLOT(clear())); |
43 | 44 | ||
44 | mountpt->insertItem("/mnt/samba1",-1); | 45 | mountpt->insertItem("/mnt/samba1",-1); |
45 | mountpt->insertItem("/mnt/samba2",-1); | 46 | mountpt->insertItem("/mnt/samba2",-1); |
46 | mountpt->insertItem("/mnt/samba3",-1); | 47 | mountpt->insertItem("/mnt/samba3",-1); |
47 | 48 | ||
48 | top_element = NULL; | 49 | top_element = NULL; |
49 | scanning = false; | 50 | scanning = false; |
50 | } | 51 | } |
51 | 52 | ||
52 | Qsmb::~Qsmb() | 53 | Qsmb::~Qsmb() |
53 | { | 54 | { |
54 | } | 55 | } |
55 | 56 | ||
56 | void Qsmb::clear() | 57 | void Qsmb::clear() |
57 | { | 58 | { |
58 | if (scanning) return; | 59 | if (scanning) return; |
59 | ListViewScan->clear(); | 60 | ListViewScan->clear(); |
60 | TextViewOutput->setText(""); | 61 | TextViewOutput->setText(""); |
61 | top_element = NULL; | 62 | top_element = NULL; |
62 | } | 63 | } |
63 | 64 | ||
64 | void Qsmb::scanClicked() | 65 | void Qsmb::scanClicked() |
65 | { | 66 | { |
66 | if (scanning) return; | 67 | if (scanning) return; |
67 | pthread_create(&tpid, NULL, runit, (void *)this); | 68 | pthread_create(&tpid, NULL, runit, (void *)this); |
68 | } | 69 | } |
69 | 70 | ||
70 | void Qsmb::DoItClicked() | 71 | void Qsmb::DoItClicked() |
71 | { | 72 | { |
72 | 73 | ||
73 | if(! ListViewScan->selectedItem()) { | 74 | if(! ListViewScan->selectedItem()) { |
74 | QMessageBox::warning(this, tr("Error"),tr("<p>No share selected!</p>")); | 75 | QMessageBox::warning(this, tr("Error"),tr("<p>No share selected!</p>")); |
75 | return; | 76 | return; |
76 | } | 77 | } |
77 | if (scanning) return; | 78 | if (scanning) return; |
78 | pthread_create(&tpid, NULL, runitm, (void *)this); | 79 | pthread_create(&tpid, NULL, runitm, (void *)this); |
79 | } | 80 | } |
80 | 81 | ||
81 | void* runit(void* arg) | 82 | void* runit(void* arg) |
82 | { | 83 | { |
83 | Qsmb* caller = (Qsmb*)arg; | 84 | Qsmb* caller = (Qsmb*)arg; |
84 | caller->scan(); | 85 | caller->scan(); |
85 | return(0); | 86 | return(0); |
86 | } | 87 | } |
87 | 88 | ||
88 | void* runitm(void* arg) | 89 | void* runitm(void* arg) |
89 | { | 90 | { |
90 | Qsmb* caller = (Qsmb*)arg; | 91 | Qsmb* caller = (Qsmb*)arg; |
91 | caller->DoIt(); | 92 | caller->DoIt(); |
92 | return(0); | 93 | return(0); |
93 | } | 94 | } |
94 | 95 | ||
95 | void Qsmb::scan() | 96 | void Qsmb::scan() |
96 | { | 97 | { |
97 | int i; | 98 | int i; |
98 | 99 | ||
99 | if (scanning) return; | 100 | if (scanning) return; |
100 | scanning = true; | 101 | scanning = true; |
101 | 102 | ||
102 | QString match; | 103 | QString match; |
103 | char lmhosts[512]; | ||
104 | QString cmd; | 104 | QString cmd; |
105 | char result[256]; | ||
106 | |||
107 | FILE *pipe, *pipe2; | ||
108 | |||
109 | LScan->setText("Scanning..."); | 105 | LScan->setText("Scanning..."); |
110 | qApp->processEvents(); | 106 | qApp->processEvents(); |
111 | 107 | ||
112 | sockaddr_in my_addr; | 108 | sockaddr_in my_addr; |
113 | get_myaddress( &my_addr); | 109 | get_myaddress( &my_addr); |
114 | 110 | ||
115 | QString ip = inet_ntoa( my_addr.sin_addr); | 111 | QString ip = inet_ntoa( my_addr.sin_addr); |
116 | owarn << "IP Address : " << ip<< oendl; | 112 | owarn << "IP Address : " << ip<< oendl; |
117 | 113 | ||
118 | match = ip.left(5); | 114 | match = ip.left(5); |
119 | 115 | ||
120 | cmd = "smbfind"; | 116 | QStringList ccmd; |
121 | owarn <<"cmd: " << cmd << oendl; | 117 | TextViewOutput->append("smbfind"); |
122 | |||
123 | TextViewOutput->append(cmd); | ||
124 | |||
125 | snprintf(lmhosts, 512, "echo '127.0.0.1 localhost'>/etc/samba/lmhosts"); | ||
126 | |||
127 | if ((pipe2 = popen(lmhosts, "r")) == NULL) { | ||
128 | snprintf(result, 256, "Error: Can't run %s", lmhosts); | ||
129 | //TextViewOutput->append(result); | ||
130 | return; | ||
131 | } | ||
132 | |||
133 | /* run command & read output */ | ||
134 | if ((pipe = popen(cmd.latin1(), "r")) == NULL) { | ||
135 | snprintf(result, 256, "Error: Can't run %s", cmd.latin1()); | ||
136 | TextViewOutput->append(result); | ||
137 | return; | ||
138 | } | ||
139 | 118 | ||
119 | QFile lmhosts("/etc/samba/lmhosts"); | ||
120 | QTextStream lms(&lmhosts); | ||
121 | lmhosts.open(IO_WriteOnly); | ||
122 | lms << "127.0.0.1 localhost\n"; | ||
123 | |||
140 | /* parse output and display in ListViewScan */ | 124 | /* parse output and display in ListViewScan */ |
141 | while(fgets(result, 256, pipe) != NULL) { | 125 | ccmd = "smbfind"; |
142 | /* put result into TextViewOutput */ | 126 | owarn <<"cmd: " << ccmd << oendl; |
143 | TextViewOutput->append(result); | 127 | runCommand(ccmd); |
144 | 128 | ||
145 | if( strstr(result, match.latin1()) != NULL ) { | 129 | QTextStream s(&out, IO_ReadOnly); |
146 | char ip_addr[256], host[256], *ptr1; | 130 | |
147 | 131 | while ( !s.atEnd() ) { | |
148 | strcpy( ip_addr, result); | 132 | QString ip_addr, host, output; |
149 | ptr1 = strchr(ip_addr,' '); | 133 | QString tmp = s.readLine(); |
150 | strcpy( host, ptr1); | 134 | bool ok; |
151 | ip_addr[ptr1 - ip_addr]='\0'; | 135 | tmp.left(1).toInt( &ok, 10 ); |
152 | 136 | if(ok) { | |
153 | for(i = 0; i < 256; i++) { | 137 | QStringList token = QStringList::split(' ', tmp ); |
154 | if(host[i]!=' ') { | 138 | ip_addr = token[0]; |
155 | strcpy( host, host + i); | 139 | host = token[1]; |
156 | break; | ||
157 | } | ||
158 | } | ||
159 | ptr1 = strchr(host,' '); | ||
160 | host[ptr1 - host] = '\0'; | ||
161 | |||
162 | owarn << "add host: " << host << oendl; | ||
163 | |||
164 | CBHost->insertItem( host, -1); | 140 | CBHost->insertItem( host, -1); |
165 | snprintf( lmhosts, 512, "echo '%s %s'>>/etc/samba/lmhosts", ip_addr,host); | 141 | lms << ip_addr+" "+host+"\n"; |
166 | |||
167 | owarn << "lmhosts: " << lmhosts << oendl; | ||
168 | |||
169 | if ((pipe2 = popen(lmhosts, "r")) == NULL) { | ||
170 | snprintf(result, 256, "Error: Can't run %s", lmhosts); | ||
171 | return; | ||
172 | } | ||
173 | } | 142 | } |
174 | } | 143 | } |
144 | lmhosts.close(); | ||
175 | 145 | ||
176 | TextViewOutput->append("\n\n============================================\n"); | 146 | TextViewOutput->append("\n\n============================================\n"); |
177 | LScan->setText(""); | 147 | LScan->setText(""); |
178 | scanning = false; | 148 | scanning = false; |
179 | } | 149 | } |
180 | 150 | ||
181 | void Qsmb::hostSelected(int index) | 151 | void Qsmb::hostSelected(int index) |
182 | { | 152 | { |
183 | owarn << "hostSelected" << oendl; | 153 | owarn << "hostSelected" << oendl; |
184 | int i; | 154 | int i; |
185 | 155 | ||
186 | QListViewItem *element; | 156 | QListViewItem *element; |
187 | QListViewItem *parent; | 157 | QListViewItem *parent; |
188 | 158 | ||
189 | QString text = CBHost->currentText(); | 159 | QString text = CBHost->currentText(); |
190 | 160 | ||
191 | if (scanning) return; | 161 | if (scanning) return; |
192 | scanning = true; | 162 | scanning = true; |
193 | 163 | ||
194 | QString cmd; | 164 | QString cmd; |
195 | char result[256]; | 165 | char result[256]; |
196 | 166 | ||
197 | FILE *pipe; | 167 | FILE *pipe; |
198 | 168 | ||
199 | LScan->setText("Scanning..."); | 169 | LScan->setText("Scanning..."); |
200 | 170 | ||
201 | if(username->text().isEmpty()) | 171 | if(username->text().isEmpty()) |
202 | cmd = "/usr/bin/smbclient -L //"+CBHost->currentText()+" -N 2>&1 |grep Disk"; | 172 | cmd = "/usr/bin/smbclient -L //"+CBHost->currentText()+" -N 2>&1 |grep Disk"; |
203 | else | 173 | else |
204 | cmd = "/usr/bin/smbclient -L //"+CBHost->currentText()+" -N -U "+username->text()+"\%"+password->text()+" 2>&1 |grep Disk"; | 174 | cmd = "/usr/bin/smbclient -L //"+CBHost->currentText()+" -N -U "+username->text()+"\%"+password->text()+" 2>&1 |grep Disk"; |
205 | 175 | ||
206 | // for(i = 0; i < 512; i++) { | 176 | // for(i = 0; i < 512; i++) { |
207 | // if(cmd[i]==':') { | 177 | // if(cmd[i]==':') { |
208 | // cmd[i]='%'; | 178 | // cmd[i]='%'; |
209 | // break; | 179 | // break; |
210 | // } | 180 | // } |
211 | // if(cmd[i]=='\0') | 181 | // if(cmd[i]=='\0') |
212 | // break; | 182 | // break; |
213 | // } | 183 | // } |
214 | 184 | ||
215 | owarn << "i="<< index << "cmd:" << cmd << oendl; | 185 | owarn << "i="<< index << "cmd:" << cmd << oendl; |
216 | 186 | ||
217 | TextViewOutput->append(cmd); | 187 | TextViewOutput->append(cmd); |
218 | 188 | ||
219 | /* run smbclient & read output */ | 189 | /* run smbclient & read output */ |
220 | if ((pipe = popen(cmd.latin1(), "r")) == NULL) { | 190 | if ((pipe = popen(cmd.latin1(), "r")) == NULL) { |
221 | snprintf(result, 256, "Error: Can't run %s", cmd.latin1()); | 191 | snprintf(result, 256, "Error: Can't run %s", cmd.latin1()); |
222 | // cmd = "Error: Can't run "+cmd; | 192 | // cmd = "Error: Can't run "+cmd; |
223 | TextViewOutput->append(result); | 193 | TextViewOutput->append(result); |
224 | return; | 194 | return; |
225 | } | 195 | } |
226 | 196 | ||
227 | /* parse output and display in ListViewScan */ | 197 | /* parse output and display in ListViewScan */ |
228 | while(fgets(result, 256, pipe) != NULL) { | 198 | while(fgets(result, 256, pipe) != NULL) { |
229 | /* put result into TextViewOutput */ | 199 | /* put result into TextViewOutput */ |
230 | TextViewOutput->append(result); | 200 | TextViewOutput->append(result); |
231 | 201 | ||
232 | if( strchr(result, '$') == NULL ) { | 202 | if( strchr(result, '$') == NULL ) { |
233 | char share[256], *ptr1; | 203 | char share[256], *ptr1; |
234 | 204 | ||
235 | strcpy(share,result); | 205 | strcpy(share,result); |
236 | ptr1 = strchr(share,' '); | 206 | ptr1 = strchr(share,' '); |
237 | share[ptr1 - share]='\0'; | 207 | share[ptr1 - share]='\0'; |
238 | 208 | ||
239 | owarn<< "add share: " << share << oendl; | 209 | owarn<< "add share: " << share << oendl; |
240 | 210 | ||
241 | if(top_element != NULL) { | 211 | if(top_element != NULL) { |
242 | bool found = false; | 212 | bool found = false; |
243 | element = top_element; | 213 | element = top_element; |
244 | 214 | ||
245 | while(element != NULL && !found) { | 215 | while(element != NULL && !found) { |
246 | if(strcmp( element->text(0).ascii(), share)==0) { | 216 | if(strcmp( element->text(0).ascii(), share)==0) { |
247 | parent = element; | 217 | parent = element; |
248 | found = true; | 218 | found = true; |
249 | } | 219 | } |
250 | element = element->nextSibling(); | 220 | element = element->nextSibling(); |
251 | } | 221 | } |
252 | 222 | ||
253 | if(!found) { | 223 | if(!found) { |
254 | element = new QListViewItem(ListViewScan,share); | 224 | element = new QListViewItem(ListViewScan,share); |
255 | element->setOpen(true); | 225 | element->setOpen(true); |
256 | parent=element; | 226 | parent=element; |
257 | } | 227 | } |
258 | } else { | 228 | } else { |
259 | element = new QListViewItem(ListViewScan,share); | 229 | element = new QListViewItem(ListViewScan,share); |
260 | element->setOpen(true); | 230 | element->setOpen(true); |
261 | top_element = element; | 231 | top_element = element; |
262 | parent = element; | 232 | parent = element; |
263 | } | 233 | } |
264 | } | 234 | } |
265 | } | 235 | } |
266 | 236 | ||
267 | TextViewOutput->append("\n\n============================================\n"); | 237 | TextViewOutput->append("\n\n============================================\n"); |
268 | LScan->setText(""); | 238 | LScan->setText(""); |
269 | scanning = false; | 239 | scanning = false; |
270 | } | 240 | } |