Skip to content

Instantly share code, notes, and snippets.

@sasairc
Last active October 18, 2018 13:13
Show Gist options
  • Save sasairc/b21b3d2f92b728b7a292ecb1c137ee54 to your computer and use it in GitHub Desktop.
Save sasairc/b21b3d2f92b728b7a292ecb1c137ee54 to your computer and use it in GitHub Desktop.

sonya => 殺し屋

yasuna => おバカ

agiri => 忍者

botsu => (何書いたっけ)

/*
* 古典的 login シミュレータ
*
* % gcc kmb.c -lbenly_typestring -o kmb
*/
#include <benly/typestring.h>
#include <stdio.h>
#include <string.h>
#include <pwd.h>
#define SALT "kmb"
#define PWDDB "./passwd"
static struct passwd*
getpwnamin(const char* user)
{
struct passwd* pwd = NULL;
FILE* fp = fopen(PWDDB, "r");
while ((pwd = fgetpwent(fp)) != NULL) {
if (strcmp(user, pwd->pw_name) == 0)
break;
}
fclose(fp);
return pwd;
}
static
int login(STRING** s)
{
struct passwd* pwd = NULL;
fprintf(stdout, "login: ");
fgetline(stdin, s);
(*s)->chomp(s);
if ((pwd = getpwnamin((*s)->c_str(*s))) == NULL)
return 0;
fprintf(stdout, "password: ");
fgetline(stdin, s);
(*s)->chomp(s);
(*s)->assign(s, (*s)->crypt(*s, SALT));
if (strcmp(pwd->pw_passwd, (*s)->c_str(*s)) == 0)
return 1;
fprintf(stdout, "incorrect\n");
return 0;
}
int main(void)
{
STRING* s = new_string(NULL);
while (!login(&s));
fprintf(stdout, "ナイスですねーナイスですねー\n");
s->release(s);
return 0;
}
sonya:km1hQI9EQaw0I:1000:1000:sonya:/home/sonya:/bin/sh
yasuna:kmboYiYiO3otU:2000:2000:yasuna:/home/yasuna:/bin/sh
agiri:kms1/t.jJ6WSw:3000:3000:sonya:/home/agiri:/bin/sh
botsu:kmIKigOujGXF6:65535:65535:botsu:/nonexistent:/bin/false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment