- 论坛徽章:
- 0
|
我前几天写的,用户和密码使用环境变量来传:
参考:http://www.linuxforum.net/books/upfaq/x541.htm#AEN557- #define _XOPEN_SOURCE
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <shadow.h>
- #include <unistd.h>
- #include <errno.h>
-
- int check_password (const char *user, const char *password)
- {
- struct spwd *user_data;
-
- if (strcmp (user, "root") == 0)
- {
- fprintf (stderr, "root not allowed\n");
- return 3;
- }
-
- errno = 0;
- user_data = getspnam (user);
- if (user_data == NULL)
- {
- fprintf (stderr, "No such user %s, or error %s\n", user, strerror (errno));
- sleep (2);
- return 1;
- }
-
- if (strcmp (crypt (password, user_data->sp_pwdp), user_data->sp_pwdp) != 0)
- {
- fprintf (stderr, "Auth user %s failed\n", user);
- sleep (2);
- return 2;
- }
-
- return 0;
- }
-
- int main (void)
- {
- char *user, *password;
-
- if ((user= getenv("USER")) == NULL) exit(2);
- if ((password= getenv("PASS")) == NULL) exit(3);
-
- exit (check_password(user, password));
- }
复制代码 |
|