Code Monkey home page Code Monkey logo

pam_lua's Introduction

pam_lua

A PAM module scriptable using Lua.

Dependencies

  • Sane OS/Distro
    • GNU/Linux works.
    • FreeBSD works too, but...
      • There are minor differences, but should be OK as long as you don't dive too deep..
  • C99 compatible compiler
  • Open PAM or Linux PAM. Might work with other PAM variants, dunno.
  • Lua libraries (5.1 and luajit tested)
    • Anything above 5.1 should work, hopefully.

Compiling

make lua=5.1

Set 5.1 to whatever Lua version you want to use, according to pkg-config, 5.1/-5.1 for Lua 5.1, jit for luajit, etc...

Using

  • Copy pam_lua.so to /lib/security or alternative for wherever your distribution wants PAM modules to reside in.
    • On FreeBSD, copy it to /usr/lib.
  • Make a Lua script, similar to this (untested) script:
if pam.handler == "authenticate" then
	local username = pam.get_user()
	local password = pam.readline("Password: ", false) -- password prompt with hidden input
	if username == "user" and password == "letmein" then
		return pam.ret.success -- correct credentials, allow login!
	end
	return pam.ret.perm_denied
else -- not authenticate
	return pam.ret.ignore -- ignore this handler, cause it doesn't apply.
end
  • Edit a PAM config file and add a line using pam_lua.so, say auth sufficient pam_lua.so script=/path/to/script.lua
    • Note: If the script is incorrect, you might not be able to log in. If it contains backdoors, someone else might get in...

Examples

A dynamic MOTD generator I made is here: motd.lua. It ain't the prettiest code-wise, but it does the job. Ships with Clippy!

API

  • pam.type

    • The invoked module call type, one of the following:
      • auth
      • account
      • session
      • password
  • pam.handler

    • The PAM hook type, one of the following:
      • setcred
      • authenticate
      • acc_mgnt
      • open_session
      • close_session
      • chauthtok
    • You mostly have to only care about authenticate.
  • active = pam.flag[name]

    • Checks if flag is active.
    • Flags:
      • silent: If the service should not generate any messages.
      • when in pam hook authenticate:
        • disallow_null_authtok: If the service should return pam.ret.auth_error when the auth token is null.
      • when in pam hook setcred:
        • establish_cred: set credentials
        • delete_cred: delete credentials
        • reinitialize_cred: reinitialize credentials, like resetting password
        • refresh_cred: extend lifetime of credentials
      • when in pam hook chauthtok:
        • change_expired_authtok: only update those passwords if they have aged, otherwise update them unconditionally.
  • user = pam.get_user([login_prompt])

    • Returns the username, with prompt if not asked before.
  • input[, failure_code] = pam.readline(prompt[, visible])

    • Generic text input, if visible is false then the input is hidden.
    • If getting input fails, it returns nil, error_code, which is the numerical representation of a PAM error.
  • return_code = pam.info(text)

    • Displays info text
  • return_code = pam.error(text)

    • Displays an error.
  • textual_return_code = pam.code[numerical_return_code]

    • Looks up the textual return code by a numeric one.
  • numerical_return_code = pam.ret[textual_return_code]

    • The opposite of the above.
    • Some important codes:
      • "ignore": Skip handler.
      • "success": Success, allow login/go to next handler.
      • "perm_denied": Permission denied, drop out if handler is required.
      • "abort": Abort.
      • "try_again": Try again!
  • value = pam.getenv(key)

    • Returns the environment variable in the PAM env.
  • return_code = pam.setenv(key[, value])

    • Set an environment variable in the PAM env. If value is not given, key will be deleted instead of set.

Make sure to return a return code, like pam.ret.success or pam.ret.perm_denied.

If you do not plan to do anything in a specific PAM hook, return pam.ret.ignore.

License

MIT

pam_lua's People

Contributors

vifino avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

wangqiangcc

pam_lua's Issues

Examples

We need to provide some decent examples ranging from simple MOTD things to actual authentication.

Testing

pam_lua should be tested by CI and a test suite.

  • Figure out a way to test functionality.
  • Use pamtester and some hackery.
  • Test all functions and variables.
  • It is quite important that everything works.
  • Test across multiple distributions and OSes.
  • Mainly to test differences between OpenPAM, Linux PAM, etc...
  • Linux is the priority, but FreeBSD should be tested eventually.

Wrap pam_syslog(3)

https://linux.die.net/man/3/pam_syslog

Not sure how to handle the priority bit. Not all Lua versions have bitwise operations.

  • Bind pam_syslog(3) as pam.log(priority, fmt, ...).
  • Maybe don't use the printf style arguments of the function, but string.format to give more Lua-esqe results.
  • Bind priority related values: type and facility.
  • Figure out how to make them easily usable.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.