ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/login.ext
(Generate patch)

Comparing deliantra/server/ext/login.ext (file contents):
Revision 1.110 by root, Sat May 8 10:49:44 2010 UTC vs.
Revision 1.123 by root, Fri Nov 16 12:02:46 2012 UTC

2 2
3# login handling 3# login handling
4 4
5use Fcntl; 5use Fcntl;
6use Coro::AIO; 6use Coro::AIO;
7use Deliantra::Util ();
7 8
8our $MAX_DISCONNECT_TIME = $cf::CFG{max_disconnect_time} || 3600; 9CONF MAX_DISCONNECT_TIME = 3600;
9
10# paranoia function to overwrite a string-in-place
11sub nuke_str {
12 substr $_[0], 0, (length $_[0]), "x" x length $_[0]
13}
14 10
15sub query { 11sub query {
16 my ($ns, $flags, $text) = @_; 12 my ($ns, $flags, $text) = @_;
17 13
18 $ns->query ($flags, $text, Coro::rouse_cb); 14 $ns->query ($flags, $text, Coro::rouse_cb);
59 my $m = $ob->map 55 my $m = $ob->map
60 or return; 56 or return;
61 my $x = $ob->x; 57 my $x = $ob->x;
62 my $y = $ob->y; 58 my $y = $ob->y;
63 59
64# return 0;#d# 60 # never happens normally, but helps when shell users make mistakes
65# warn join ":", $m->at ($x, $y);#d# 61 $m->linkable
66# warn "FOO$m { ".scalar ($m->at ($x, $y))." }\n"; 62 or return 1;
67# return 0;
68 63
69 scalar grep $_->type == cf::SAVEBED, $m->at ($x, $y) 64 scalar grep $_->type == cf::SAVEBED, $m->at ($x, $y)
70} 65}
71 66
72sub enter_map { 67sub enter_map {
127 122
128 $ob->goto ($map, $x, $y); 123 $ob->goto ($map, $x, $y);
129} 124}
130 125
131sub encode_password($) { 126sub encode_password($) {
132# crypt $_[0],
133# join '',
134# ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[(cf::rndm 64), (cf::rndm 64)]
135 "!" . unpack "H*", $_[0] 127 "!" . unpack "H*", $_[0]
136} 128}
137 129
138sub compare_password($$) { 130sub compare_password($$) {
139 my ($pass, $token) = @_; 131 my ($pass, $token) = @_;
140 132
141 if ($token =~ /\!(.*)/) { 133 if ($token =~ /!!(.*)/) {
134 return +(substr $pass, 0, 8) eq pack "H*", $1;
135 } elsif ($token =~ /!(.*)/) {
142 return $pass eq pack "H*", $1; 136 return $pass eq pack "H*", $1;
143 } else { 137 } else {
144 return $token eq crypt $pass, $token; 138 return $token eq crypt $pass, $token;
145 } 139 }
146} 140}
147 141
148# delete a player directory, be non-blocking AND synchronous... 142# delete a player directory
149# (that's hard, so we crap out and fork).
150sub nuke_playerdir { 143sub nuke_playerdir {
151 my ($user) = @_; 144 my ($user) = @_;
152 145
146 my $lock = cf::lock_acquire "ext::login::nuke_playerdir";
147
153 my $temp = "$PLAYERDIR/~$Coro::current~deleting~"; 148 my $temp = "$PLAYERDIR/~$Coro::current~deleting~";
154
155 cf::fork_call {
156 rename "$PLAYERDIR/$user", $temp; 149 aio_rename "$PLAYERDIR/$user", $temp;
157 system "rm", "-rf", $temp; 150 IO::AIO::aio_rmtree $temp;
151}
152
153sub login_done {
154 my ($pl) = @_;
155
156 if (0 < Coro::AIO::aio_load "$cf::CONFDIR/motd", my $motd) {
157 $pl->ns->send_msg ("c/motd" => $motd, cf::NDI_CLEAR);
158 }; 158 }
159}
160
161sub chargen {
162 my ($ns, $user, $pass) = @_;
163
164 # the rest of this function is character creation
165 $Coro::current->{desc} = "addme($user) chargen";
166
167 # just to make sure nothing is left over
168 # normally, nothing is there.
169 nuke_playerdir $user;
170
171 my $pl = cf::player::new $user;
172 $pl->password (encode_password $pass);
173 $pl->connect ($ns);
174 my $ob = $pl->ob;
175
176 $ob->goto ($pl->maplevel, $ob->x, $ob->y);
177
178 while () {
179 $ob->update_stats;
180 $pl->save_stats;
181
182 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
183 "[y] to roll new stats [n] to use stats\n[1-7] [1-7] to swap stats.\nRoll again (y/n/1-7)?";
184
185 if ($res =~ /^[Nn]/) {
186 last;
187 } elsif ($res > 0 && $res <= 7) {
188 my $swap = query $ns, cf::CS_QUERY_SINGLECHAR, "Swap stat with (will not roll new stats) [1-7]?";
189
190 if ($swap > 0 && $swap <= 7) {
191 $ob->swap_stats ($res - 1, $swap - 1);
192 }
193 } else {
194 $ob->roll_stats;
195 }
196
197 Coro::Timer::sleep 0.05;
198 }
199
200 $ob->set_animation (2);
201 $ob->add_statbonus;
202
203 while () {
204 $ns->send_msg ("chargen-race-title", ucfirst $pl->title, -1);
205 my $msg = $ob->msg;
206 $msg =~ s/(?<=\S)\n(?=\S)/ /g;
207 $ns->send_msg ("chargen-race-description", $msg, cf::NDI_BLUE);
208
209 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
210 "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n";
211
212 last if $res =~ /[dD]/;
213
214 $pl->chargen_race_next;
215 Coro::Timer::sleep 0.05;
216 }
217
218 # create the playerdir, if necessary, as chargen_race_done did it before
219 # presumably because of unique maps
220 aio_mkdir playerdir $pl, 0770;
221 $pl->chargen_race_done;
222
223 while () {
224 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
225 "Now choose a gender.\nPress 'f' to become female, and 'm' to become male.\n";
226
227 if ($res =~ /^[fF]/) {
228 $pl->gender (1);
229 last;
230 } elsif ($res =~ /^[mM]/) {
231 $pl->gender (0);
232 last;
233 }
234 Coro::Timer::sleep 0.05;
235 }
236
237 $ob->reply (undef, "Welcome to Deliantra!");
238
239 # XXX: Workaround for delayed client ext protocol handshake
240 $pl->esrv_new_player;
241
242 delete $pl->{deny_save};
159} 243}
160 244
161cf::client->attach (on_addme => sub { 245cf::client->attach (on_addme => sub {
162 my ($ns) = @_; 246 my ($ns) = @_;
163 247
164 $ns->pl and return $ns->destroy; 248 $ns->{addme}++ and return $ns->destroy;
165 249
166 $ns->async (sub { 250 $ns->async (sub {
167 $Coro::current->{desc} = "addme init"; 251 $Coro::current->{desc} = "addme init";
168 252
169 my ($user, $pass); 253 my ($user, $pass);
244 aio_stat $pl->path and next; 328 aio_stat $pl->path and next;
245 my $mtime = (stat _)[9]; 329 my $mtime = (stat _)[9];
246 my $token = $pl->password; 330 my $token = $pl->password;
247 331
248 if ($cf::CFG{ext_login_nocheck} or compare_password $pass, $token) { 332 if ($cf::CFG{ext_login_nocheck} or compare_password $pass, $token) {
249 $pl->password (encode_password $pass); # make sure we store the new encoding #d# 333 # player exists and passwords match - we can proceed
250 nuke_str $pass; 334
251 # password matches, wonderful 335 # password matches, wonderful
252 my $pl = cf::player::find $user or next; 336 my $pl = cf::player::find $user or next;
253 $pl->connect ($ns); 337 $pl->connect ($ns);
254 enter_map $pl; 338 enter_map $pl;
339 login_done $pl;
255 last; 340 return;
256 } elsif (can_cleanup $pl, $mtime) { 341 } elsif (can_cleanup $pl, $mtime) {
257 Coro::Timer::sleep 1; 342 Coro::Timer::sleep 1;
258 343
259 $ns->send_drawinfo ( 344 $ns->send_drawinfo (
260 "Player exists, but password does not match. If this is your account, " 345 "Player exists, but password does not match. If this is your account, "
261 . "please try again. If not, you can now decide to take over this account " 346 . "please try again. If not, you can now decide to take over this account "
262 . "because it has not been in-use for some time.", 347 . "because it has not been in-use for some time.",
263 cf::NDI_RED 348 cf::NDI_RED
264 ); 349 );
265 350
266 #TODO: nuke_str
267 (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/ 351 (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/
268 or next; 352 or next;
269 353
270 # check if the file hasn't changed 354 # check if the file hasn't changed
271 aio_stat cf::player::path $user and next; 355 aio_stat cf::player::path $user and next;
273 357
274 $pl->quit_character; 358 $pl->quit_character;
275 359
276 # fall through to creation 360 # fall through to creation
277 } else { 361 } else {
278 nuke_str $pass;
279
280 Coro::Timer::sleep 1; 362 Coro::Timer::sleep 1;
281 363
282 $ns->send_drawinfo ( 364 $ns->send_drawinfo (
283 "Wrong username or password. Please try again " 365 "Wrong username or password. Please try again "
284 . "(check for Numlock and other semi-obvious error sources).", 366 . "(check for Numlock and other semi-obvious error sources).",
286 ); 368 );
287 next; 369 next;
288 } 370 }
289 } else { 371 } else {
290 # unable to load the playerfile: 372 # unable to load the playerfile:
291 # check wether the player dir exists, which means the file is corrupted or 373 # check whether the player dir exists, which means the file is corrupted or
292 # something very similar. 374 # something very similar.
293 if (!aio_stat cf::player::playerdir $user) { 375 if (!aio_stat cf::player::playerdir $user) {
294 $ns->send_drawinfo ( 376 $ns->send_drawinfo (
295 "Unable to retrieve this player. It might be a locked or broken account. " 377 "Unable to retrieve this player. It might be a locked or broken account. "
296 . "If this is your account, ask a dungeon master for assistance. " 378 . "If this is your account, ask a dungeon master for assistance. "
299 ); 381 );
300 next; 382 next;
301 } 383 }
302 } 384 }
303 385
304 # the rest of this function is character creation
305 $Coro::current->{desc} = "addme($user) chargen";
306
307 # just to make sure nothing is left over
308 nuke_playerdir $user;
309
310 my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again."; 386 my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again.";
311 387
312 if ($pass2 ne $pass) { 388 if ($pass2 ne $pass) {
313 nuke_str $pass;
314 nuke_str $pass2;
315 $ns->send_drawinfo ( 389 $ns->send_drawinfo (
316 "The passwords do not match, please try again.", 390 "The passwords do not match, please try again.",
317 cf::NDI_RED 391 cf::NDI_RED
318 ); 392 );
319 Coro::Timer::sleep 0.5; 393 Coro::Timer::sleep 0.5;
320 next; 394 next;
321 } 395 }
322 396
323 nuke_str $pass2;
324
325 my $pl = cf::player::new $user;
326 $pl->password (encode_password $pass);
327 nuke_str $pass;
328 $pl->connect ($ns);
329 my $ob = $pl->ob;
330
331 $ob->goto ($pl->maplevel, $ob->x, $ob->y);
332
333 while () {
334 $ob->update_stats;
335 $pl->save_stats;
336
337 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
338 "[y] to roll new stats [n] to use stats\n[1-7] [1-7] to swap stats.\nRoll again (y/n/1-7)?";
339
340 if ($res =~ /^[Nn]/) {
341 last;
342 } elsif ($res > 0 && $res <= 7) {
343 my $swap = query $ns, cf::CS_QUERY_SINGLECHAR, "Swap stat with (will not roll new stats) [1-7]?";
344
345 if ($swap > 0 && $swap <= 7) {
346 $ob->swap_stats ($res - 1, $swap - 1);
347 }
348 } else {
349 $ob->roll_stats;
350 }
351
352 Coro::Timer::sleep 0.05;
353 }
354
355 $ob->set_animation (2);
356 $ob->add_statbonus;
357
358 while () {
359 $ns->send_msg ("chargen-race-title", ucfirst $pl->title, -1);
360 my $msg = $ob->msg;
361 $msg =~ s/(?<=\S)\n(?=\S)/ /g;
362 $ns->send_msg ("chargen-race-description", $msg, cf::NDI_BLUE);
363
364 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
365 "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n";
366
367 last if $res =~ /[dD]/;
368
369 $pl->chargen_race_next;
370 Coro::Timer::sleep 0.05;
371 }
372
373 # create the playerdir, if necessary, as chargen_race_done did it before
374 # presumably because of unique maps
375 aio_mkdir playerdir $pl, 0770;
376 $pl->chargen_race_done;
377
378 while () {
379 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
380 "Now choose a gender.\nPress 'f' to become female, and 'm' to become male.\n";
381
382 if ($res =~ /^[fF]/) {
383 $pl->gender (1);
384 last;
385 } elsif ($res =~ /^[mM]/) {
386 $pl->gender (0);
387 last;
388 }
389 Coro::Timer::sleep 0.05;
390 }
391
392 $ob->reply (undef, "Welcome to Deliantra!");
393
394 # XXX: Workaround for delayed client ext protocol handshake
395 $pl->esrv_new_player;
396
397 delete $pl->{deny_save};
398
399 last; 397 last;
400 } 398 }
401 399
402 if (0 < Coro::AIO::aio_load "$cf::CONFDIR/motd", my $motd) { 400 # lock again, too layz to make this nicer
403 $ns->send_msg ("c/motd" => $motd, cf::NDI_CLEAR); 401 local $cf::LOGIN_LOCK{$user} = 1;
404 } 402
403 chargen $ns, $user, $pass;
404 login_done $ns->pl;
405 }); 405 });
406}); 406});
407 407
408cf::client->attach (
409 on_version => sub {
410 my ($ns, $arg) = @_;
411
412 srand 1;#d#
413 $ns->{nonces} = [map { join "", map { chr rand 256 } 0..63 } 1..2];
414 $ns->ext_msg (nonces => @{ $ns->{nonces} });
415 },
416);
417
418#cf::register_async_exticmd create_login => sub {
419# my ($ns, $reply, $user, $pass) = @_;
420#
421# $ns->{addme}++ and return $ns->destroy;
422#};
423
424cf::register_async_exticmd login => sub {
425 my ($ns, $reply, $user, $hash) = @_;
426
427 $ns->{addme}++ and return $ns->destroy;
428
429 $ns->async (sub {
430 local $cf::LOGIN_LOCK{$user} = 1;
431
432 $Coro::current->{desc} = "login($user) check";
433
434 my $fail = sub {
435 $reply->(0, $_[0]);
436 $ns->flush; # does not ensure that the data reaches the client - TODO
437 # need to do this in another thread, as this one gets canceled
438 Coro::async_pool {
439 Coro::AnyEvent::sleep 0.1; # TODO, see above, extra hack
440 $ns->destroy;
441 };
442 Coro::schedule; # do the destroy, should not return
443 };
444
445 # try to read the user file and check the password
446 my $pl = cf::player::find $user
447 or return $fail->("User '$user' does not exist - wrong spelling?");
448
449 aio_stat $pl->path
450 and return $ns->destroy;
451
452 my $mtime = (stat _)[9];
453 my $token = $pl->password;
454
455 $token = $token =~ /^!/
456 ? Deliantra::Util::hash_pw pack "H*", substr $token, 1
457 : pack "H*", $token;
458
459 $token = Deliantra::Util::auth_pw $token, $ns->{nonces}[0], $ns->{nonces}[1];
460
461 $token eq $hash
462 or $cf::CFG{ext_login_nocheck}
463 or return $fail->("User exists, but the password doesn't match - check your spelling, NumLock/CapsLock etc.");
464
465 # player exists and passwords match - we can proceed
466
467 $pl->connect ($ns);
468 enter_map $pl;
469 login_done $pl;
470
471 $reply->(1, "Success");
472 });
473};
474
408cf::register_command password => sub { 475cf::register_command password => sub {
409 my ($pl, $arg) = @_; 476 my ($pl, $arg) = @_;
410 477
478 unless ($pl->flag (cf::FLAG_WIZ)) {
479 $pl->message (
480 "The password can currently only changed by a DM.",
481 cf::NDI_UNIQUE | cf::NDI_REPLY);
482 return;
483 }
484
411 my (@args) = split /\s+/, $arg; 485 my (@args) = split /\s+/, $arg;
412
413 my ($new_pw, $player);
414
415 if ($pl->flag (cf::FLAG_WIZ)) {
416 ($player, $new_pw) = @args; 486 my ($player, $new_pw) = @args;
417 } else {
418 $new_pw = $args[0];
419 }
420 487
421 if ($pl->flag (cf::FLAG_WIZ) && $player eq '') { 488 if ($pl->flag (cf::FLAG_WIZ) && $player eq '') {
422 $pl->message ( 489 $pl->message (
423 "Usage: password <player> [<new password>]", 490 "Usage: password <player> [<new password>]",
424 cf::NDI_UNIQUE | cf::NDI_REPLY); 491 cf::NDI_UNIQUE | cf::NDI_REPLY);
425 return; 492 return;
426 } elsif (!$pl->flag (cf::FLAG_WIZ) && $new_pw eq '') {
427 $pl->message (
428 "Usage: password <new password>",
429 cf::NDI_UNIQUE | cf::NDI_REPLY);
430 return;
431 } 493 }
432 494
433 if ($player ne '' && $pl->flag (cf::FLAG_WIZ)) {
434 unless ($new_pw ne '') { 495 if ($new_pw eq '') {
435 $new_pw = 496 $new_pw =
436 join '', 497 join '',
437 map { ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[(cf::rndm 64)] } 498 map { ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[(cf::rndm 64)] }
438 1..9; 499 1..9;
439 } 500 }
440 501
441 cf::async { 502 cf::async {
442 my $plc = cf::player::find $player; 503 my $plc = cf::player::find $player;
443 if ($plc) { 504 if ($plc) {
444 $plc->password (encode_password $new_pw); 505 $plc->password (encode_password $new_pw);
445 $pl->message (
446 "Ok, changed password of '$player' to '$new_pw'!",
447 cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY);
448 } else {
449 $pl->message (
450 "Fail! Couldn't set password for '$player', "
451 . "he doesn't seem to exist!",
452 cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY);
453 }
454 };
455 } else {
456 my $change = delete $pl->{password_change};
457
458 if ($change && (time - $change->[0]) < 60) {
459 $pl->message ( 506 $pl->message (
460 "Ok, changed your password!", 507 "Ok, changed password of '$player' to '$new_pw'!",
461 cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); 508 cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY);
462 $pl->contr->password (encode_password $new_pw);
463
464 } else { 509 } else {
465 $pl->message ( 510 $pl->message (
466 "Ok, please confirm your new password by sending " 511 "Fail! Couldn't set password for '$player', "
467 . "the command again within one minute!", 512 . "he doesn't seem to exist!",
468 cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); 513 cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY);
469 $pl->{password_change} = [time, $new_pw];
470 } 514 }
471 } 515 };
472}; 516};
473 517
474cf::register_command quit => sub { 518cf::register_command quit => sub {
475 my ($ob, $arg) = @_; 519 my ($ob, $arg) = @_;
476 520
488 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to PERMANENTLY delete your character and all associated data (y/n)?", sub { 532 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to PERMANENTLY delete your character and all associated data (y/n)?", sub {
489 if ($_[0] !~ /^[yY]/) { 533 if ($_[0] !~ /^[yY]/) {
490 $ob->send_msg (undef, "Ok, not not quitting then.", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); 534 $ob->send_msg (undef, "Ok, not not quitting then.", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY);
491 } else { 535 } else {
492 $ob->send_msg (undef, "Ok, quitting, hope to see you again.", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); 536 $ob->send_msg (undef, "Ok, quitting, hope to see you again.", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY);
493 $pl->ns->flush;
494 cf::async { 537 cf::async {
495 ext::highscore::check $pl->ob;
496 $pl->quit_character; 538 $pl->quit_character;
497 }; 539 };
498 } 540 }
499 }); 541 });
500}; 542};
508 550
509 my $pl = $ob->contr; 551 my $pl = $ob->contr;
510 552
511 # update respawn position 553 # update respawn position
512 $pl->savebed ($bed->map->path, $bed->x, $bed->y); 554 $pl->savebed ($bed->map->path, $bed->x, $bed->y);
513 cf::async { $pl->save };
514 555
556 cf::async {
515 my $killer = cf::arch::get "killer_logout"; $pl->killer ($killer); $killer->destroy; 557 my $killer = cf::arch::get "killer_logout"; $pl->killer ($killer); $killer->destroy;
516 ext::highscore::check $ob; 558 ext::highscore::check $ob;
517 559
560 $pl->save;
561
518 $ob->send_msg ($cf::SAY_CHANNEL => "In the future, you will wake up here when you die.", cf::NDI_DEF | cf::NDI_REPLY); 562 $ob->send_msg ($cf::SAY_CHANNEL => "In the future, you will wake up here when you die.", cf::NDI_DEF | cf::NDI_REPLY);
519 563
564 my $ns = $pl->ns
565 or return;
566
520 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub { 567 $ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub {
521 if ($_[0] !~ /^[yY]/) { 568 if ($_[0] !~ /^[yY]/) {
522 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); 569 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1);
523 $pl->deactivate; 570 $pl->deactivate;
524 $pl->ns->destroy; 571 $pl->ns->destroy;
525 } else { 572 }
526 cf::async { $pl->save };
527 } 573 });
528 }); 574 };
529 }, 575 },
530); 576);
531 577
532cf::player->attach ( 578cf::player->attach (
533 on_login => sub { 579 on_login => sub {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines