아이디와 패스워드가 동일한 계정 찾는 코드인데요.. 성능면에서

superkkt의 이미지

안녕하세요..

아이디와 패스워드가 동일한 계정을 찾는 코드를 만들어 봤습니다. 일단 정상 작동은 하는것 같은데요.. 혹시 잘못된 엔트리가 있어서 passwd와 shadow의 순서가 일치하지 않을 경우를 대비해서 chk_shadow함수의 처음에 rewind를 써서 파일의 처음부터 다시 검색을 하도록 했습니다.

계정이 얼마 없는 서버에서는 괜찮은데 좀 많은데서 테스트 해보니 좀 느리네요.. 성능개선을 좀 해볼라고 shadow파일 전체를 2차원 배열에 넣고 해볼까 했는데.. 쩝..초보라서 구현을 못하겠네요..

성능개선을 위한 힌트좀 부탁드립니다~

/*
*	chk_shadow v0.1		2005/10/12
*	
*	아이디와 패스워드가 동일한 계정을 찾아서 보여준다.
*
*/

#include <stdio.h>
#include <pwd.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

void chk_shadow(FILE *fp, struct passwd *p);
void err_print(char *err_string);

int 
main(void)
{
	struct passwd *p;
	FILE *fp;

	if((fp = fopen("/etc/shadow", "r")) == NULL)
		err_print("/etc/shadow open failed ");
		
	while((p = getpwent()) != NULL) {
		chk_shadow(fp, p);	
	}

	endpwent();
	fclose(fp);
	
	return 0;	
}


void
chk_shadow(FILE *fp, struct passwd *p)
{
	char shadow[LINE_MAX];
	char *id;
	char *pw;
	char salt[13];
		
	rewind(fp);	/* 파일의 처음으로 이동 */
	while(fgets(shadow, LINE_MAX, fp) != NULL) {
		if((id = strtok(shadow, ":")) == NULL)
			err_print("shadow file ID entry parsing error ");
		if((pw = strtok(NULL, ":")) == NULL)
			err_print("shadow file PW entry parsing error ");
		
		if(strcmp(id, p->pw_name) != 0) continue;	/* 찾는 아이디가 아니면 다음줄로 이동 */
	
		/* md5 구현 (리눅스) */
		if(pw[0] == '$') {				/* !!인 경우는 pw[2], pw[11]이 잘못된 포인터라서 */
			if(pw[2] == '$' && pw[11] == '$') {	/* 1, 3, 12번째가 $로 나오면 MD5를 사용한 암호화 */
				strncpy(salt, pw, 12);		/* MD5의 경우에는 앞 12글자가 salt 값이다	*/
				salt[12] = '\0';
			}
		}
		/* 일반 crypt 구현 (솔라리스) */
		else {
			strncpy(salt, pw, 2);			/* 일반 crypt의 경우에는 앞 2글자만 salt 값이다 */
			salt[2] = '\0';
		}
			
		if(strcmp(pw, crypt(p->pw_name, salt)) == 0) 
			printf("%s (%s) account detected\n", p->pw_name, p->pw_dir);		

	}
}


void 
err_print(char *err_string)
{
	perror(err_string);
	exit(EXIT_FAILURE);	
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.