/* Search in the password database for a user id that matches the user name
* `name`. Returns 0 on success, -1 on failure or 1 if name cannot be found.
+ *
+ * Warns if @missing_ok is false
*/
static int
-virGetUserIDByName(const char *name, uid_t *uid)
+virGetUserIDByName(const char *name, uid_t *uid, bool missing_ok)
{
char *strbuf = NULL;
struct passwd pwbuf;
}
if (!pw) {
- if (rc != 0) {
+ if (rc != 0 && !missing_ok) {
char buf[1024];
/* log the possible error from getpwnam_r. Unfortunately error
* reporting from this function is bad and we can't really
goto cleanup;
}
- *uid = pw->pw_uid;
+ if (uid)
+ *uid = pw->pw_uid;
ret = 0;
cleanup:
if (*user == '+') {
user++;
} else {
- int rc = virGetUserIDByName(user, uid);
+ int rc = virGetUserIDByName(user, uid, false);
if (rc <= 0)
return rc;
}
/* Search in the group database for a group id that matches the group name
* `name`. Returns 0 on success, -1 on failure or 1 if name cannot be found.
+ *
+ * Warns if @missing_ok is false
*/
static int
-virGetGroupIDByName(const char *name, gid_t *gid)
+virGetGroupIDByName(const char *name, gid_t *gid, bool missing_ok)
{
char *strbuf = NULL;
struct group grbuf;
}
if (!gr) {
- if (rc != 0) {
+ if (rc != 0 && !missing_ok) {
char buf[1024];
/* log the possible error from getgrnam_r. Unfortunately error
* reporting from this function is bad and we can't really
goto cleanup;
}
- *gid = gr->gr_gid;
+ if (gid)
+ *gid = gr->gr_gid;
ret = 0;
cleanup:
if (*group == '+') {
group++;
} else {
- int rc = virGetGroupIDByName(group, gid);
+ int rc = virGetGroupIDByName(group, gid, false);
if (rc <= 0)
return rc;
}
return 0;
}
+/* Silently checks if User @name exists.
+ * Returns if the user exists and fallbacks to false on error.
+ */
+int
+virDoesUserExist(const char *name)
+{
+ return virGetUserIDByName(name, NULL, true) == 0;
+}
+
+/* Silently checks if Group @name exists.
+ * Returns if the group exists and fallbacks to false on error.
+ */
+int
+virDoesGroupExist(const char *name)
+{
+ return virGetGroupIDByName(name, NULL, true) == 0;
+}
+
/* Compute the list of primary and supplementary groups associated
* with @uid, and including @gid in the list (unless it is -1),