aboutsummaryrefslogtreecommitdiff
path: root/pam_exec.c
blob: a11db96ca66924949fb6d14799a3e619beb60b23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* pam_exec module */

/*
 * Written by Guido Guenther <agx@sigxcpu.org>
 * based on pam_warn by Andrew Morgan <morgan@linux.kernel.org>
 */

#define _BSD_SOURCE

#include <stdio.h>
#include <unistd.h>
#include <syslog.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>

/*
 * here, we make a definition for the externally accessible function
 * in this file (this definition is required for static a module
 * but strongly encouraged generally) it is used to instruct the
 * modules include file to define the function prototypes.
 */

#define PAM_SM_AUTH
#define PAM_SM_PASSWORD

#include <security/pam_modules.h>


static void _pam_log(int err, const char *format, ...)
{
    va_list args;

    va_start(args, format);
    openlog("PAM-exec", LOG_CONS|LOG_PID, LOG_AUTH);
    vsyslog(err, format, args);
    va_end(args);
    closelog();
}


static int append_arg(pam_handle_t* pamh, int item, char* name, char* (*pargv[]))
{
    const char* value;
    int i=0;
    int ret;

    if((ret=pam_get_item(pamh, item, (const void **) &value)) != PAM_SUCCESS ) {
	return ret;
    }
    if(value) {
	while((*pargv)[i] != NULL) // skip to end of array
	    i++;
	(*pargv)[i] = malloc(strlen(name)+strlen(value)+2);
	if((*pargv)[i] == NULL ) {
	    return PAM_BUF_ERR;
	}
	sprintf((*pargv)[i], "%s=%s", name, value);
    }
    return PAM_SUCCESS;
}


static int do_exec(pam_handle_t *pamh, const char *function, int argc, const char* argv[])
{
  int debug=0, ret=PAM_SUCCESS, i;
  char *args=NULL;
  #define ARGS_MAX 9
  char **pass_argv;
  pid_t child;

  pass_argv=calloc(ARGS_MAX, sizeof(char*));

  for (i = 0; i < argc; i++) {
    if (!strncmp (argv[i], "exec=", 5))
	pass_argv[0] = argv[i] + 5;
    if (!strncmp (argv[i], "args=", 5))
	args = argv[i] + 5;
    else if (!strcmp (argv[i], "debug"))
	debug=1;
    else
	syslog (LOG_ERR, "illegal option %s", argv[i]);
  }
  if(!pass_argv[0]) {
      _pam_log(LOG_ERR, "No executable given");
      return PAM_SYSTEM_ERR;
  } else {
      pass_argv[1] = function;
      if(args)
	  pass_argv[2] = args;
  }
  
  if((ret=append_arg(pamh, PAM_USER, "user", &pass_argv)) != PAM_SUCCESS) {
	  goto out;
  }
  if((ret=append_arg(pamh, PAM_SERVICE, "service", &pass_argv)) != PAM_SUCCESS) {
	  goto out;
  }
  if((ret=append_arg(pamh, PAM_TTY, "tty", &pass_argv)) != PAM_SUCCESS) {
	  goto out;
  }
#if 0 /* not good passing this on the commandline, we could put it in the environment */
  if((ret=append_arg(pamh, PAM_AUTHTOK, "passwd", &pass_argv)) != PAM_SUCCESS) {
	  goto out;
  }
#endif
  if((ret=append_arg(pamh, PAM_RUSER, "ruser", &pass_argv)) != PAM_SUCCESS) {
	  goto out;
  }
  if((ret=append_arg(pamh, PAM_RHOST, "rhost", &pass_argv)) != PAM_SUCCESS) {
	  goto out;
  }

  if(debug) {
      for(i=0; i < ARGS_MAX; i++) {
	 if(pass_argv[i] == NULL)
	     break;
	 _pam_log(LOG_DEBUG, "arg%d: %s", i, pass_argv[i]);
      }
  }
 
  if((child = fork()) == -1) {
	_pam_log(LOG_ERR, "Cannot fork: %s", strerror(errno));
	ret=PAM_SYSTEM_ERR;
	goto out;
  } else {
      if(!child) {
          if(execve(pass_argv[0], pass_argv, NULL) == -1)
	  _pam_log(LOG_ERR, "Cannot execve: %s", strerror(errno));
      	  exit(1);
      } else {
 	 int status;
	 if(debug)
             _pam_log(LOG_DEBUG, "Waiting for child: %d", child);
	 if(waitpid(child, &status, 0) != child) {
             _pam_log(LOG_ERR, "Waitpid problem: %s", strerror(errno));
	 } else {
             _pam_log(LOG_DEBUG, "Child returnd with %d", WEXITSTATUS(status));
	 }

      }
  }
out:
  if(!args)
      i=2; /* don't free argv[0] and function */
  else
      i=3; /* don't free argv[0], function and args */
  for(; i < ARGS_MAX; i++) {
     if(pass_argv[i] != NULL)
	 free(pass_argv[i]);
     else
	 break;
  }
  free(pass_argv);

  if(ret == PAM_SUCCESS)
     ret=PAM_IGNORE; 
  return ret;
}

/* --- authentication management functions (only) --- */

PAM_EXTERN
int pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc,
			const char **argv)
{
  return do_exec(pamh, __FUNCTION__, argc, argv);
}

PAM_EXTERN
int pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
  return do_exec(pamh, __FUNCTION__, argc, argv);
}

/* password updating functions */

PAM_EXTERN
int pam_sm_chauthtok(pam_handle_t *pamh,int flags,int argc,const char **argv)
{
  return do_exec(pamh, __FUNCTION__, argc, argv);
}

PAM_EXTERN int
pam_sm_acct_mgmt (pam_handle_t *pamh, int flags, int argc, const char **argv)
{
  return do_exec(pamh, __FUNCTION__, argc, argv);
}

PAM_EXTERN int
pam_sm_open_session (pam_handle_t *pamh, int flags, int argc,
                     const char **argv)
{
  return do_exec(pamh, __FUNCTION__, argc, argv);
}

PAM_EXTERN int
pam_sm_close_session (pam_handle_t *pamh, int flags, int argc,
		      const char **argv)
{
  return do_exec(pamh, __FUNCTION__, argc, argv);
}

#ifdef PAM_STATIC

/* static module data */

struct pam_module _pam_warn_modstruct = {
    "pam_exec",
    pam_sm_authenticate,
    pam_sm_setcred,
    pam_sm_acct_mgmt,
    pam_sm_open_session,
    pam_sm_close_session,
    pam_sm_chauthtok,
};

#endif

/* end of module definition */