1
0
mirror of https://github.com/Yubico/yubico-pam.git synced 2024-11-29 00:24:11 +01:00

Move soon-to-be commonly used code to util.c

This commit is contained in:
Fredrik Thulin 2011-03-17 14:36:19 +01:00 committed by Tollef Fog Heen
parent 3abc5b2d81
commit c557249503
4 changed files with 134 additions and 66 deletions

View File

@ -32,7 +32,7 @@ libdir = $(PAMDIR)
lib_LTLIBRARIES = pam_yubico.la
pam_yubico_la_SOURCES = pam_yubico.c
pam_yubico_la_SOURCES = pam_yubico.c util.c
# XXX add -Wl,-x too? PAM documentation suggests it.
pam_yubico_la_LIBADD = @LTLIBYKCLIENT@ @LIBLDAP@ @YKPERS_LIBS@
pam_yubico_la_LDFLAGS = -module -avoid-version

View File

@ -34,6 +34,8 @@
#include <ctype.h>
#include <syslog.h>
#include "util.h"
/* Libtool defines PIC for shared objects */
#ifndef PIC
#define PAM_STATIC
@ -86,9 +88,6 @@
#endif
#endif
#include <sys/types.h>
#include <pwd.h>
#define TOKEN_OTP_LEN 32
#define MAX_TOKEN_ID_LEN 16
#define DEFAULT_TOKEN_ID_LEN 12
@ -174,25 +173,13 @@ authorize_user_token (const char *authfile,
}
else
{
char *userfile = NULL;
/* Getting file from user home directory
..... i.e. ~/.yubico/authorized_yubikeys
*/
struct passwd *p;
char *userfile = NULL;
#define USERFILE "/.yubico/authorized_yubikeys"
p = getpwnam (username);
if (p)
{
userfile = malloc ((p->pw_dir ? strlen (p->pw_dir) : 0)
+ strlen (USERFILE) + 1);
if (!userfile)
return 0;
strcpy (userfile, p->pw_dir);
strcat (userfile, USERFILE);
}
if (! get_user_cfgfile_path (NULL, "authorized_yubikeys", username, &userfile))
return 0;
retval = check_user_token (userfile, username, otp_id);
@ -389,62 +376,23 @@ struct cfg
char *chalresp_path;
};
/* Fill buf with len bytes of random data */
static int generate_challenge(char *buf, int len)
{
FILE *u;
int i, res;
u = fopen("/dev/urandom", "r");
if (!u) {
return -1;
}
res = fread(buf, 1, (size_t) len, u);
fclose(u);
return (res != len);
}
int
get_user_challenge_file(struct cfg *cfg, const char *username, char **fn)
get_user_challenge_file(const char *chalresp_path, const char *username, char **fn)
{
/* Getting file from user home directory, i.e. ~/.yubico/challenge, or
* from a system wide directory.
*
* Format is hex(challenge):hex(response):slot num
*/
struct passwd *p;
char *userfile;
if (cfg->chalresp_path) {
if (asprintf (&userfile, "%s/%s", cfg->chalresp_path, username) >= 0)
*fn = userfile;
return (userfile >= 0);
}
/* The challenge to use is located in a file in the user's home directory,
* which therefor can't be encrypted.
* which therefor can't be encrypted. If an encrypted home directory is used,
* the option chalresp_path can be used to point to a system-wide directory.
*/
#define USERFILE "/.yubico/challenge"
p = getpwnam (username);
if (!p)
goto out;
userfile = malloc ((p->pw_dir ? strlen (p->pw_dir) : 0)
+ strlen (USERFILE) + 1);
if (!userfile)
goto out;
char *filename = "challenge";
strcpy (userfile, p->pw_dir);
strcat (userfile, USERFILE);
*fn = userfile;
return 1;
out:
return 0;
return get_user_cfgfile_path (chalresp_path, filename, username, fn);
}
static int
@ -468,7 +416,7 @@ do_challenge_response(struct cfg *cfg, const char *username)
ret = PAM_AUTH_ERR;
flags |= YK_FLAG_MAYBLOCK;
if (! get_user_challenge_file (cfg, username, &userfile)) {
if (! get_user_challenge_file (cfg->chalresp_path, username, &userfile)) {
D(("Failed getting user challenge file for user %s", username));
goto out;
}
@ -537,7 +485,7 @@ do_challenge_response(struct cfg *cfg, const char *username)
D(("Got the expected response, generating new challenge (%i bytes).", CR_CHALLENGE_SIZE));
if (generate_challenge(challenge, CR_CHALLENGE_SIZE)) {
if (generate_random(challenge, CR_CHALLENGE_SIZE)) {
D(("Failed generating new challenge!"));
goto out;
}

85
util.c Normal file
View File

@ -0,0 +1,85 @@
/*
* Copyright (c) 2011 Yubico AB.
* Copyright (c) 2011 Tollef Fog Heen <tfheen@err.no>
* All rights reserved.
*
* Author : Fredrik Thulin <fredrik@yubico.com>
* Author : Tollef Fog Heen <tfheen@err.no>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <pwd.h>
#include "util.h"
/* Fill buf with len bytes of random data */
static int generate_random(char *buf, int len)
{
FILE *u;
int i, res;
u = fopen("/dev/urandom", "r");
if (!u) {
return -1;
}
res = fread(buf, 1, (size_t) len, u);
fclose(u);
return (res != len);
}
int
get_user_cfgfile_path(const char *common_path, const char *filename, const char *username, char **fn)
{
/* Getting file from user home directory, i.e. ~/.yubico/challenge, or
* from a system wide directory.
*
* Format is hex(challenge):hex(response):slot num
*/
struct passwd *p;
char *userfile;
if (common_path != NULL) {
if (asprintf (&userfile, "%s/%s", common_path, filename) >= 0)
*fn = userfile;
return (userfile >= 0);
}
/* No common path provided. Construct path to user's ~/.yubico/filename */
p = getpwnam (username);
if (!p)
return 0;
if (asprintf (&userfile, "%s/.yubico/%s", p->pw_dir, filename) >= 0)
*fn = userfile;
return (userfile >= 0);
}

35
util.h Normal file
View File

@ -0,0 +1,35 @@
/*
* Copyright (c) 2011 Yubico AB.
* Copyright (c) 2011 Tollef Fog Heen <tfheen@err.no>
* All rights reserved.
*
* Author : Fredrik Thulin <fredrik@yubico.com>
* Author : Tollef Fog Heen <tfheen@err.no>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
int generate_random(char *buf, int len);
int get_user_cfgfile_path(const char *common_path, const char *filename, const char *username, char **fn);