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.109 by root, Tue May 4 21:45:42 2010 UTC vs.
Revision 1.124 by root, Sat Nov 17 06:41:16 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 {
126 } 121 }
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], 127 "!" . unpack "H*", $_[0]
133 join '',
134 ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[(cf::rndm 64), (cf::rndm 64)]
135} 128}
136 129
137# delete a player directory, be non-blocking AND synchronous... 130sub compare_password($$) {
138# (that's hard, so we crap out and fork). 131 my ($pass, $token) = @_;
132
133 if ($token =~ /!!(.*)/) {
134 return +(substr $pass, 0, 8) eq pack "H*", $1;
135 } elsif ($token =~ /!(.*)/) {
136 return $pass eq pack "H*", $1;
137 } else {
138 return $token eq crypt $pass, $token;
139 }
140}
141
142# delete a player directory
139sub nuke_playerdir { 143sub nuke_playerdir {
140 my ($user) = @_; 144 my ($user) = @_;
141 145
146 my $lock = cf::lock_acquire "ext::login::nuke_playerdir";
147
142 my $temp = "$PLAYERDIR/~$Coro::current~deleting~"; 148 my $temp = "$PLAYERDIR/~$Coro::current~deleting~";
143
144 cf::fork_call {
145 rename "$PLAYERDIR/$user", $temp; 149 aio_rename "$PLAYERDIR/$user", $temp;
146 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);
147 }; 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};
148} 243}
149 244
150cf::client->attach (on_addme => sub { 245cf::client->attach (on_addme => sub {
151 my ($ns) = @_; 246 my ($ns) = @_;
152 247
153 $ns->pl and return $ns->destroy; 248 $ns->{addme}++ and return $ns->destroy;
154 249
155 $ns->async (sub { 250 $ns->async (sub {
156 $Coro::current->{desc} = "addme init"; 251 $Coro::current->{desc} = "addme init";
157 252
158 my ($user, $pass); 253 my ($user, $pass);
230 325
231 # try to read the user file and check the password 326 # try to read the user file and check the password
232 if (my $pl = cf::player::find $user) { 327 if (my $pl = cf::player::find $user) {
233 aio_stat $pl->path and next; 328 aio_stat $pl->path and next;
234 my $mtime = (stat _)[9]; 329 my $mtime = (stat _)[9];
235 my $hash = $pl->password; 330 my $token = $pl->password;
236 331
237 if ($cf::CFG{ext_login_nocheck} or $hash eq crypt $pass, $hash) { 332 if ($cf::CFG{ext_login_nocheck} or compare_password $pass, $token) {
238 nuke_str $pass; 333 # player exists and passwords match - we can proceed
334
239 # password matches, wonderful 335 # password matches, wonderful
240 my $pl = cf::player::find $user or next; 336 my $pl = cf::player::find $user or next;
241 $pl->connect ($ns); 337 $pl->connect ($ns);
242 enter_map $pl; 338 enter_map $pl;
339 login_done $pl;
243 last; 340 return;
244 } elsif (can_cleanup $pl, $mtime) { 341 } elsif (can_cleanup $pl, $mtime) {
245 Coro::Timer::sleep 1; 342 Coro::Timer::sleep 1;
246 343
247 $ns->send_drawinfo ( 344 $ns->send_drawinfo (
248 "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, "
249 . "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 "
250 . "because it has not been in-use for some time.", 347 . "because it has not been in-use for some time.",
251 cf::NDI_RED 348 cf::NDI_RED
252 ); 349 );
253 350
254 #TODO: nuke_str
255 (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]/
256 or next; 352 or next;
257 353
258 # check if the file hasn't changed 354 # check if the file hasn't changed
259 aio_stat cf::player::path $user and next; 355 aio_stat cf::player::path $user and next;
261 357
262 $pl->quit_character; 358 $pl->quit_character;
263 359
264 # fall through to creation 360 # fall through to creation
265 } else { 361 } else {
266 nuke_str $pass;
267
268 Coro::Timer::sleep 1; 362 Coro::Timer::sleep 1;
269 363
270 $ns->send_drawinfo ( 364 $ns->send_drawinfo (
271 "Wrong username or password. Please try again " 365 "Wrong username or password. Please try again "
272 . "(check for Numlock and other semi-obvious error sources).", 366 . "(check for Numlock and other semi-obvious error sources).",
274 ); 368 );
275 next; 369 next;
276 } 370 }
277 } else { 371 } else {
278 # unable to load the playerfile: 372 # unable to load the playerfile:
279 # 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
280 # something very similar. 374 # something very similar.
281 if (!aio_stat cf::player::playerdir $user) { 375 if (!aio_stat cf::player::playerdir $user) {
282 $ns->send_drawinfo ( 376 $ns->send_drawinfo (
283 "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. "
284 . "If this is your account, ask a dungeon master for assistance. " 378 . "If this is your account, ask a dungeon master for assistance. "
287 ); 381 );
288 next; 382 next;
289 } 383 }
290 } 384 }
291 385
292 # the rest of this function is character creation
293 $Coro::current->{desc} = "addme($user) chargen";
294
295 # just to make sure nothing is left over
296 nuke_playerdir $user;
297
298 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.";
299 387
300 if ($pass2 ne $pass) { 388 if ($pass2 ne $pass) {
301 nuke_str $pass;
302 nuke_str $pass2;
303 $ns->send_drawinfo ( 389 $ns->send_drawinfo (
304 "The passwords do not match, please try again.", 390 "The passwords do not match, please try again.",
305 cf::NDI_RED 391 cf::NDI_RED
306 ); 392 );
307 Coro::Timer::sleep 0.5; 393 Coro::Timer::sleep 0.5;
308 next; 394 next;
309 } 395 }
310 396
311 nuke_str $pass2;
312
313 my $pl = cf::player::new $user;
314 $pl->password (encode_password $pass);
315 nuke_str $pass;
316 $pl->connect ($ns);
317 my $ob = $pl->ob;
318
319 $ob->goto ($pl->maplevel, $ob->x, $ob->y);
320
321 while () {
322 $ob->update_stats;
323 $pl->save_stats;
324
325 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
326 "[y] to roll new stats [n] to use stats\n[1-7] [1-7] to swap stats.\nRoll again (y/n/1-7)?";
327
328 if ($res =~ /^[Nn]/) {
329 last;
330 } elsif ($res > 0 && $res <= 7) {
331 my $swap = query $ns, cf::CS_QUERY_SINGLECHAR, "Swap stat with (will not roll new stats) [1-7]?";
332
333 if ($swap > 0 && $swap <= 7) {
334 $ob->swap_stats ($res - 1, $swap - 1);
335 }
336 } else {
337 $ob->roll_stats;
338 }
339
340 Coro::Timer::sleep 0.05;
341 }
342
343 $ob->set_animation (2);
344 $ob->add_statbonus;
345
346 while () {
347 $ns->send_msg ("chargen-race-title", ucfirst $pl->title, -1);
348 my $msg = $ob->msg;
349 $msg =~ s/(?<=\S)\n(?=\S)/ /g;
350 $ns->send_msg ("chargen-race-description", $msg, cf::NDI_BLUE);
351
352 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
353 "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n";
354
355 last if $res =~ /[dD]/;
356
357 $pl->chargen_race_next;
358 Coro::Timer::sleep 0.05;
359 }
360
361 # create the playerdir, if necessary, as chargen_race_done did it before
362 # presumably because of unique maps
363 aio_mkdir playerdir $pl, 0770;
364 $pl->chargen_race_done;
365
366 while () {
367 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
368 "Now choose a gender.\nPress 'f' to become female, and 'm' to become male.\n";
369
370 if ($res =~ /^[fF]/) {
371 $pl->gender (1);
372 last;
373 } elsif ($res =~ /^[mM]/) {
374 $pl->gender (0);
375 last;
376 }
377 Coro::Timer::sleep 0.05;
378 }
379
380 $ob->reply (undef, "Welcome to Deliantra!");
381
382 # XXX: Workaround for delayed client ext protocol handshake
383 $pl->esrv_new_player;
384
385 delete $pl->{deny_save};
386
387 last; 397 last;
388 } 398 }
389 399
390 if (0 < Coro::AIO::aio_load "$cf::CONFDIR/motd", my $motd) { 400 # lock again, too layz to make this nicer
391 $ns->send_msg ("c/motd" => $motd, cf::NDI_CLEAR); 401 local $cf::LOGIN_LOCK{$user} = 1;
392 } 402
403 chargen $ns, $user, $pass;
404 login_done $ns->pl;
393 }); 405 });
394}); 406});
395 407
408cf::client->attach (
409 on_version => sub {
410 my ($ns, $arg) = @_;
411
412 $ns->{nonces} = [map { join "", map { chr rand 256 } 0..63 } 1..2];
413 $ns->ext_msg (nonces => @{ $ns->{nonces} });
414 },
415);
416
417#cf::register_async_exticmd create_login => sub {
418# my ($ns, $reply, $user, $pass) = @_;
419#
420# $ns->{addme}++ and return $ns->destroy;
421#};
422
423cf::register_async_exticmd login => sub {
424 my ($ns, $reply, $user, $hash) = @_;
425
426 $ns->{addme}++ and return $ns->destroy;
427
428 $ns->async (sub {
429 local $cf::LOGIN_LOCK{$user} = 1;
430
431 $Coro::current->{desc} = "login($user) check";
432
433 my $fail = sub {
434 $reply->(0, $_[0]);
435 $ns->flush; # does not ensure that the data reaches the client - TODO
436 # need to do this in another thread, as this one gets canceled
437 Coro::async_pool {
438 Coro::AnyEvent::sleep 0.1; # TODO, see above, extra hack
439 $ns->destroy;
440 };
441 Coro::schedule; # do the destroy, should not return
442 };
443
444 # try to read the user file and check the password
445 my $pl = cf::player::find $user
446 or return $fail->("User '$user' does not exist - wrong spelling?");
447
448 aio_stat $pl->path
449 and return $ns->destroy;
450
451 my $mtime = (stat _)[9];
452 my $token = $pl->password;
453
454 $token = $token =~ /^!/
455 ? Deliantra::Util::hash_pw pack "H*", substr $token, 1
456 : pack "H*", $token;
457
458 $token = Deliantra::Util::auth_pw $token, $ns->{nonces}[0], $ns->{nonces}[1];
459
460 $token eq $hash
461 or $cf::CFG{ext_login_nocheck}
462 or return $fail->("User exists, but the password doesn't match - check your spelling, NumLock/CapsLock etc.");
463
464 # player exists and passwords match - we can proceed
465
466 $pl->connect ($ns);
467 enter_map $pl;
468 login_done $pl;
469
470 $reply->(1, "Success");
471 });
472};
473
396cf::register_command password => sub { 474cf::register_command password => sub {
397 my ($pl, $arg) = @_; 475 my ($pl, $arg) = @_;
398 476
477 unless ($pl->flag (cf::FLAG_WIZ)) {
478 $pl->message (
479 "The password can currently only changed by a DM.",
480 cf::NDI_UNIQUE | cf::NDI_REPLY);
481 return;
482 }
483
484 $pl->message (#d#
485 "Passwords cannot currently be changed.",#d#
486 cf::NDI_UNIQUE | cf::NDI_REPLY);#d#
487 return;#d#
488
399 my (@args) = split /\s+/, $arg; 489 my (@args) = split /\s+/, $arg;
400
401 my ($new_pw, $player);
402
403 if ($pl->flag (cf::FLAG_WIZ)) {
404 ($player, $new_pw) = @args; 490 my ($player, $new_pw) = @args;
405 } else {
406 $new_pw = $args[0];
407 }
408 491
409 if ($pl->flag (cf::FLAG_WIZ) && $player eq '') { 492 if ($pl->flag (cf::FLAG_WIZ) && $player eq '') {
410 $pl->message ( 493 $pl->message (
411 "Usage: password <player> [<new password>]", 494 "Usage: password <player> [<new password>]",
412 cf::NDI_UNIQUE | cf::NDI_REPLY); 495 cf::NDI_UNIQUE | cf::NDI_REPLY);
413 return; 496 return;
414 } elsif (!$pl->flag (cf::FLAG_WIZ) && $new_pw eq '') {
415 $pl->message (
416 "Usage: password <new password>",
417 cf::NDI_UNIQUE | cf::NDI_REPLY);
418 return;
419 } 497 }
420 498
421 if ($player ne '' && $pl->flag (cf::FLAG_WIZ)) {
422 unless ($new_pw ne '') { 499 if ($new_pw eq '') {
423 $new_pw = 500 $new_pw =
424 join '', 501 join '',
425 map { ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[(cf::rndm 64)] } 502 map { ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[(cf::rndm 64)] }
426 1..9; 503 1..9;
427 } 504 }
428 505
429 cf::async { 506 cf::async {
430 my $plc = cf::player::find $player; 507 my $plc = cf::player::find $player;
431 if ($plc) { 508 if ($plc) {
432 $plc->password (encode_password $new_pw); 509 $plc->password (encode_password $new_pw);
433 $pl->message (
434 "Ok, changed password of '$player' to '$new_pw'!",
435 cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY);
436 } else {
437 $pl->message (
438 "Fail! Couldn't set password for '$player', "
439 . "he doesn't seem to exist!",
440 cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY);
441 }
442 };
443 } else {
444 my $change = delete $pl->{password_change};
445
446 if ($change && (time - $change->[0]) < 60) {
447 $pl->message ( 510 $pl->message (
448 "Ok, changed your password!", 511 "Ok, changed password of '$player' to '$new_pw'!",
449 cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); 512 cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY);
450 $pl->contr->password (encode_password $new_pw);
451
452 } else { 513 } else {
453 $pl->message ( 514 $pl->message (
454 "Ok, please confirm your new password by sending " 515 "Fail! Couldn't set password for '$player', "
455 . "the command again within one minute!", 516 . "he doesn't seem to exist!",
456 cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); 517 cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY);
457 $pl->{password_change} = [time, $new_pw];
458 } 518 }
459 } 519 };
460}; 520};
461 521
462cf::register_command quit => sub { 522cf::register_command quit => sub {
463 my ($ob, $arg) = @_; 523 my ($ob, $arg) = @_;
464 524
476 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to PERMANENTLY delete your character and all associated data (y/n)?", sub { 536 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to PERMANENTLY delete your character and all associated data (y/n)?", sub {
477 if ($_[0] !~ /^[yY]/) { 537 if ($_[0] !~ /^[yY]/) {
478 $ob->send_msg (undef, "Ok, not not quitting then.", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); 538 $ob->send_msg (undef, "Ok, not not quitting then.", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY);
479 } else { 539 } else {
480 $ob->send_msg (undef, "Ok, quitting, hope to see you again.", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); 540 $ob->send_msg (undef, "Ok, quitting, hope to see you again.", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY);
481 $pl->ns->flush;
482 cf::async { 541 cf::async {
483 ext::highscore::check $pl->ob;
484 $pl->quit_character; 542 $pl->quit_character;
485 }; 543 };
486 } 544 }
487 }); 545 });
488}; 546};
496 554
497 my $pl = $ob->contr; 555 my $pl = $ob->contr;
498 556
499 # update respawn position 557 # update respawn position
500 $pl->savebed ($bed->map->path, $bed->x, $bed->y); 558 $pl->savebed ($bed->map->path, $bed->x, $bed->y);
501 cf::async { $pl->save };
502 559
560 cf::async {
503 my $killer = cf::arch::get "killer_logout"; $pl->killer ($killer); $killer->destroy; 561 my $killer = cf::arch::get "killer_logout"; $pl->killer ($killer); $killer->destroy;
504 ext::highscore::check $ob; 562 ext::highscore::check $ob;
505 563
564 $pl->save;
565
506 $ob->send_msg ($cf::SAY_CHANNEL => "In the future, you will wake up here when you die.", cf::NDI_DEF | cf::NDI_REPLY); 566 $ob->send_msg ($cf::SAY_CHANNEL => "In the future, you will wake up here when you die.", cf::NDI_DEF | cf::NDI_REPLY);
507 567
568 my $ns = $pl->ns
569 or return;
570
508 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub { 571 $ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub {
509 if ($_[0] !~ /^[yY]/) { 572 if ($_[0] !~ /^[yY]/) {
510 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); 573 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1);
511 $pl->deactivate; 574 $pl->deactivate;
512 $pl->ns->destroy; 575 $pl->ns->destroy;
513 } else { 576 }
514 cf::async { $pl->save };
515 } 577 });
516 }); 578 };
517 }, 579 },
518); 580);
519 581
520cf::player->attach ( 582cf::player->attach (
521 on_login => sub { 583 on_login => sub {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines