diff options
Diffstat (limited to 'src/libnsss/nsss_switch_pwd_read.c')
-rw-r--r-- | src/libnsss/nsss_switch_pwd_read.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/libnsss/nsss_switch_pwd_read.c b/src/libnsss/nsss_switch_pwd_read.c new file mode 100644 index 0000000..936b0f4 --- /dev/null +++ b/src/libnsss/nsss_switch_pwd_read.c @@ -0,0 +1,53 @@ +/* ISC license. */ + +#include <stdint.h> +#include <string.h> +#include <errno.h> +#include <skalibs/posixplz.h> +#include <skalibs/uint32.h> +#include <skalibs/error.h> +#include <skalibs/stralloc.h> +#include <skalibs/unix-timed.h> +#include <nsss/pwd-def.h> +#include "nsss-switch-internal.h" + +/* + Expects: + 4 bytes pw_uid + 4 bytes pw_gid + 4 bytes total length of strings (including \0's) + \0-terminated pw_name + \0-terminated pw_passwd + \0-terminated pw_gecos + \0-terminated pw_dir + \0-terminated pw_shell +*/ + +int nsss_switch_pwd_read (buffer *b, struct passwd *pw, stralloc *sa, tain_t const *deadline, tain_t *stamp) +{ + struct passwd pwtmp ; + uint32_t total, len, x ; + char *p ; + char buf[12] ; + if (!buffer_timed_get(b, buf, 12, deadline, stamp)) return 0 ; + uint32_unpack_big(buf, &x) ; pwtmp.pw_uid = x ; + uint32_unpack_big(buf + 4, &x) ; pwtmp.pw_gid = x ; + uint32_unpack_big(buf + 8, &total) ; + if (!stralloc_readyplus(sa, total)) return 0 ; + if (!buffer_timed_get(b, sa->s + sa->len, total, deadline, stamp)) return 0 ; + if (sa->s[sa->len + total - 1]) return (errno = EPROTO, 0) ; + p = sa->s + sa->len ; len = total ; + pwtmp.pw_name = p ; x = strnlen(p, len) + 1 ; p += x ; len -= x ; + if (!len) return (errno = EPROTO, 0) ; + pwtmp.pw_passwd = p ; x = strnlen(p, len) + 1 ; p += x ; len -= x ; + if (!len) return (errno = EPROTO, 0) ; + pwtmp.pw_gecos = p ; x = strnlen(p, len) + 1 ; p += x ; len -= x ; + if (!len) return (errno = EPROTO, 0) ; + pwtmp.pw_dir = p ; x = strnlen(p, len) + 1 ; p += x ; len -= x ; + if (!len) return (errno = EPROTO, 0) ; + pwtmp.pw_shell = p ; x = strnlen(p, len) + 1 ; p += x ; len -= x ; + if (len) return (errno = EPROTO, 0) ; + sa->len += total ; + *pw = pwtmp ; + return 1 ; +} |