pam_examples
blank.c
/*
* $Id$
*/ /* Andrew Morgan (morgan@parc.power.net) -- a self contained `blank'
* application
*
* I am not very proud of this code. It makes use of a possibly ill-
* defined pamh pointer to call pam_strerror() with. The reason that
* I was sloppy with this is historical (pam_strerror, prior to 0.59,
* did not require a pamh argument) and if this program is used as a
* model for anything, I should wish that you will take this error into
* account.
*/ #include <stdio.h>
#include <stdlib.h> #include <security/pam_appl.h>
#include <security/pam_misc.h> /* ------ some local (static) functions ------- */ static void bail_out(pam_handle_t *pamh, int really, int code, const char *fn)
{
fprintf(stderr,"==> called %s()\n got: `%s'\n", fn,
pam_strerror(pamh, code));
if (really && code)
exit ();
} /* ------ some static data objects ------- */ static struct pam_conv conv = {
misc_conv,
NULL
}; /* ------- the application itself -------- */ int main(int argc, char **argv)
{
pam_handle_t *pamh=NULL;
char *username=NULL;
int retcode; /* did the user call with a username as an argument ? */ if (argc > ) {
fprintf(stderr,"usage: %s [username]\n",argv[]);
} else if (argc == ) {
username = argv[];
} /* initialize the Linux-PAM library */
retcode = pam_start("blank", username, &conv, &pamh);
bail_out(pamh,,retcode,"pam_start"); /* test the environment stuff */
{
#define MAXENV 15
const char *greek[MAXENV] = {
"a=alpha", "b=beta", "c=gamma", "d=delta", "e=epsilon",
"f=phi", "g=psi", "h=eta", "i=iota", "j=mu", "k=nu",
"l=zeta", "h=", "d", "k=xi"
};
char **env;
int i; for (i=; i<MAXENV; ++i) {
retcode = pam_putenv(pamh,greek[i]);
bail_out(pamh,,retcode,"pam_putenv");
}
env = pam_getenvlist(pamh);
if (env)
env = pam_misc_drop_env(env);
else
fprintf(stderr,"???\n");
fprintf(stderr,"a test: c=[%s], j=[%s]\n"
, pam_getenv(pamh, "c"), pam_getenv(pamh, "j"));
} /* to avoid using goto we abuse a loop here */
for (;;) {
/* authenticate the user --- `0' here, could have been PAM_SILENT
* | PAM_DISALLOW_NULL_AUTHTOK */ retcode = pam_authenticate(pamh, );
bail_out(pamh,,retcode,"pam_authenticate"); /* has the user proved themself valid? */
if (retcode != PAM_SUCCESS) {
fprintf(stderr,"%s: invalid request\n",argv[]);
break;
} /* the user is valid, but should they have access at this
time? */ retcode = pam_acct_mgmt(pamh, ); /* `0' could be as above */
bail_out(pamh,,retcode,"pam_acct_mgmt"); if (retcode == PAM_NEW_AUTHTOK_REQD) {
fprintf(stderr,"Application must request new password...\n");
retcode = pam_chauthtok(pamh,PAM_CHANGE_EXPIRED_AUTHTOK);
bail_out(pamh,,retcode,"pam_chauthtok");
} if (retcode != PAM_SUCCESS) {
fprintf(stderr,"%s: invalid request\n",argv[]);
break;
} /* `0' could be as above */
retcode = pam_setcred(pamh, PAM_ESTABLISH_CRED);
bail_out(pamh,,retcode,"pam_setcred1"); if (retcode != PAM_SUCCESS) {
fprintf(stderr,"%s: problem setting user credentials\n"
,argv[]);
break;
} /* open a session for the user --- `0' could be PAM_SILENT */
retcode = pam_open_session(pamh,);
bail_out(pamh,,retcode,"pam_open_session");
if (retcode != PAM_SUCCESS) {
fprintf(stderr,"%s: problem opening a session\n",argv[]);
break;
} fprintf(stderr,"The user has been authenticated and `logged in'\n"); /* close a session for the user --- `0' could be PAM_SILENT
* it is possible that this pam_close_call is in another program..
*/ retcode = pam_close_session(pamh,);
bail_out(pamh,,retcode,"pam_close_session");
if (retcode != PAM_SUCCESS) {
fprintf(stderr,"%s: problem closing a session\n",argv[]);
break;
} retcode = pam_setcred(pamh, PAM_DELETE_CRED);
bail_out(pamh,,retcode,"pam_setcred2"); break; /* don't go on for ever! */
} /* close the Linux-PAM library */
retcode = pam_end(pamh, PAM_SUCCESS);
pamh = NULL; bail_out(pamh,,retcode,"pam_end"); exit();
}
check_user.c
/*
$Id$ This program was contributed by Shane Watts <shane@icarus.bofh.asn.au>
slight modifications by AGM. You need to add the following (or equivalent) to the /etc/pam.conf file.
# check authorization
check auth required pam_unix_auth.so
check account required pam_unix_acct.so
*/ #include <security/pam_appl.h>
#include <security/pam_misc.h>
#include <stdio.h> static struct pam_conv conv = {
misc_conv,
NULL
}; int main(int argc, char *argv[])
{
pam_handle_t *pamh=NULL;
int retval;
const char *user="nobody"; if(argc == ) {
user = argv[];
} if(argc > ) {
fprintf(stderr, "Usage: check_user [username]\n");
exit();
} retval = pam_start("check", user, &conv, &pamh); if (retval == PAM_SUCCESS)
retval = pam_authenticate(pamh, ); /* is user really user? */ if (retval == PAM_SUCCESS)
retval = pam_acct_mgmt(pamh, ); /* permitted access? */ /* This is where we have been authorized or not. */ if (retval == PAM_SUCCESS) {
fprintf(stdout, "Authenticated\n");
} else {
fprintf(stdout, "Not Authenticated\n");
} if (pam_end(pamh,retval) != PAM_SUCCESS) { /* close Linux-PAM */
pamh = NULL;
fprintf(stderr, "check_user: failed to release authenticator\n");
exit();
} return ( retval == PAM_SUCCESS ? : ); /* indicate success */
}
vpass.c
#include "config.h" #include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>
#include <security/pam_appl.h> static int
test_conv (int num_msg UNUSED, const struct pam_message **msgm UNUSED,
struct pam_response **response UNUSED, void *appdata_ptr UNUSED)
{
return ;
} static struct pam_conv conv = {
test_conv,
NULL
}; int main(void)
{
char *user;
pam_handle_t *pamh;
struct passwd *pw;
uid_t uid;
int res; uid = geteuid();
pw = getpwuid(uid);
if (pw) {
user = pw->pw_name;
} else {
fprintf(stderr, "Invalid userid: %lu\n", (unsigned long) uid);
exit();
} pam_start("vpass", user, &conv, &pamh);
pam_set_item(pamh, PAM_TTY, "/dev/tty");
if ((res = pam_authenticate(pamh, )) != PAM_SUCCESS) {
fprintf(stderr, "Oops: %s\n", pam_strerror(pamh, res));
exit();
} pam_end(pamh, res);
exit();
}
xsh.c
/* Andrew Morgan (morgan@kernel.org) -- an example application
* that invokes a shell, based on blank.c */ #include "config.h" #include <stdio.h>
#include <stdlib.h> #include <security/pam_appl.h>
#include <security/pam_misc.h> #include <pwd.h>
#include <sys/types.h>
#include <unistd.h> /* ------ some local (static) functions ------- */ static void bail_out(pam_handle_t *pamh,int really, int code, const char *fn)
{
fprintf(stderr,"==> called %s()\n got: `%s'\n", fn,
pam_strerror(pamh,code));
if (really && code)
exit ();
} /* ------ some static data objects ------- */ static struct pam_conv conv = {
misc_conv,
NULL
}; /* ------- the application itself -------- */ int main(int argc, char **argv)
{
pam_handle_t *pamh=NULL;
const void *username=NULL;
const char *service="xsh";
int retcode; /* did the user call with a username as an argument ?
* did they also */ if (argc > ) {
fprintf(stderr,"usage: %s [username [service-name]]\n",argv[]);
}
if ((argc >= ) && (argv[][] != '-')) {
username = argv[];
}
if (argc == ) {
service = argv[];
} /* initialize the Linux-PAM library */
retcode = pam_start(service, username, &conv, &pamh);
bail_out(pamh,,retcode,"pam_start"); /* fill in the RUSER and RHOST etc. fields */
{
char buffer[];
struct passwd *pw;
const char *tty; pw = getpwuid(getuid());
if (pw != NULL) {
retcode = pam_set_item(pamh, PAM_RUSER, pw->pw_name);
bail_out(pamh,,retcode,"pam_set_item(PAM_RUSER)");
} retcode = gethostname(buffer, sizeof(buffer)-);
if (retcode) {
perror("failed to look up hostname");
retcode = pam_end(pamh, PAM_ABORT);
bail_out(pamh,,retcode,"pam_end");
}
retcode = pam_set_item(pamh, PAM_RHOST, buffer);
bail_out(pamh,,retcode,"pam_set_item(PAM_RHOST)"); tty = ttyname(fileno(stdin));
if (tty) {
retcode = pam_set_item(pamh, PAM_TTY, tty);
bail_out(pamh,,retcode,"pam_set_item(PAM_RHOST)");
}
} /* to avoid using goto we abuse a loop here */
for (;;) {
/* authenticate the user --- `0' here, could have been PAM_SILENT
* | PAM_DISALLOW_NULL_AUTHTOK */ retcode = pam_authenticate(pamh, );
bail_out(pamh,,retcode,"pam_authenticate"); /* has the user proved themself valid? */
if (retcode != PAM_SUCCESS) {
fprintf(stderr,"%s: invalid request\n",argv[]);
break;
} /* the user is valid, but should they have access at this
time? */ retcode = pam_acct_mgmt(pamh, ); /* `0' could be as above */
bail_out(pamh,,retcode,"pam_acct_mgmt"); if (retcode == PAM_NEW_AUTHTOK_REQD) {
fprintf(stderr,"Application must request new password...\n");
retcode = pam_chauthtok(pamh,PAM_CHANGE_EXPIRED_AUTHTOK);
bail_out(pamh,,retcode,"pam_chauthtok");
} if (retcode != PAM_SUCCESS) {
fprintf(stderr,"%s: invalid request\n",argv[]);
break;
} /* `0' could be as above */
retcode = pam_setcred(pamh, PAM_ESTABLISH_CRED);
bail_out(pamh,,retcode,"pam_setcred"); if (retcode != PAM_SUCCESS) {
fprintf(stderr,"%s: problem setting user credentials\n"
,argv[]);
break;
} /* open a session for the user --- `0' could be PAM_SILENT */
retcode = pam_open_session(pamh,);
bail_out(pamh,,retcode,"pam_open_session");
if (retcode != PAM_SUCCESS) {
fprintf(stderr,"%s: problem opening a session\n",argv[]);
break;
} pam_get_item(pamh, PAM_USER, &username);
fprintf(stderr,
"The user [%s] has been authenticated and `logged in'\n",
(const char *)username); /* this is always a really bad thing for security! */
retcode = system("/bin/sh"); /* close a session for the user --- `0' could be PAM_SILENT
* it is possible that this pam_close_call is in another program..
*/ retcode = pam_close_session(pamh,);
bail_out(pamh,,retcode,"pam_close_session");
if (retcode != PAM_SUCCESS) {
fprintf(stderr,"%s: problem closing a session\n",argv[]);
break;
} /* `0' could be as above */
retcode = pam_setcred(pamh, PAM_DELETE_CRED);
bail_out(pamh,,retcode,"pam_setcred");
if (retcode != PAM_SUCCESS) {
fprintf(stderr,"%s: problem deleting user credentials\n"
,argv[]);
break;
} break; /* don't go on for ever! */
} /* close the Linux-PAM library */
retcode = pam_end(pamh, PAM_SUCCESS);
pamh = NULL;
bail_out(pamh,,retcode,"pam_end"); return ();
}
pam_examples的更多相关文章
随机推荐
- CenOS 定时任务,at和crontab
1.一次性定时任务,只执行一次 语法:# at [参数] [时间] at> 执行的指令 退出at命令 ctrl+d 1.1 mini安装版本可能没有预装at 安装at yum -y instal ...
- 用Docker搭建Nexus私服
搜索Nexus 在docker容器中加载Nexus镜像 发布本地项目到Nexus私服 配置连接方式 发布指令 打源码包上传插件 搜索Nexus 在我们打算使用Nexus时,我们先搜索一下docke ...
- [bzoj] 3673 3674 可持久化并查集 || 可持久化数组
原题 加强版 题意: 可持久化并查集模板-- 题解: 用可持久化线段树维护一个可持久化数组,来记录每一次操作后的状态. 不能用路径压缩,但是要按置合并,使复杂度保证在O(log) #include&l ...
- [Leetcode] search in rotated sorted array 搜索旋转有序数组
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e.,0 1 2 4 5 6 7might ...
- AOJ.720 丢失的学妹
缺失的学妹 考察点 STL MAP Time Mem Len Lang 3.81s 39.1MB 0.68K G++ 题意分析 给出妹子学号的个数n,给出n个学号,和n-1个学号,求在n学号中那个没有 ...
- 去掉vue 中的代码规范检测(Eslint验证)
去掉vue 中的代码规范检测(Eslint验证): 1.在搭建vue脚手架时提示是否启用eslint检测的. Use ESLint to lint your code? 写 no; 2.如果项目已经生 ...
- win7下idea远程连接hadoop,运行wordCount
1.将hadoop-2.6.1.tar.gz解压到本地 配置环境变量 HADOOP_HOME E:\kaifa\hadoop-2.6.1\hadoop-2.6.1 HADOOP_BIN_PATH %H ...
- Linux系统通过AWS命令行上传文件至S3
打开你的AWS控制台: 在IAM中创建一个新用户(比如test),创建时它会自动创建一个用户安全凭证,是由“访问密钥ID”和“私有访问密钥”组成的,请记住它并下载该凭证,后面会用到它: 选择你刚创建的 ...
- pg_basebackup: invalid tar block header size
问题: 在使用pg_basebackup搭建备节点时,由于pg_basebackup本身使用的是int整型来保存传输的数据大小,当传输的数据大于4G的话,整数就会溢出,进而报出:pg_baseback ...
- squid总结
squid可以完成的工作: 代理服务器 反向代理服务器 防火墙 缓存功能 透明代理 squid和varnish的对比,以及squid的优缺点说明: 缓存到硬盘,容易遇到I/O瓶颈 V3.2以下不支持多 ...