passwd.c revision fcef1231eea30ee9cdc75f3f39f4b9207a84ea1d
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen/*
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * System Security Services Daemon. NSS client interface
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen *
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * Copyright (C) Simo Sorce 2007
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen *
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * This program is free software; you can redistribute it and/or modify
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * it under the terms of the GNU Lesser General Public License as
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * published by the Free Software Foundation; either version 2.1 of the
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * License, or (at your option) any later version.
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen *
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * This program is distributed in the hope that it will be useful,
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * but WITHOUT ANY WARRANTY; without even the implied warranty of
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * GNU Lesser General Public License for more details.
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen *
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * You should have received a copy of the GNU Lesser General Public
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * License along with this program; if not, write to the Free Software
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen */
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen/* PASSWD database NSS interface */
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen#include <nss.h>
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen#include <errno.h>
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen#include <sys/types.h>
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen#include <unistd.h>
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen#include <stdlib.h>
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen#include <stdint.h>
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen#include <string.h>
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen#include "sss_cli.h"
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainenstatic struct sss_nss_getpwent_data {
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen size_t len;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen size_t ptr;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen uint8_t *data;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen} sss_nss_getpwent_data;
01f54478a7c69b88ab13840c99bbab19a0d7d754Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainenstatic void sss_nss_getpwent_data_clean(void) {
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen if (sss_nss_getpwent_data.data != NULL) {
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen free(sss_nss_getpwent_data.data);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen sss_nss_getpwent_data.data = NULL;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen sss_nss_getpwent_data.len = 0;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen sss_nss_getpwent_data.ptr = 0;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen}
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen/* GETPWNAM Request:
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen *
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * 0-X: string with name
01f54478a7c69b88ab13840c99bbab19a0d7d754Timo Sirainen *
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * GERTPWUID Request:
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen *
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * 0-7: 64bit number with uid
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen *
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * Replies:
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen *
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * 0-3: 32bit unsigned number of results
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * 4-7: 32bit unsigned (reserved/padding)
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * For each result:
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * 0-7: 64bit number uid
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * 8-15: 64bit number gid
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * 16-X: sequence of 5, 0 terminated, strings (name, passwd, gecos, dir, shell)
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen */
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainenstruct sss_nss_pw_rep {
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen struct passwd *result;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen char *buffer;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen size_t buflen;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen};
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainenstatic int sss_nss_getpw_readrep(struct sss_nss_pw_rep *pr,
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen uint8_t *buf, size_t *len)
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen{
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen size_t i, slen;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen char *sbuf;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen int err;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen if (*len < 21) { /* not enough space for data, bad packet */
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen return EBADMSG;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen pr->result->pw_uid = ((int64_t *)buf)[0];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen pr->result->pw_gid = ((int64_t *)buf)[1];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
01f54478a7c69b88ab13840c99bbab19a0d7d754Timo Sirainen sbuf = (char *)&buf[16];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen if (*len < pr->buflen) {
01f54478a7c69b88ab13840c99bbab19a0d7d754Timo Sirainen slen = *len;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen err = EBADMSG;
01f54478a7c69b88ab13840c99bbab19a0d7d754Timo Sirainen } else {
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen slen = pr->buflen;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen err = ENOMEM;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen pr->result->pw_name = &(pr->buffer[0]);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen i = 0;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen while (i < slen) {
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen pr->buffer[i] = sbuf[i];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen if (pr->buffer[i] == '\0') break;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen i++;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen if (i == slen) { /* premature end of buf */
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen return err;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen i++;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen pr->result->pw_passwd = &(pr->buffer[i]);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen while (i < slen) {
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen pr->buffer[i] = sbuf[i];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen if (pr->buffer[i] == '\0') break;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen i++;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen if (i == slen) { /* premature end of buf */
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen return err;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen i++;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen pr->result->pw_gecos = &(pr->buffer[i]);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen while (i < slen) {
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen pr->buffer[i] = sbuf[i];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen if (pr->buffer[i] == '\0') break;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen i++;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen if (i == slen) { /* premature end of buf */
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen return err;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen i++;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen pr->result->pw_dir = &(pr->buffer[i]);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen while (i < slen) {
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen pr->buffer[i] = sbuf[i];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen if (pr->buffer[i] == '\0') break;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen i++;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen if (i == slen) { /* premature end of buf */
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen return err;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen i++;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen pr->result->pw_shell = &(pr->buffer[i]);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen while (i < slen) {
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen pr->buffer[i] = sbuf[i];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen if (pr->buffer[i] == '\0') break;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen i++;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen if (pr->buffer[i] != '\0') { /* premature end of buf */
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen return err;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen *len = *len -16 -i -1;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen return 0;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen}
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainenenum nss_status _nss_sss_getpwnam_r(const char *name, struct passwd *result,
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen char *buffer, size_t buflen, int *errnop)
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen{
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen struct sss_cli_req_data rd;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen struct sss_nss_pw_rep pwrep;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen uint8_t *repbuf;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen size_t replen, len;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen enum nss_status nret;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen int ret;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen rd.len = strlen(name) + 1;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen rd.data = name;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen nret = sss_nss_make_request(SSS_NSS_GETPWNAM, &rd,
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen &repbuf, &replen, errnop);
01f54478a7c69b88ab13840c99bbab19a0d7d754Timo Sirainen if (nret != NSS_STATUS_SUCCESS) {
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen return nret;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen pwrep.result = result;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen pwrep.buffer = buffer;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen pwrep.buflen = buflen;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen /* no results if not found */
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen if (((uint32_t *)repbuf)[0] == 0) {
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen free(repbuf);
01f54478a7c69b88ab13840c99bbab19a0d7d754Timo Sirainen return NSS_STATUS_NOTFOUND;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen /* only 1 result is accepted for this function */
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen if (((uint32_t *)repbuf)[0] != 1) {
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen *errnop = EBADMSG;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen return NSS_STATUS_TRYAGAIN;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen len = replen - 8;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ret = sss_nss_getpw_readrep(&pwrep, repbuf+8, &len);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen free(repbuf);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen if (ret) {
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen *errnop = ret;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen return NSS_STATUS_TRYAGAIN;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen return NSS_STATUS_SUCCESS;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen}
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainenenum nss_status _nss_sss_getpwuid_r(uid_t uid, struct passwd *result,
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen char *buffer, size_t buflen, int *errnop)
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen{
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen struct sss_cli_req_data rd;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen struct sss_nss_pw_rep pwrep;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen uint8_t *repbuf;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen size_t replen, len;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen enum nss_status nret;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen int64_t user_uid;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen int ret;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen user_uid = uid;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen rd.len = sizeof(int64_t);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen rd.data = &user_uid;
01f54478a7c69b88ab13840c99bbab19a0d7d754Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen nret = sss_nss_make_request(SSS_NSS_GETPWUID, &rd,
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen &repbuf, &replen, errnop);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen if (nret != NSS_STATUS_SUCCESS) {
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen return nret;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen pwrep.result = result;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen pwrep.buffer = buffer;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen pwrep.buflen = buflen;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen /* no results if not found */
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen if (((uint32_t *)repbuf)[0] == 0) {
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen free(repbuf);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen return NSS_STATUS_NOTFOUND;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen /* only 1 result is accepted for this function */
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen if (((uint32_t *)repbuf)[0] != 1) {
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen *errnop = EBADMSG;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen return NSS_STATUS_TRYAGAIN;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen len = replen - 8;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ret = sss_nss_getpw_readrep(&pwrep, repbuf+8, &len);
01f54478a7c69b88ab13840c99bbab19a0d7d754Timo Sirainen free(repbuf);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen if (ret) {
01f54478a7c69b88ab13840c99bbab19a0d7d754Timo Sirainen *errnop = ret;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen return NSS_STATUS_TRYAGAIN;
01f54478a7c69b88ab13840c99bbab19a0d7d754Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen return NSS_STATUS_SUCCESS;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen}
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainenenum nss_status _nss_sss_setpwent(void)
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen{
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen enum nss_status nret;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen int errnop;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen /* make sure we do not have leftovers, and release memory */
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen sss_nss_getpwent_data_clean();
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen nret = sss_nss_make_request(SSS_NSS_SETPWENT,
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen NULL, NULL, NULL, &errnop);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen if (nret != NSS_STATUS_SUCCESS) {
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen errno = errnop;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen return nret;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
01f54478a7c69b88ab13840c99bbab19a0d7d754Timo Sirainen
01f54478a7c69b88ab13840c99bbab19a0d7d754Timo Sirainen return NSS_STATUS_SUCCESS;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen}
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainenenum nss_status _nss_sss_getpwent_r(struct passwd *result,
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen char *buffer, size_t buflen,
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen int *errnop)
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen{
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen struct sss_cli_req_data rd;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen struct sss_nss_pw_rep pwrep;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen uint8_t *repbuf;
size_t replen;
enum nss_status nret;
uint32_t num_entries;
int ret;
/* if there are leftovers return the next one */
if (sss_nss_getpwent_data.data != NULL &&
sss_nss_getpwent_data.ptr < sss_nss_getpwent_data.len) {
repbuf = sss_nss_getpwent_data.data + sss_nss_getpwent_data.ptr;
replen = sss_nss_getpwent_data.len - sss_nss_getpwent_data.ptr;
pwrep.result = result;
pwrep.buffer = buffer;
pwrep.buflen = buflen;
ret = sss_nss_getpw_readrep(&pwrep, repbuf, &replen);
if (ret) {
*errnop = ret;
return NSS_STATUS_TRYAGAIN;
}
/* advance buffer pointer */
sss_nss_getpwent_data.ptr = sss_nss_getpwent_data.len - replen;
return NSS_STATUS_SUCCESS;
}
/* release memory if any */
sss_nss_getpwent_data_clean();
/* retrieve no more than SSS_NSS_MAX_ENTRIES at a time */
num_entries = SSS_NSS_MAX_ENTRIES;
rd.len = sizeof(uint32_t);
rd.data = &num_entries;
nret = sss_nss_make_request(SSS_NSS_GETPWENT, &rd,
&repbuf, &replen, errnop);
if (nret != NSS_STATUS_SUCCESS) {
return nret;
}
/* no results if not found */
if ((((uint32_t *)repbuf)[0] == 0) || (replen - 8 == 0)) {
free(repbuf);
return NSS_STATUS_NOTFOUND;
}
sss_nss_getpwent_data.data = repbuf;
sss_nss_getpwent_data.len = replen;
sss_nss_getpwent_data.ptr = 8; /* skip metadata fields */
/* call again ourselves, this will return the first result */
return _nss_sss_getpwent_r(result, buffer, buflen, errnop);
}
enum nss_status _nss_sss_endpwent(void)
{
enum nss_status nret;
int errnop;
/* make sure we do not have leftovers, and release memory */
sss_nss_getpwent_data_clean();
nret = sss_nss_make_request(SSS_NSS_ENDPWENT,
NULL, NULL, NULL, &errnop);
if (nret != NSS_STATUS_SUCCESS) {
errno = errnop;
return nret;
}
return NSS_STATUS_SUCCESS;
}