1 | /* |
---|
2 | * Abuse - dark 2D side-scrolling platform game |
---|
3 | * Copyright (c) 1995 Crack dot Com |
---|
4 | * Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net> |
---|
5 | * |
---|
6 | * This software was released into the Public Domain. As with most public |
---|
7 | * domain software, no warranty is made or implied by Crack dot Com, by |
---|
8 | * Jonathan Clark, or by Sam Hocevar. |
---|
9 | */ |
---|
10 | |
---|
11 | #if defined HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #if HAVE_NETWORK |
---|
16 | |
---|
17 | #include "common.h" |
---|
18 | |
---|
19 | #include "netfile.h" |
---|
20 | #include "../inc/netface.h" |
---|
21 | #include "engine.h" |
---|
22 | |
---|
23 | nfs_client *first_nfs_client=NULL; |
---|
24 | remote_file *remote_file_list=NULL; |
---|
25 | char default_fs_name[256]; // default file server name (set with parm -fs) |
---|
26 | |
---|
27 | nfs_client::nfs_client(net_socket *sock, int file_fd, nfs_client *next) : |
---|
28 | sock(sock),file_fd(file_fd),next(next),size_to_read(0) |
---|
29 | { |
---|
30 | sock->read_selectable(); |
---|
31 | } |
---|
32 | |
---|
33 | |
---|
34 | nfs_client::~nfs_client() |
---|
35 | { |
---|
36 | delete sock; |
---|
37 | if (file_fd>=0) |
---|
38 | close(file_fd); |
---|
39 | } |
---|
40 | |
---|
41 | void secure_filename(char *filename, char *mode) |
---|
42 | { |
---|
43 | if (!no_security) |
---|
44 | { |
---|
45 | if (filename[0]=='/') { filename[0]=0; return ; } |
---|
46 | int level=0; |
---|
47 | char *f=filename; |
---|
48 | while (*f) |
---|
49 | { |
---|
50 | if (*f=='/') { f++; level++; } |
---|
51 | else if (*f=='.' && f[1]=='.') |
---|
52 | { |
---|
53 | if (f[3]=='.') while (*f!='.') f++; |
---|
54 | else |
---|
55 | { |
---|
56 | f+=2; |
---|
57 | level--; |
---|
58 | } |
---|
59 | } else f++; |
---|
60 | |
---|
61 | } |
---|
62 | if (level<0) |
---|
63 | filename[0]=0; |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | int local_address(char *server_name) // returns 1 if server name is ourself |
---|
69 | { |
---|
70 | struct hostent *hn=gethostbyname(server_name); // first check to see if this address is 127.0.0.1 |
---|
71 | if (!hn) return 0; // if bad server_name, return false |
---|
72 | char **ip_address=hn->h_addr_list; |
---|
73 | while (*ip_address) |
---|
74 | { |
---|
75 | char *a=*ip_address; |
---|
76 | if (a[0]==127 && a[1]==0 && a[2]==0 && a[3]==1) |
---|
77 | return 1; |
---|
78 | ip_address++; |
---|
79 | } |
---|
80 | char server_ip[4]; |
---|
81 | memcpy(server_ip,hn->h_addr_list,4); |
---|
82 | |
---|
83 | char my_name[100]; // now check to see if this address is 'hostname' |
---|
84 | gethostname(my_name,100); |
---|
85 | struct hostent *l_hn=gethostbyname(my_name); |
---|
86 | char **l_ip_address=l_hn->h_addr_list; |
---|
87 | |
---|
88 | while (*l_ip_address) // local ip_address |
---|
89 | { |
---|
90 | char *a=*l_ip_address; // scan through all local ip's |
---|
91 | ip_address=hn->h_addr_list; |
---|
92 | while (*ip_address) // scan through all ip's for server_name |
---|
93 | { |
---|
94 | char *b=server_ip; |
---|
95 | if (a[0]==b[0] && a[1]==b[1] && a[2]==b[2] && a[3]==b[3]) // check for match |
---|
96 | return 1; |
---|
97 | ip_address++; |
---|
98 | } |
---|
99 | l_ip_address++; |
---|
100 | } |
---|
101 | return 0; // didn't match localhost nor hostname, must be somewhere else |
---|
102 | } |
---|
103 | |
---|
104 | int nfs_client::send_read() // return 0 if failure on socket, not failure to read |
---|
105 | { |
---|
106 | if (file_fd>=0 && socket_fd>=0) |
---|
107 | { |
---|
108 | // first make sure the socket isn't 'full' |
---|
109 | |
---|
110 | struct timeval tv={ 0,0}; // don't wait |
---|
111 | fd_set write_check; |
---|
112 | FD_ZERO(&write_check); |
---|
113 | FD_SET(socket_fd,&write_check); |
---|
114 | select(FD_SETSIZE,NULL,&write_check,NULL,&tv); |
---|
115 | |
---|
116 | if (FD_ISSET(socket_fd,&write_check)) // ready to write? |
---|
117 | { |
---|
118 | char buf[READ_PACKET_SIZE]; |
---|
119 | int16_t read_total; |
---|
120 | int16_t actual; |
---|
121 | |
---|
122 | do |
---|
123 | { |
---|
124 | read_total=size_to_read>(READ_PACKET_SIZE-2) ? (READ_PACKET_SIZE-2) : size_to_read; |
---|
125 | actual=read(file_fd,buf,read_total); |
---|
126 | actual=lstl(actual); |
---|
127 | if (write(socket_fd,&actual,sizeof(actual))!=sizeof(actual)) |
---|
128 | { |
---|
129 | fprintf(stderr,"write failed\n"); |
---|
130 | return 0; |
---|
131 | } |
---|
132 | actual=lstl(actual); |
---|
133 | |
---|
134 | int write_amount=write(socket_fd,buf,actual); |
---|
135 | if (write_amount!=actual) |
---|
136 | { |
---|
137 | fprintf(stderr,"write failed\n"); |
---|
138 | return 0; |
---|
139 | } |
---|
140 | |
---|
141 | size_to_read-=actual; |
---|
142 | |
---|
143 | FD_ZERO(&write_check); |
---|
144 | FD_SET(socket_fd,&write_check); |
---|
145 | select(FD_SETSIZE,NULL,&write_check,NULL,&tv); |
---|
146 | |
---|
147 | if (!FD_ISSET(socket_fd,&write_check)) |
---|
148 | { |
---|
149 | FD_SET(socket_fd,&master_write_set); // socket is full, wait for it empty |
---|
150 | FD_CLR(socket_fd,&master_set); // don't check for reading or process commands till read is done |
---|
151 | return 1; // not ok to write anymore, try again latter |
---|
152 | } |
---|
153 | |
---|
154 | } while (size_to_read && actual==read_total); |
---|
155 | size_to_read=0; |
---|
156 | FD_CLR(socket_fd,&master_write_set); // don't check this socket for write ok |
---|
157 | FD_SET(socket_fd,&master_set); // check it for reading |
---|
158 | return 1; |
---|
159 | } else |
---|
160 | { |
---|
161 | FD_SET(socket_fd,&master_write_set); // socket is full, wait for it empty |
---|
162 | FD_CLR(socket_fd,&master_set); // don't check for reading or process commands till read is done |
---|
163 | return 1; |
---|
164 | } |
---|
165 | } |
---|
166 | return 0; |
---|
167 | } |
---|
168 | |
---|
169 | |
---|
170 | int process_nfs_command(nfs_client *c) |
---|
171 | { |
---|
172 | char cmd; |
---|
173 | if (read(c->socket_fd,&cmd,1)!=1) return 0; |
---|
174 | switch (cmd) |
---|
175 | { |
---|
176 | case NFCMD_READ : |
---|
177 | { |
---|
178 | int32_t size; |
---|
179 | if (read(c->socket_fd,&size,sizeof(size))!=sizeof(size)) return 0; |
---|
180 | size=lltl(size); |
---|
181 | |
---|
182 | c->size_to_read=size; |
---|
183 | return c->send_read(); |
---|
184 | } break; |
---|
185 | case NFCMD_CLOSE : |
---|
186 | { |
---|
187 | return 0; |
---|
188 | } break; |
---|
189 | case NFCMD_SEEK : |
---|
190 | { |
---|
191 | int32_t offset; |
---|
192 | if (read(c->socket_fd,&offset,sizeof(offset))!=sizeof(offset)) return 0; |
---|
193 | offset=lltl(offset); |
---|
194 | offset=lseek(c->file_fd,offset,0); |
---|
195 | offset=lltl(offset); |
---|
196 | if (write(c->socket_fd,&offset,sizeof(offset))!=sizeof(offset)) return 0; |
---|
197 | return 1; |
---|
198 | } break; |
---|
199 | case NFCMD_TELL : |
---|
200 | { |
---|
201 | int32_t offset=lseek(c->file_fd,0,SEEK_CUR); |
---|
202 | offset=lltl(offset); |
---|
203 | if (write(c->socket_fd,&offset,sizeof(offset))!=sizeof(offset)) return 0; |
---|
204 | return 1; |
---|
205 | } break; |
---|
206 | |
---|
207 | default : |
---|
208 | { fprintf(stderr,"net driver : bad command from nfs client\n"); |
---|
209 | return 0; |
---|
210 | } |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | |
---|
215 | |
---|
216 | void add_nfs_client(int fd) |
---|
217 | { |
---|
218 | uint8_t size[2]; |
---|
219 | char filename[300],mode[20],*mp; |
---|
220 | if (read(fd,size,2)!=2) { close(fd); return ; } |
---|
221 | if (read(fd,filename,size[0])!=size[0]) { close(fd); return ; } |
---|
222 | if (read(fd,mode,size[1])!=size[1]) { close(fd); return ; } |
---|
223 | |
---|
224 | fprintf(stderr,"remote request for %s ",filename); |
---|
225 | |
---|
226 | secure_filename(filename,mode); // make sure this filename isn't a security risk |
---|
227 | if (filename[0]==0) { fprintf(stderr,"(denied)\n"); close(fd); return ; } |
---|
228 | |
---|
229 | mp=mode; |
---|
230 | int flags=0; |
---|
231 | |
---|
232 | while (*mp) |
---|
233 | { |
---|
234 | if (*mp=='w') flags|=O_CREAT|O_RDWR; |
---|
235 | else if (*mp=='r') flags|=O_RDONLY; |
---|
236 | mp++; |
---|
237 | } |
---|
238 | |
---|
239 | int f=open(filename,flags,S_IRWXU | S_IRWXG | S_IRWXO); |
---|
240 | |
---|
241 | if (f<0) |
---|
242 | { |
---|
243 | fprintf(stderr,"(not found)\n"); |
---|
244 | f=-1; // make sure this is -1 |
---|
245 | } |
---|
246 | |
---|
247 | int32_t ret=lltl(f); |
---|
248 | if (write(fd,&ret,sizeof(ret))!=sizeof(ret)) { close(fd); return ; } |
---|
249 | |
---|
250 | if (f<0) // no file, sorry |
---|
251 | close(fd); |
---|
252 | else |
---|
253 | { |
---|
254 | int32_t cur_pos=lseek(f,0,SEEK_CUR); |
---|
255 | int32_t size=lseek(f,0,SEEK_END); |
---|
256 | lseek(f,cur_pos,SEEK_SET); |
---|
257 | size=lltl(size); |
---|
258 | if (write(fd,&size,sizeof(size))!=sizeof(size)) { close(f); close(fd); return ; } |
---|
259 | |
---|
260 | first_nfs_client=new nfs_client(fd,f,first_nfs_client); |
---|
261 | first_nfs_client->size=size; |
---|
262 | } |
---|
263 | } |
---|
264 | |
---|
265 | void remote_file::r_close(char *reason) |
---|
266 | { |
---|
267 | if (reason) |
---|
268 | fprintf(stderr,"remote_file : %s\n",reason); |
---|
269 | if (socket_fd>=0) |
---|
270 | { |
---|
271 | uint8_t cmd=NFCMD_CLOSE; |
---|
272 | write(socket_fd,&cmd,1); |
---|
273 | close(socket_fd); |
---|
274 | } |
---|
275 | socket_fd=-1; |
---|
276 | } |
---|
277 | |
---|
278 | remote_file::remote_file(char *filename, char *mode, remote_file *Next) |
---|
279 | { |
---|
280 | next=Next; |
---|
281 | open_local=0; |
---|
282 | |
---|
283 | socket_fd=connect_to_server(filename); |
---|
284 | if (socket_fd==-1) |
---|
285 | { |
---|
286 | fprintf(stderr,"unable to connect\n"); |
---|
287 | return ; |
---|
288 | } |
---|
289 | |
---|
290 | uint8_t sizes[3]={ CLIENT_NFS,strlen(filename)+1,strlen(mode)+1}; |
---|
291 | if (write(socket_fd,sizes,3)!=3) { r_close("could not send open info"); return ; } |
---|
292 | if (write(socket_fd,filename,sizes[1])!=sizes[1]) { r_close("could not send filename"); return ; } |
---|
293 | if (write(socket_fd,mode,sizes[2])!=sizes[2]) { r_close("could not send mode"); return ; } |
---|
294 | |
---|
295 | int32_t remote_file_fd; |
---|
296 | if (read(socket_fd,&remote_file_fd,sizeof(remote_file_fd))!=sizeof(remote_file_fd)) |
---|
297 | { r_close("could not read remote fd"); return ; } |
---|
298 | remote_file_fd=lltl(remote_file_fd); |
---|
299 | if (remote_file_fd<0) { r_close("remote fd is bad"); return ; } |
---|
300 | |
---|
301 | if (read(socket_fd,&size,sizeof(size))!=sizeof(size)) { r_close("could not read remote filesize"); return ; } |
---|
302 | // uint32_t remote_crc; |
---|
303 | // if (read(socket_fd,&remote_crc,sizeof(remote_crc))!=sizeof(remote_crc)) { r_close("could not read remote checksum"); return ; } |
---|
304 | // uint32_t local_crc= |
---|
305 | |
---|
306 | size=lltl(size); |
---|
307 | } |
---|
308 | |
---|
309 | int remote_file::unbuffered_read(int out_fd, size_t count) |
---|
310 | { |
---|
311 | if (socket_fd>=0 && count) |
---|
312 | { |
---|
313 | uint8_t cmd=NFCMD_READ; |
---|
314 | if (write(socket_fd,&cmd,sizeof(cmd))!=sizeof(cmd)) { r_close("read : could not send command"); return 0; } |
---|
315 | |
---|
316 | int32_t rsize=lltl(count); |
---|
317 | if (write(socket_fd,&rsize,sizeof(rsize))!=sizeof(rsize)) { r_close("read : could not send size"); return 0; } |
---|
318 | |
---|
319 | int32_t total_read=0,total; |
---|
320 | char buf[READ_PACKET_SIZE]; |
---|
321 | uint16_t size; |
---|
322 | |
---|
323 | uint16_t packet_size; |
---|
324 | do |
---|
325 | { |
---|
326 | if (read(socket_fd,&packet_size,sizeof(packet_size))!=sizeof(packet_size)) |
---|
327 | { |
---|
328 | fprintf(stderr,"could not read packet size\n"); |
---|
329 | return 0; |
---|
330 | } |
---|
331 | packet_size=lstl(packet_size); |
---|
332 | |
---|
333 | uint16_t size_read=read(socket_fd,buf+2,packet_size); |
---|
334 | |
---|
335 | if (size_read!=packet_size) |
---|
336 | { |
---|
337 | if (read(socket_fd,buf+2+size_read,packet_size-size_read)!=packet_size-size_read) |
---|
338 | { |
---|
339 | fprintf(stderr,"incomplete packet\n"); |
---|
340 | return 0; |
---|
341 | } |
---|
342 | } |
---|
343 | |
---|
344 | *((int16_t *)buf)=packet_size; |
---|
345 | if (write(out_fd,buf,packet_size+2)!=packet_size+2) comm_failed(); |
---|
346 | |
---|
347 | total_read+=packet_size; |
---|
348 | count-=packet_size; |
---|
349 | } while (packet_size==READ_PACKET_SIZE-2 && count); |
---|
350 | return total_read; |
---|
351 | } |
---|
352 | return 0; |
---|
353 | } |
---|
354 | |
---|
355 | int remote_file::unbuffered_tell() // ask server where the offset of the file pointer is |
---|
356 | { |
---|
357 | if (socket_fd>=0) |
---|
358 | { |
---|
359 | uint8_t cmd=NFCMD_TELL; |
---|
360 | if (write(socket_fd,&cmd,sizeof(cmd))!=sizeof(cmd)) { r_close("tell : could not send command"); return 0; } |
---|
361 | |
---|
362 | int32_t offset; |
---|
363 | if (read(socket_fd,&offset,sizeof(offset))!=sizeof(offset)) { r_close("tell : could not read offset"); return 0; } |
---|
364 | return lltl(offset); |
---|
365 | } |
---|
366 | return 0; |
---|
367 | } |
---|
368 | |
---|
369 | int remote_file::unbuffered_seek(int32_t offset) // tell server to seek to a spot in a file |
---|
370 | { |
---|
371 | if (socket_fd>=0) |
---|
372 | { |
---|
373 | uint8_t cmd=NFCMD_SEEK; |
---|
374 | if (write(socket_fd,&cmd,sizeof(cmd))!=sizeof(cmd)) { r_close("seek : could not send command"); return 0; } |
---|
375 | |
---|
376 | int32_t off=lltl(offset); |
---|
377 | if (write(socket_fd,&off,sizeof(off))!=sizeof(off)) { r_close("seek : could not send offset"); return 0; } |
---|
378 | |
---|
379 | if (read(socket_fd,&offset,sizeof(offset))!=sizeof(offset)) { r_close("seek : could not read offset"); return 0; } |
---|
380 | return lltl(offset); |
---|
381 | } |
---|
382 | return 0; |
---|
383 | } |
---|
384 | |
---|
385 | int open_file(char *&filename, char *mode) |
---|
386 | { |
---|
387 | if (filename[0]!='/' && filename[1]!='/' && default_fs_name[0]) // default file server? |
---|
388 | { |
---|
389 | char tmp_fn[500]; |
---|
390 | sprintf(tmp_fn,"//%s/%s",default_fs_name,filename); |
---|
391 | strcpy(filename,tmp_fn); |
---|
392 | } |
---|
393 | |
---|
394 | if (filename[0]=='/' && filename[1]=='/') // passive server file reference? |
---|
395 | { |
---|
396 | filename+=2; |
---|
397 | remote_file *rf=new remote_file(filename,mode,remote_file_list); |
---|
398 | if (rf->open_failure()) |
---|
399 | { |
---|
400 | delete rf; |
---|
401 | return -1; |
---|
402 | } |
---|
403 | else |
---|
404 | { |
---|
405 | remote_file_list=rf; |
---|
406 | return rf->socket_fd; |
---|
407 | } |
---|
408 | } |
---|
409 | |
---|
410 | secure_filename(filename,mode); |
---|
411 | if (filename[0]==0) return -1; |
---|
412 | |
---|
413 | int flags=0; |
---|
414 | while (*mode) |
---|
415 | { |
---|
416 | if (*mode=='w') flags|=O_CREAT|O_RDWR; |
---|
417 | else if (*mode=='r') flags|=O_RDONLY; |
---|
418 | mode++; |
---|
419 | } |
---|
420 | |
---|
421 | int f=open(filename,flags,S_IRWXU | S_IRWXG | S_IRWXO); |
---|
422 | if (f>=0) |
---|
423 | { close(f); |
---|
424 | return -2; |
---|
425 | } |
---|
426 | |
---|
427 | return -1; |
---|
428 | } |
---|
429 | |
---|
430 | remote_file *find_rfile(int fd) |
---|
431 | { |
---|
432 | remote_file *r=remote_file_list; |
---|
433 | for (; r && r->socket_fd!=fd; r=r->next) |
---|
434 | { |
---|
435 | if (r->socket_fd==-1) |
---|
436 | { |
---|
437 | fprintf(stderr,"bad sock\n"); |
---|
438 | } |
---|
439 | } |
---|
440 | return r; |
---|
441 | } |
---|
442 | |
---|
443 | void unlink_remote_file(remote_file *rf) |
---|
444 | { |
---|
445 | if (rf==remote_file_list) |
---|
446 | remote_file_list=rf->next; |
---|
447 | else |
---|
448 | { |
---|
449 | remote_file *last=remote_file_list; |
---|
450 | while (last->next && last->next!=rf) last=last->next; |
---|
451 | last->next=rf->next; |
---|
452 | } |
---|
453 | } |
---|
454 | |
---|
455 | remote_file::~remote_file() |
---|
456 | { r_close(NULL); } |
---|
457 | |
---|
458 | |
---|
459 | int fetch_crcs(char *server) |
---|
460 | { |
---|
461 | int socket_fd=connect_to_server(server); |
---|
462 | if (socket_fd==-1) |
---|
463 | { |
---|
464 | fprintf(stderr,"unable to connect\n"); |
---|
465 | return 0; |
---|
466 | } |
---|
467 | |
---|
468 | uint8_t cmd=CLIENT_CRC_WAITER; |
---|
469 | if (write(socket_fd,&cmd,1)!=1) { close(socket_fd); return 0; } |
---|
470 | if (read(socket_fd,&cmd,1)!=1) { close(socket_fd); return 0; } |
---|
471 | close(socket_fd); |
---|
472 | return cmd; |
---|
473 | |
---|
474 | } |
---|
475 | |
---|
476 | #endif // HAVE_NETWORK |
---|
477 | |
---|