صفحه نخست | سیستم وبلاگ | در مورد من | twitter
$ gcc login.c -lcrypt
کد زیر را کپی و در فایلی به نام login.c ذخیره و با دستور فوق کامپایل کنید.
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <pwd.h> #include <errno.h> #include <err.h> #include <shadow.h> int main(int argc, char *argv[]) { char uname[255]; // username char pass[255]; // array for copying password char *p, *ptr; int len; // uname length struct passwd *pwd; struct spwd *spwd; setbuf(stdout, NULL); while (1) { printf("username: "); // clear EOF in stdin, if exists! clearerr(stdin); p = fgets(uname, sizeof(uname), stdin); if (p == NULL || *p == '\0') { putchar('\n'); continue; } len = strlen(uname); if (uname[len - 1] == '\n') uname[len - 1] = '\0'; if (!strlen(uname)) { putchar('\n'); continue; } // clear EOF in stdin clearerr(stdin); p = getpass("password: "); if (p == NULL || *p == '\0') { continue; } ptr = strncpy(pass, p, strlen(p) + 1); if (ptr == NULL) { fprintf(stderr, "\nstrncpy error\n"); continue; } while(*p != '\0') *p++ = '\0'; errno = 0; pwd = getpwnam(uname); if (pwd == NULL) { if (errno) { perror(NULL); continue; } fprintf(stderr, "%s user not exists\n", uname); continue; } errno = 0; spwd = getspnam(uname); if (spwd == NULL) { if (errno) { perror(NULL); continue; } fprintf(stderr, "%s user not exists\n", uname); continue; } p = crypt(pass, spwd->sp_pwdp); if (strcmp(p, spwd->sp_pwdp) != 0) { printf("Incorrect username or password.\n"); continue; } exit(EXIT_SUCCESS); } }
کلیه حقوق برای دارندهی سایت محفوظ است.