mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 06:47:35 +00:00
LibC: Implement putpwent()
It was previously impossible to (correctly) put an entry into the password file via libc. This patch implements the functionality to do so.
This commit is contained in:
parent
dfaa5ecf81
commit
5f78e2ff3a
2 changed files with 27 additions and 0 deletions
|
@ -151,4 +151,29 @@ next_entry:
|
||||||
strncpy(__pwdb_entry->shell_buffer, e_shell.characters(), PWDB_STR_MAX_LEN);
|
strncpy(__pwdb_entry->shell_buffer, e_shell.characters(), PWDB_STR_MAX_LEN);
|
||||||
return __pwdb_entry;
|
return __pwdb_entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int putpwent(const struct passwd* p, FILE* stream)
|
||||||
|
{
|
||||||
|
if (!p || !stream || !p->pw_name || !p->pw_dir || !p->pw_gecos || !p->pw_shell) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto is_valid_field = [](const char* str) {
|
||||||
|
return str && !strpbrk(str, ":\n");
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!is_valid_field(p->pw_name) || !is_valid_field(p->pw_dir) || !is_valid_field(p->pw_gecos) || !is_valid_field(p->pw_shell)) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int nwritten = fprintf(stream, "%s:x:%u:%u:%s,,,:%s:%s\n", p->pw_name, p->pw_uid, p->pw_gid, p->pw_gecos, p->pw_dir, p->pw_shell);
|
||||||
|
if (!nwritten || nwritten < 0) {
|
||||||
|
errno = ferror(stream);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <bits/FILE.h>
|
||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
@ -46,5 +47,6 @@ void setpwent();
|
||||||
void endpwent();
|
void endpwent();
|
||||||
struct passwd* getpwnam(const char* name);
|
struct passwd* getpwnam(const char* name);
|
||||||
struct passwd* getpwuid(uid_t);
|
struct passwd* getpwuid(uid_t);
|
||||||
|
int putpwent(const struct passwd* p, FILE* stream);
|
||||||
|
|
||||||
__END_DECLS
|
__END_DECLS
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue