summaryrefslogtreecommitdiff
path: root/src/basic/user-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2020-08-06 16:46:18 +0200
committerLennart Poettering <lennart@poettering.net>2020-08-07 17:36:11 +0200
commitb10fd796f56e4f16f7430cd22f59f544766d3bef (patch)
treea739f9bf0971bbac2bfd3b87fcc7fd8e21263f52 /src/basic/user-util.c
parentc1c28fe2f70573270b0544670dba38b3a2f06c13 (diff)
downloadsystemd-b10fd796f56e4f16f7430cd22f59f544766d3bef.tar.gz
user-util: add mangle_gecos() call for turning strings into fields suitable as GECOS fields
Diffstat (limited to 'src/basic/user-util.c')
-rw-r--r--src/basic/user-util.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/basic/user-util.c b/src/basic/user-util.c
index 8115065b5e..0e96a75797 100644
--- a/src/basic/user-util.c
+++ b/src/basic/user-util.c
@@ -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. */