On 10/22/2014 02:17 PM, Jakub Hrozek wrote:
+errno_t sss_user_by_name_or_uid(const char *input, uid_t *_uid,
gid_t *_gid)
+{
+ uid_t uid;
+ errno_t ret;
+ char *endptr;
+ struct passwd *pwd;
+
+ /* Try if it's an ID first */
+ errno = 0;
+ uid = strtouint32(input, &endptr, 10);
+ if (errno != 0 || *endptr != '\0') {
+ ret = errno;
+ if (ret == ERANGE) {
+ DEBUG(SSSDBG_OP_FAILURE,
+ "UID [%s] is out of range.\n", input);
+ return ret;
+ }
+
+ /* Nope, maybe a username? */
+ pwd = getpwnam(input);
+ } else {
+ pwd = getpwuid(uid);
+ }
+
+ if (pwd == NULL) {
+ DEBUG(SSSDBG_OP_FAILURE,
+ "[%s] is neither a valid UID nor a user name which could be "
+ "resolved by getpwnam().\n", input);
+ return EINVAL;
+ }
+
+ if (_uid) {
+ *_uid = pwd->pw_uid;
+ }
+
+ if (_gid) {
+ *_gid = pwd->pw_gid;
+ }
+ return EOK;
+}
Hello, sorry for opening an old thread.
I was just wondering if we support user names that are completely
numerical - like 111 or 555?
This function would fail for such users if we added added test like this:
src/tests/cwrap/passwd
111:x:222:444:User whose name is numerical:/home/111:/bin/bash
src/tests/cwrap/test_usertools.c
+void test_get_user_with_numerical_username(void **state)
+{
+ uid_t uid;
+ gid_t gid;
+ errno_t ret;
+
+ ret = sss_user_by_name_or_uid("111", &uid, &gid);
+ assert_int_equal(ret, EOK);
+ assert_int_equal(uid, 222);
+ assert_int_equal(gid, 444);
+}
Fixing sss_user_by_name_or_uid() is a simple matter and I'm happy to do
it, should I?