From 2cd1f79e8c75534ef3bba7fad98611ba4821f801 Mon Sep 17 00:00:00 2001 From: Dante Catalfamo Date: Wed, 23 Dec 2020 16:10:36 -0500 Subject: bsd-auth: add auth_setpwd --- .../WIP-how-bsd-authentication-works/index.org | 57 ++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'content/posts/WIP-how-bsd-authentication-works/index.org') diff --git a/content/posts/WIP-how-bsd-authentication-works/index.org b/content/posts/WIP-how-bsd-authentication-works/index.org index 0c1eaa8..f4cfc4b 100644 --- a/content/posts/WIP-how-bsd-authentication-works/index.org +++ b/content/posts/WIP-how-bsd-authentication-works/index.org @@ -330,6 +330,63 @@ =auth_getstate= return the =state= of =*as=. +** auth_setpwd + :PROPERTIES: + :CUSTOM_ID: auth_setpwd + :END: + @@html:
@@ + #+begin_src c + int auth_setpwd(auth_session_t *as, struct passwd *pwd) + #+end_src + @@html: @@ + #+begin_src c + { + struct passwd pwstore; + char *instance, pwbuf[_PW_BUF_LEN]; + + if (pwd == NULL && as->pwd == NULL && as->name == NULL) + return (-1); /* true failure */ + + if (pwd == NULL) { + /* + * If we were not passed in a pwd structure we need to + * go find one for ourself. Always look up the username + * (if it is defined) in the passwd database to see if there + * is an entry for the user. If not, either use the current + * entry or simply return a 1 which implies there is + * no user by that name here. This is not a failure, just + * a point of information. + */ + if (as->name == NULL) + return (0); + getpwnam_r(as->name, &pwstore, pwbuf, sizeof(pwbuf), &pwd); + if (pwd == NULL) { + instance = strchr(as->name, '/'); + if (instance == NULL) + return (as->pwd ? 0 : 1); + if (strcmp(instance, "/root") == 0) { + getpwnam_r(instance + 1, &pwstore, pwbuf, + sizeof(pwbuf), &pwd); + } + if (pwd == NULL) + return (as->pwd ? 0 : 1); + } + } + if ((pwd = pw_dup(pwd)) == NULL) + return (-1); /* true failure */ + if (as->pwd) { + explicit_bzero(as->pwd->pw_passwd, strlen(as->pwd->pw_passwd)); + free(as->pwd); + } + as->pwd = pwd; + return (0); + } + #+end_src + @@html:
@@ + + =auth_setpwd= is used to retrieve and set the [[https://man.openbsd.org/man3/getpwnam.3][password database]] + entry in =as= if one isn't already set. + ** auth_set_va_list :PROPERTIES: :CUSTOM_ID: auth_set_va_list -- cgit v1.2.3