user-util: add mangle_gecos() call for turning strings into fields suitable as GECOS fields

This commit is contained in:
Lennart Poettering
2020-08-07 17:36:11 +02:00
parent c1c28fe2f7
commit b10fd796f5
3 changed files with 52 additions and 0 deletions
+31
View File
@@ -863,6 +863,37 @@ bool valid_gecos(const char *d) {
return true;
}
char *mangle_gecos(const char *d) {
char *mangled;
/* Makes sure the provided string becomes valid as a GEGOS field, by dropping bad chars. glibc's
* putwent() only changes \n and : to spaces. We do more: replace all CC too, and remove invalid
* UTF-8 */
mangled = strdup(d);
if (!mangled)
return NULL;
for (char *i = mangled; *i; i++) {
int len;
if ((uint8_t) *i < (uint8_t) ' ' || *i == ':') {
*i = ' ';
continue;
}
len = utf8_encoded_valid_unichar(i, (size_t) -1);
if (len < 0) {
*i = ' ';
continue;
}
i += len - 1;
}
return mangled;
}
bool valid_home(const char *p) {
/* Note that this function is also called by valid_shell(), any
* changes must account for that. */