2019-08-03 22:23:04 +08:00
|
|
|
/****************************************************************************
|
2019-08-05 03:32:36 +08:00
|
|
|
* libs/libc/grp/lib_getgrbufr.c
|
2019-08-03 22:23:04 +08:00
|
|
|
*
|
2024-09-25 20:05:00 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-03-02 16:55:05 +08:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2019-08-03 22:23:04 +08:00
|
|
|
*
|
2021-03-02 16:55:05 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2019-08-03 22:23:04 +08:00
|
|
|
*
|
2021-03-02 16:55:05 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2019-08-03 22:23:04 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <grp.h>
|
|
|
|
|
|
|
|
#include "grp/lib_grp.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: getgrbuf_r
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* libc/grp internal helper function for getgrgid_r and getgrnam_r to setup
|
|
|
|
* the caller supplied 'grp' and 'buf' buffers once a matching entry has
|
|
|
|
* been found.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* gid - Value to set grp->gr_gid to.
|
|
|
|
* name - Value to set grp->gr_name to.
|
|
|
|
* passwd - Value to set grp->passwd to.
|
|
|
|
* grp - Pointer to the space to store the retrieved group structure in.
|
|
|
|
* buf - String fields pointed to by the group struct are stored here.
|
|
|
|
* buflen - The length of buf in bytes.
|
|
|
|
* result - Pointer to the resulting group struct, or NULL in case of fail.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success getgrgid_r returns 0 and sets *result to grp. In case of
|
|
|
|
* failure an error number is returned.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int getgrbuf_r(gid_t gid, FAR const char *name, FAR const char *passwd,
|
|
|
|
FAR struct group *grp, FAR char *buf, size_t buflen,
|
|
|
|
FAR struct group **result)
|
|
|
|
{
|
|
|
|
size_t reqdlen;
|
|
|
|
size_t padlen;
|
2023-02-08 11:29:09 +08:00
|
|
|
size_t namesize;
|
|
|
|
size_t passwdsize;
|
2019-08-03 22:23:04 +08:00
|
|
|
|
|
|
|
/* In 'buf' a NULL pointer value will be stored, which must be naturally
|
|
|
|
* aligned, followed by the null terminated group name string and the null
|
|
|
|
* terminated passwd string 'x' (indicating 'no password'). Make sure
|
|
|
|
* sufficient buffer space was supplied by the caller.
|
|
|
|
*/
|
|
|
|
|
2023-02-08 11:29:09 +08:00
|
|
|
namesize = strlen(name) + 1;
|
|
|
|
passwdsize = strlen(passwd) + 1;
|
2019-08-03 22:41:55 +08:00
|
|
|
padlen = sizeof(FAR void *) - ((uintptr_t)buf % sizeof(FAR char *));
|
2023-02-08 11:29:09 +08:00
|
|
|
reqdlen = sizeof(FAR void *) + namesize + passwdsize;
|
2019-08-03 22:23:04 +08:00
|
|
|
|
|
|
|
if (buflen < padlen + reqdlen)
|
|
|
|
{
|
|
|
|
/* Insufficient buffer space supplied. */
|
|
|
|
|
|
|
|
*result = NULL;
|
|
|
|
return ERANGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
grp->gr_mem = (FAR char **)&buf[padlen];
|
|
|
|
grp->gr_name = &buf[padlen + sizeof(FAR char *)];
|
2023-02-08 11:29:09 +08:00
|
|
|
grp->gr_passwd = &buf[padlen + sizeof(FAR char *) + namesize];
|
2019-08-03 22:23:04 +08:00
|
|
|
|
2023-02-08 11:29:09 +08:00
|
|
|
strlcpy(grp->gr_name, name, namesize);
|
|
|
|
strlcpy(grp->gr_passwd, passwd, passwdsize);
|
2019-08-03 22:23:04 +08:00
|
|
|
grp->gr_gid = gid;
|
|
|
|
*grp->gr_mem = NULL;
|
|
|
|
|
|
|
|
*result = grp;
|
|
|
|
return 0;
|
|
|
|
}
|