46 extern int get_auth_token(FILE *fd,
char *user,
char *cred,
char **nextline);
67 PasswordAuth::PasswordAuth(
const char *u,
const char *
c,
const char *salt_format)
72 strncpy(User, u,
sizeof(User));
76 if (strncmp(c,
"{CRYPT}",
STRLENOF(
"{CRYPT}")) == 0) {
79 if (strlen(tmp) >=
sizeof(Cred)) {
80 silent_cerr(
"unable to handle credentials (too long)"
89 silent_cerr(
"crypt() failed" << std::endl);
94 strncpy(Cred, tmp,
sizeof(Cred));
99 PasswordAuth::Auth(
const char *user,
const char *cred)
const
101 if (user == NULL || cred == NULL) {
105 char *tmp = crypt(cred, Cred);
110 if (strcmp(User, user) == 0 && strcmp(Cred, tmp) == 0) {
118 PasswordAuth::Auth(
int sock)
const
133 #include <security/pam_appl.h>
134 #ifdef HAVE_PAM_MISC_H
135 #include <pam_misc.h>
136 #elif HAVE_SECURITY_PAM_MISC_H
137 #include <security/pam_misc.h>
142 #define INPUTSIZE PAM_MAX_MSG_SIZE
143 #define CONV_ECHO_ON 1
144 #define CONV_ECHO_OFF 0
147 pam_misc_conv_delete_binary(
void **delete_me)
149 if (delete_me && *delete_me) {
150 unsigned char *packet = *(
unsigned char **)delete_me;
153 length = 4+(packet[0]<<24)+(packet[1]<<16)+(packet[2]<<8)+packet[3];
154 memset(packet, 0, length);
156 *delete_me = packet = NULL;
160 int (*mb_pam_bh_fn)(
const void *send,
void **receive) = NULL;
161 void (*mb_pam_bh_free)(
void **packet_p) = pam_misc_conv_delete_binary;
168 mbdyn_conv(
int num_msg,
const struct pam_message **msgm,
169 struct pam_response **response,
void *appdata_ptr)
172 struct pam_response *reply;
178 reply = (
struct pam_response *) calloc(num_msg,
sizeof(
struct pam_response));
183 for (count = 0; count < num_msg; ++
count) {
186 switch (msgm[count]->msg_style) {
187 case PAM_PROMPT_ECHO_OFF:
188 case PAM_PROMPT_ECHO_ON:
189 string = (
char *)x_strdup((
char *)appdata_ptr);
190 if (
string == NULL) {
191 goto failed_conversation;
196 if (fprintf(stderr,
"%s\n", msgm[count]->
msg) < 0) {
197 goto failed_conversation;
203 if (fprintf(stdout,
"%s\n", msgm[count]->
msg) < 0) {
204 goto failed_conversation;
209 case PAM_BINARY_PROMPT: {
210 void *pack_out = NULL;
211 const void *pack_in = msgm[
count]->msg;
214 || mb_pam_bh_fn(pack_in, &pack_out) != PAM_SUCCESS
215 || pack_out == NULL) {
216 goto failed_conversation;
218 string = (
char *) pack_out;
223 case PAM_BINARY_MSG: {
224 const void *pack_in = msgm[
count]->msg;
225 if (!pam_binary_handler_fn
226 || pam_binary_handler_fn(pack_in, NULL) != PAM_SUCCESS) {
227 goto failed_conversation;
234 fprintf(stderr,
"erroneous conversation (%d)\n",
235 msgm[count]->msg_style);
236 goto failed_conversation;
241 reply[
count].resp_retcode = 0;
242 reply[
count].resp = string;
254 failed_conversation:;
256 for (count = 0; count < num_msg; ++
count) {
257 if (reply[count].resp == NULL) {
261 switch (msgm[count]->msg_style) {
262 case PAM_PROMPT_ECHO_ON:
263 case PAM_PROMPT_ECHO_OFF:
264 _pam_overwrite(reply[count].resp);
265 free(reply[count].resp);
268 case PAM_BINARY_PROMPT:
269 mb_pam_bh_free((
void **) &reply[count].resp);
278 free(reply[count].resp);
281 reply[
count].resp = NULL;
292 PAM_Auth::PAM_Auth(
const char *u)
296 struct passwd* pw = getpwuid(getuid());
299 silent_cerr(
"PAM_Auth: cannot determine the effective user!" << std::endl);
308 struct pam_conv conv;
309 conv.conv = mbdyn_conv;
310 conv.appdata_ptr = NULL;
312 pam_handle_t *pamh = NULL;
313 int retval = pam_start(
"mbdyn", User, &conv, &pamh);
315 if (retval != PAM_SUCCESS) {
316 silent_cerr(
"PAM_Auth: user \"" << User
317 <<
"\" cannot be authenticated " << std::endl);
319 if (pam_end(pamh, retval) != PAM_SUCCESS) {
320 silent_cerr(
"PAM_Auth: unable to release PAM authenticator" << std::endl);
326 if (pam_end(pamh, retval) != PAM_SUCCESS) {
327 silent_cerr(
"PAM_Auth: unable to release PAM authenticator" << std::endl);
332 PAM_Auth::Auth(
const char *user,
const char *cred)
const
334 pam_handle_t *pamh = NULL;
339 if (user == NULL || cred == NULL) {
343 if (strcmp(User, user) != 0) {
344 silent_cerr(
"PAM_Auth::Auth: user \"" << user
345 <<
"\" cannot be authenticated " << std::endl);
349 struct pam_conv conv;
350 conv.conv = mbdyn_conv;
351 conv.appdata_ptr = (
void*)cred;
352 retval = pam_start(
"mbdyn", User, &conv, &pamh);
353 if (retval == PAM_SUCCESS) {
354 retval = pam_authenticate(pamh, 0);
355 if (retval == PAM_SUCCESS) {
364 if (pam_end(pamh, retval) != PAM_SUCCESS) {
365 silent_cerr(
"PAM_Auth::Auth: unable to release PAM authenticator" << std::endl);
372 PAM_Auth::Auth(
int sock)
const
383 #if defined(HAVE_SASL_SASL_H)
384 #include <sasl/sasl.h>
385 #elif defined(HAVE_SASL_H)
391 mbdyn_sasl_log(
void *context,
int level,
const char *message)
396 std::cerr <<
"[mbdyn " << level <<
"] " << message << std::endl;
400 silent_cerr(
"[mbdyn " << level <<
"] " << message << std::endl);
404 std::cout <<
"[mbdyn " << level <<
"] " << message << std::endl;
408 silent_cout(
"[mbdyn " << level <<
"] " << message << std::endl);
415 SASL2_Auth::SASL2_Auth(
const mbdyn_sasl_t *ms)
419 log_server_f = mbdyn_sasl_log;
421 if (mbdyn_sasl_init(&mbdyn_sasl) != SASL_OK) {
427 SASL2_Auth::Auth(
const char *user,
const char *cred)
const
433 SASL2_Auth::Auth(
int sock)
const
435 switch (mbdyn_sasl_auth(sock, NULL, &mbdyn_sasl)) {
455 const char* sKeyWords[] = {
482 switch (CurrKeyWord) {
492 silent_cerr(
"ReadAuthMethod: user expected at line "
498 if (strlen(tmp) == 0) {
499 silent_cerr(
"ReadAuthMethod: Need a legal user id at line "
504 std::string user(tmp);
507 silent_cerr(
"ReadAuthMethod: credentials expected at line "
513 tmp = getpass(
"password: ");
518 if (tmp[0] ==
'\0') {
519 silent_cout(
"ReadAuthMethod: null credentials at line "
523 std::string cred(tmp);
524 memset((
char *)tmp,
'\0', strlen(tmp));
526 std::string salt_format;
534 PasswordAuth(user.c_str(), cred.c_str(), salt_format.c_str()));
535 memset(cred.c_str(),
'\0', cred.size());
539 silent_cerr(
"ReadAuthMethod: line " << HP.
GetLineData()
540 <<
": no working crypt(3)" << std::endl);
550 if (strlen(tmp) == 0) {
551 silent_cerr(
"ReadAuthMethod: Need a legal user id at line "
562 silent_cerr(
"ReadAuthMethod: line " << HP.
GetLineData()
563 <<
": no PAM support" << std::endl);
570 mbdyn_sasl_t mbdyn_sasl = MBDYN_SASL_INIT;
571 mbdyn_sasl.use_sasl = MBDYN_SASL_SERVER;
578 silent_cerr(
"ReadAuthMethod: unable to get SASL mech at line "
586 silent_cerr(
"ReadAuthMethod: line " << HP.
GetLineData()
587 <<
": no SASL2 support" << std::endl);
593 silent_cerr(
"ReadAuthMethod: PWDB not implemented yet" << std::endl);
AuthMethod::AuthRes Auth(const char *, const char *) const
#define MBDYN_EXCEPT_ARGS
int get_auth_token(FILE *fd, char *user, char *cred, char **nextline)
#define SAFENEW(pnt, item)
virtual bool IsKeyWord(const char *sKeyWord)
virtual const char * GetStringWithDelims(enum Delims Del=DEFAULTDELIM, bool escape=true)
#define ASSERT(expression)
#define SAFENEWWITHCONSTRUCTOR(pnt, item, constructor)
static std::stack< cleanup * > c
char * mbdyn_make_salt(char *salt, size_t saltlen, const char *salt_format)
virtual int GetWord(void)
#define SAFESTRDUP(pnt, src)
AuthMethod * ReadAuthMethod(const DataManager *, MBDynParser &HP)
virtual HighParser::ErrOut GetLineData(void) const