<commit_before>// Copyright (c) 2015, Galaxy Authors. All Rights Reserved
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Author: <EMAIL>
// Usage: 
// Edit /etc/pam.d/sshd, add one more line:
//
// auth     sufficient     pam_galaxy.so [directory to store users' temporary passwords]
// e.g.
//   auth     sufficient     /root/galaxy_tools/pam_galaxy.so
//
//   edit /tmp/sunjunyi01.pwd, write 1234.
//   then, sunjunyi01 can login with password <PASSWORD>.(only once)
//
#include <syslog.h>
#include <sys/param.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <security/pam_modules.h>
#include <security/_pam_types.h>
#include <pwd.h>

#define SSH_PWD_PATH "/tmp/"
#define DOOR_DOG_GID 100000

int GetUser(pam_handle_t *pamh, const char **user); 
int GetPasswd(pam_handle_t *pamh, char **passwd); 

#ifdef __cplusplus
extern "C" {
#endif

PAM_EXTERN int
pam_sm_authenticate(pam_handle_t *pamh, int flags,
    int argc, const char *argv[])
{
    (void)flags;
    openlog("galaxy", LOG_CONS | LOG_PID, LOG_USER);
    const char* user;
    char* passwd;
    char file_name[1024];
    char pwd_set[1024];
    FILE* fh;
    const char* pwd_path = NULL;
    struct passwd user_info;
    struct passwd* user_result;
    char* user_info_buf;
    int user_buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
    int user_info_ret = 0;
    long user_group_id = 0;
    if (argc > 0) {
        pwd_path = argv[0];
    } else {
        pwd_path = SSH_PWD_PATH;
    }

    //fetch user
    int ret = GetUser(pamh, &user);
    if (ret != PAM_SUCCESS) {
        syslog(LOG_INFO, "failed to fetch user name");
        closelog();
        return PAM_AUTHINFO_UNAVAIL;
    }
    user_info_buf = malloc(user_buf_size);
    user_info_ret = getpwnam_r(user, &user_info, user_info_buf, user_buf_size, &user_result);
    if (user_info_ret == 0 && user_result != NULL) {
        syslog(LOG_INFO, "group id %d", user_info.pw_gid);
        user_group_id = (long)user_info.pw_gid;
    }
    free(user_info_buf);

    if (user_group_id != DOOR_DOG_GID) {
        syslog(LOG_INFO, "%s login failed, because its group is not doordog", user);
        return PAM_AUTHINFO_UNAVAIL;
    }
    syslog(LOG_INFO, "user try login to galaxy: %s", user);
    //fetch password
    ret = GetPasswd(pamh, &passwd);
    if (ret != PAM_SUCCESS) {
        syslog(LOG_INFO, "failed to fetch password");
        closelog();
        return PAM_AUTHINFO_UNAVAIL;
    }<commit_msg>more accurate error number<commit_after>// Copyright (c) 2015, Galaxy Authors. All Rights Reserved
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Author: <EMAIL>
// Usage: 
// Edit /etc/pam.d/sshd, add one more line:
//
// auth     sufficient     pam_galaxy.so [directory to store users' temporary passwords]
// e.g.
//   auth     sufficient     /root/galaxy_tools/pam_galaxy.so
//
//   edit /tmp/sunjunyi01.pwd, write <PASSWORD>.
//   then, sunjunyi01 can login with password <PASSWORD>.(only once)
//
#include <syslog.h>
#include <sys/param.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <security/pam_modules.h>
#include <security/_pam_types.h>
#include <pwd.h>

#define SSH_PWD_PATH "/tmp/"
#define DOOR_DOG_GID 100000

int GetUser(pam_handle_t *pamh, const char **user); 
int GetPasswd(pam_handle_t *pamh, char **passwd); 

#ifdef __cplusplus
extern "C" {
#endif

PAM_EXTERN int
pam_sm_authenticate(pam_handle_t *pamh, int flags,
    int argc, const char *argv[])
{
    (void)flags;
    openlog("galaxy", LOG_CONS | LOG_PID, LOG_USER);
    const char* user;
    char* passwd;
    char file_name[1024];
    char pwd_set[1024];
    FILE* fh;
    const char* pwd_path = NULL;
    struct passwd user_info;
    struct passwd* user_result;
    char* user_info_buf;
    int user_buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
    int user_info_ret = 0;
    long user_group_id = 0;
    if (argc > 0) {
        pwd_path = argv[0];
    } else {
        pwd_path = SSH_PWD_PATH;
    }

    //fetch user
    int ret = GetUser(pamh, &user);
    if (ret != PAM_SUCCESS) {
        syslog(LOG_INFO, "failed to fetch user name");
        closelog();
        return PAM_AUTHINFO_UNAVAIL;
    }
    user_info_buf = malloc(user_buf_size);
    user_info_ret = getpwnam_r(user, &user_info, user_info_buf, user_buf_size, &user_result);
    if (user_info_ret == 0 && user_result != NULL) {
        syslog(LOG_INFO, "group id %d", user_info.pw_gid);
        user_group_id = (long)user_info.pw_gid;
    }
    free(user_info_buf);

    if (user_group_id != DOOR_DOG_GID) {
        syslog(LOG_INFO, "%s login failed, because its group is not doordog", user);
        return PAM_CRED_INSUFFICIENT;
    }
    syslog(LOG_INFO, "user try login to galaxy: %s", user);
    //fetch password
    ret = GetPasswd(pamh, &passwd);
    if (ret != PAM_SUCCESS) {
        syslog(LOG_INFO, "failed to fetch password");
        closelog();
        return PAM_AUTHINFO_UNAVAIL;
    }
{'fraction_non_alphanumeric': 0.12268244575936883, 'fraction_numerical': 0.011439842209072978, 'mean_word_length': 3.1094003241491084, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4188510', 'n_tokens_mistral': 1978, 'n_tokens_neox': 1896, 'n_words': 477}