2019-08-04 15:18:58 +02:00
|
|
|
/****************************************************************************
|
2019-08-04 21:32:36 +02:00
|
|
|
* libs/libc/pwd/lib_getpwbufr.c
|
2019-08-04 15:18:58 +02:00
|
|
|
*
|
2021-03-02 09:55:05 +01: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-04 15:18:58 +02:00
|
|
|
*
|
2021-03-02 09:55:05 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2019-08-04 15:18:58 +02:00
|
|
|
*
|
2021-03-02 09:55:05 +01: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-04 15:18:58 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
|
|
|
|
#include "pwd/lib_pwd.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: getpwbuf_r
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* libc/grp internal helper function for getpwuid_r and getpwnam_r to setup
|
|
|
|
* the caller supplied 'pwd' and 'buf' buffers once a matching entry has
|
|
|
|
* been found.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* uid - Value to set the passwd structure's pw_uid field to.
|
|
|
|
* gid - Value to set the passwd structure's pw_gid field to.
|
|
|
|
* name - Value to set the passwd structure's pw_name field to.
|
|
|
|
* dir - Value to set the passwd structure's pw_dir field to.
|
|
|
|
* shell - Value to set the passwd structure's pw_shell field to.
|
2021-03-02 09:55:05 +01:00
|
|
|
* pwd - Pointer to the space to store the retrieved passwd structure
|
|
|
|
* in.
|
2019-08-04 15:18:58 +02:00
|
|
|
* buf - String fields pointed to by the passwd struct are stored here.
|
|
|
|
* buflen - The length of buf in bytes.
|
2021-03-02 09:55:05 +01:00
|
|
|
* result - Pointer to the resulting passwd struct, or NULL in case of
|
|
|
|
* fail.
|
2019-08-04 15:18:58 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success getpwgid_r returns 0 and sets *result to pwd. In case of
|
|
|
|
* failure an error number is returned.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int getpwbuf_r(uid_t uid, gid_t gid, FAR const char *name,
|
|
|
|
FAR const char *dir, FAR const char *shell,
|
|
|
|
FAR struct passwd *pwd, FAR char *buf, size_t buflen,
|
|
|
|
FAR struct passwd **result)
|
|
|
|
{
|
|
|
|
size_t reqdlen;
|
|
|
|
|
|
|
|
reqdlen = strlen(name) + 1 + strlen(dir) + 1 + strlen(shell) + 1;
|
|
|
|
|
|
|
|
if (buflen < reqdlen)
|
|
|
|
{
|
|
|
|
/* Insufficient buffer space supplied. */
|
|
|
|
|
|
|
|
*result = NULL;
|
|
|
|
return ERANGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
pwd->pw_name = buf;
|
|
|
|
pwd->pw_dir = &buf[strlen(name) + 1];
|
|
|
|
pwd->pw_shell = &buf[strlen(name) + 1 + strlen(dir) + 1];
|
|
|
|
|
|
|
|
pwd->pw_uid = uid;
|
|
|
|
pwd->pw_gid = gid;
|
|
|
|
strcpy(pwd->pw_name, name);
|
|
|
|
strcpy(pwd->pw_dir, dir);
|
|
|
|
strcpy(pwd->pw_shell, shell);
|
|
|
|
|
|
|
|
*result = pwd;
|
|
|
|
return 0;
|
|
|
|
}
|