Skip to content

Commit

Permalink
Added gettext support for pam module
Browse files Browse the repository at this point in the history
Signed-off-by: Tero Saarni <[email protected]>
  • Loading branch information
tsaarni committed Mar 21, 2024
1 parent cced213 commit c3e8f81
Show file tree
Hide file tree
Showing 13 changed files with 420 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,6 @@ stamp-*

# /utils/
/utils/constants.py

# /po/
/po/*.gmo
3 changes: 3 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ endif
if ENABLE_UTILS
SUBDIRS += utils
endif
if ENABLE_NLS
SUBDIRS += po
endif
SUBDIRS += man tests

EXTRA_DIST = nslcd.conf nslcd.h $(wildcard ChangeLog-20??) \
Expand Down
2 changes: 2 additions & 0 deletions common/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ libdict_a_SOURCES = dict.c dict.h \
set.c set.h

libexpr_a_SOURCES = expr.c expr.h

noinst_HEADERS = gettext.h
38 changes: 38 additions & 0 deletions common/gettext.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
This file is part of the nss-pam-ldapd library.
Copyright (C) 2024 Tero Saarni
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/

#ifndef COMMON__GETTEXT_H
#define COMMON__GETTEXT_H 1

#if defined ENABLE_NLS && ENABLE_NLS

#include <libintl.h>
#define _(msgid) dgettext(PACKAGE, msgid)

#else

#define _(msgid) (msgid)
#define bindtextdomain(domainname, dirname) \
((void)(domainname), (const char *)(dirname))

#endif

#endif /* COMMON__GETTEXT_H */
24 changes: 13 additions & 11 deletions compat/ldap_passwordpolicy_err2txt.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,23 @@

#include "compat/ldap_compat.h"
#include "compat/attrs.h"
#include "common/gettext.h"


const char *ldap_passwordpolicy_err2txt(LDAPPasswordPolicyError error)
{
switch (error)
{
case PP_passwordExpired: return "Password expired";
case PP_accountLocked: return "Account locked";
case PP_changeAfterReset: return "Change after reset";
case PP_passwordModNotAllowed: return "Password modification not allowed";
case PP_mustSupplyOldPassword: return "Must supply old password";
case PP_insufficientPasswordQuality: return "Insufficient password quality";
case PP_passwordTooShort: return "Password too short";
case PP_passwordTooYoung: return "Password too young";
case PP_passwordInHistory: return "Password in history";
case PP_noError: return "No error";
default: return "Unknown error";
case PP_passwordExpired: return _("Password expired");
case PP_accountLocked: return _("Account locked");
case PP_changeAfterReset: return _("Change after reset");
case PP_passwordModNotAllowed: return _("Password modification not allowed");
case PP_mustSupplyOldPassword: return _("Must supply old password");
case PP_insufficientPasswordQuality: return _("Insufficient password quality");
case PP_passwordTooShort: return _("Password too short");
case PP_passwordTooYoung: return _("Password too young");
case PP_passwordInHistory: return _("Password in history");
case PP_noError: return _("No error");
default: return _("Unknown error");
}
}
8 changes: 5 additions & 3 deletions compat/pam_get_authtok.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include "compat/attrs.h"
#include "compat/pam_compat.h"

#include "common/gettext.h"

/* warning: this version assumes that try_first_pass is specified */

int pam_get_authtok(pam_handle_t *pamh, int item, const char **authtok,
Expand All @@ -56,12 +58,12 @@ int pam_get_authtok(pam_handle_t *pamh, int item, const char **authtok,
rc = pam_get_item(pamh, PAM_OLDAUTHTOK, (PAM_ITEM_CONST void **)&oldauthtok);
if ((rc == PAM_SUCCESS) && (oldauthtok != NULL))
{
prompt = (prompt != NULL) ? prompt : "New Password: ";
snprintf(retype_prompt, sizeof(retype_prompt), "Retype %s", prompt);
prompt = (prompt != NULL) ? prompt : _("New Password: ");
snprintf(retype_prompt, sizeof(retype_prompt), _("Retype %s"), prompt);
retype_prompt[sizeof(retype_prompt) - 1] = '\0';
}
else
prompt = (prompt != NULL) ? prompt : "Password: ";
prompt = (prompt != NULL) ? prompt : _("Password: ");
}
/* prepare prompt and get password */
rc = pam_prompt(pamh, PAM_PROMPT_ECHO_OFF, &passwd, "%s", prompt);
Expand Down
25 changes: 24 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,29 @@ then
AC_SUBST(nslcd_LIBS)
fi

# Optional NLS support
AC_MSG_CHECKING([whether to build the NLS support])
AC_ARG_ENABLE([nls],
AS_HELP_STRING([--enable-nls], [enable Native Language Support]),
[enable_nls=$enableval], [enable_nls=false]
)
AC_MSG_RESULT($enable_nls)
# If NLS is enabled, check for the required functions.
if test x$enable_nls = xyes; then
AC_CHECK_FUNCS([dgettext bindtextdomain], [], [enable_nls=false])
if test x$enable_nls != xyes; then
AC_MSG_ERROR([Functions required for NLS are not available])
fi

AC_CHECK_PROG(MSGFMT, msgfmt, yes)
if test x$MSGFMT != xyes; then
AC_MSG_ERROR([Command msgfmt not found])
fi

AC_DEFINE([ENABLE_NLS], [1], [Define if NLS support is enabled])
fi
AM_CONDITIONAL([ENABLE_NLS], [test x$enable_nls = xyes])

# pynslcd-specific tests
if test "x$enable_pynslcd" = "xyes"
then
Expand Down Expand Up @@ -1036,7 +1059,7 @@ AM_CONDITIONAL([NSS_FLAVOUR_FREEBSD], [test "x${with_nss_flavour}" = xfreebsd])
# generate files
AC_CONFIG_FILES([Makefile compat/Makefile common/Makefile nss/Makefile
pam/Makefile utils/Makefile nslcd/Makefile pynslcd/Makefile
man/Makefile tests/Makefile])
man/Makefile tests/Makefile po/Makefile])
AC_CONFIG_FILES([pynslcd/constants.py], [[
(
echo ''
Expand Down
2 changes: 1 addition & 1 deletion pam/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

noinst_PROGRAMS = pam_ldap.so

AM_CPPFLAGS=-I$(top_srcdir)
AM_CPPFLAGS=-I$(top_srcdir) -DLOCALEDIR=\"$(localedir)\"
AM_CFLAGS = $(PIC_CFLAGS)

pam_ldap_so_SOURCES = ../nslcd.h ../common/nslcd-prot.h \
Expand Down
9 changes: 7 additions & 2 deletions pam/pam.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
#include <pam/pam_modules.h>
#endif /* not HAVE_PAM_PAM_MODULES_H */

#include "common/gettext.h"

/* the name we store our context under */
#define PLD_CTX "PAM_LDAPD_CTX"

Expand Down Expand Up @@ -199,6 +201,9 @@ static int init(pam_handle_t *pamh, struct pld_cfg *cfg, struct pld_ctx **ctx,
{
int rc;
struct passwd *pwent;

bindtextdomain(PACKAGE, LOCALEDIR);

/* get user name */
rc = pam_get_user(pamh, username, NULL);
if (rc != PAM_SUCCESS)
Expand Down Expand Up @@ -711,7 +716,7 @@ int pam_sm_chauthtok(pam_handle_t *pamh, int flags,
/* try to authenticate with the LDAP administrator password by passing
an empty username to the authc request */
rc = pam_get_authtok(pamh, PAM_OLDAUTHTOK, &oldpassword,
"LDAP administrator password: ");
_("LDAP administrator password: "));
if (rc != PAM_SUCCESS)
return rc;
ctx->asroot = 1;
Expand All @@ -728,7 +733,7 @@ int pam_sm_chauthtok(pam_handle_t *pamh, int flags,
{
/* prompt the user for a password if needed */
rc = pam_get_authtok(pamh, PAM_OLDAUTHTOK, (const char **)&oldpassword,
"(current) LDAP Password: ");
_("(current) LDAP Password: "));
if (rc != PAM_SUCCESS)
return rc;
}
Expand Down
75 changes: 75 additions & 0 deletions po/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Makefile.am - use automake to generate Makefile.in
#
# Copyright (C) 2024 Tero Saarni.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA

# Usage:
#
# The list of source files containing translatable strings is defined in
# POTFILES variable. The list of languages is defined in LINGUAS variable.
#
# After adding new translatable strings to source files the .pot file (template)
# and the .po files (translations) need to be updated by running:
#
# make generate
#
# New language is added by creating a new .po file with msginit and adding
# the language to LINGUAS variable in this file.
#
# msginit --locale=[locale]
#

# List of source files containing translatable strings.
POTFILES = $(top_srcdir)/pam/pam.c \
$(top_srcdir)/compat/ldap_passwordpolicy_err2txt.c \
$(top_srcdir)/compat/pam_get_authtok.c

# List of languages to generate translations for.
LINGUAS = fi sv

POTFILE = $(PACKAGE).pot
POFILES = $(LINGUAS:=.po)
GMOFILES = $(LINGUAS:=.gmo)

# Files to clean when running make clean.
CLEANFILES = $(GMOFILES)

# Files to include when running make dist.
EXTRA_DIST = $(POTFILE) $(POFILES)

all: $(GMOFILES)

# Generate .pot file and update .po files.
generate: $(POTFILE) $(POFILES)

# Generate .pot file from source files.
$(POTFILE): $(POTFILES)
xgettext --keyword=_ --language=C -o $@ $(POTFILES)

# Update existing .po files with msgmerge.
$(POFILES): $(POTFILE)
msgmerge --update --no-fuzzy-matching $@ $<

# Compile .po files into .gmo files.
$(GMOFILES): %.gmo: %.po
msgfmt -c -o $@ $<

install-data-local: $(GMOFILES)
for lang in $(LINGUAS) ; do \
$(MKDIR_P) $(DESTDIR)$(localedir)/$$lang/LC_MESSAGES ; \
$(INSTALL) $$lang.gmo $(DESTDIR)$(localedir)/$$lang/LC_MESSAGES/$(PACKAGE).mo ; \
done
83 changes: 83 additions & 0 deletions po/fi.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Finnish translations for nss-pam-ldapd package.
# Copyright (C) 2024 THE nss-pam-ldapd'S COPYRIGHT HOLDER
# This file is distributed under the same license as the nss-pam-ldapd package.
# <[email protected]>, 2024.
#
msgid ""
msgstr ""
"Project-Id-Version: nss-pam-ldapd 0.9.12\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-03-18 09:41+0000\n"
"PO-Revision-Date: 2024-03-11 06:10+0000\n"
"Last-Translator: <[email protected]>\n"
"Language-Team: Finnish <[email protected]>\n"
"Language: fi\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

#: ../pam/pam.c:719
msgid "LDAP administrator password: "
msgstr "LDAP-ylläpitäjän salasana: "

#: ../pam/pam.c:736
msgid "(current) LDAP Password: "
msgstr "(nykyinen) LDAP-salasana: "

#: ../compat/ldap_passwordpolicy_err2txt.c:38
msgid "Password expired"
msgstr "Salasana vanhentunut"

#: ../compat/ldap_passwordpolicy_err2txt.c:39
msgid "Account locked"
msgstr "Käyttäjätili lukittu"

#: ../compat/ldap_passwordpolicy_err2txt.c:40
msgid "Change after reset"
msgstr "Salasana on vaihdettava nollauksen jälkeen"

#: ../compat/ldap_passwordpolicy_err2txt.c:41
msgid "Password modification not allowed"
msgstr "Salasanan muuttaminen ei ole sallittua"

#: ../compat/ldap_passwordpolicy_err2txt.c:42
msgid "Must supply old password"
msgstr "Anna vanha salasana"

#: ../compat/ldap_passwordpolicy_err2txt.c:43
msgid "Insufficient password quality"
msgstr "Riittämätön salasanan laatu"

#: ../compat/ldap_passwordpolicy_err2txt.c:44
msgid "Password too short"
msgstr "Salasana on liian lyhyt"

#: ../compat/ldap_passwordpolicy_err2txt.c:45
msgid "Password too young"
msgstr "Salasana on vaihdettu liian äskettäin"

#: ../compat/ldap_passwordpolicy_err2txt.c:46
msgid "Password in history"
msgstr "Uusi salasana on vanhojen salasanojen luettelossa"

#: ../compat/ldap_passwordpolicy_err2txt.c:47
msgid "No error"
msgstr "Ei virhettä"

#: ../compat/ldap_passwordpolicy_err2txt.c:48
msgid "Unknown error"
msgstr "Tuntematon virhe"

#: ../compat/pam_get_authtok.c:61
msgid "New Password: "
msgstr "Uusi salasana: "

#: ../compat/pam_get_authtok.c:62
#, c-format
msgid "Retype %s"
msgstr "Kirjoita uudelleen %s"

#: ../compat/pam_get_authtok.c:66
msgid "Password: "
msgstr "Salasana: "
Loading

0 comments on commit c3e8f81

Please sign in to comment.