<commit_before>// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#define _GNU_SOURCE
#include <errno.h>
#include <pwd.h>
#include <grp.h>
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include "user_group_lookup.h"

#define STACK_SIZE (64 * 1024)

/* This is all a bit copy-and-pasty from update-ssh-keys/authorized_keys_d,
 * TODO(vc): refactor authorized_keys_d a bit so external packages can reuse
 * the pieces duplicated here.
 */
typedef struct lookup_ctxt {
	void			*stack;

	const char		*name;
	const char		*root;

	lookup_res_t	*res;
	int			ret;
	int			err;
} lookup_ctxt_t;

enum lookup_type {
	LOOKUP_TYPE_USER,
	LOOKUP_TYPE_GROUP
};


static int user_lookup_fn(lookup_ctxt_t *ctxt) {
	char		buf[16 * 1024];
	struct passwd	p, *pptr;

	if(chroot(ctxt->root) == -1) {
		goto out_err;
	}

	if (chdir("/") == -1) {
		goto out_err;
	}

	if(getpwnam_r(ctxt->name, &p, buf, sizeof(buf), &pptr) != 0) {
		goto out_err;
	}

	if (!pptr) {
		// successfully found nothing
		ctxt->res->name = NULL;
		return 0;
	}

	if(!(ctxt->res->name = strdup(p.pw_name))) {
		goto out_err;
	}

	if(!(ctxt->res->home = strdup(p.pw_dir))) {
		free(ctxt->res->name);
		goto out_err;
	}

	ctxt->res->uid = p.pw_uid;
	ctxt->res->gid = p.pw_gid;

	return 0;

out_err:
	ctxt->err = errno;
	ctxt->ret = -1;
	return 0;
}

static int group_lookup_fn(lookup_ctxt_t *ctxt) {
	char buf[16 * 1024];
	struct group g, *gptr;

	if(chroot(ctxt->root) == -1) {
		goto out_err;
	}

	if (chdir("/") == -1) {
		goto out_err;
	}

	if(getgrnam_r(ctxt->name, &g, buf, sizeof(buf), &gptr) != 0) {
		goto out_err;
	}

	if (!gptr) {
		// successfully found nothing
		ctxt->res->name = NULL;
		return 0;<commit_msg>util/user_group_lookup: handle more error codes<commit_after>// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#define _GNU_SOURCE
#include <errno.h>
#include <pwd.h>
#include <grp.h>
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include "user_group_lookup.h"

#define STACK_SIZE (64 * 1024)

/* This is all a bit copy-and-pasty from update-ssh-keys/authorized_keys_d,
 * TODO(vc): refactor authorized_keys_d a bit so external packages can reuse
 * the pieces duplicated here.
 */
typedef struct lookup_ctxt {
	void			*stack;

	const char		*name;
	const char		*root;

	lookup_res_t	*res;
	int			ret;
	int			err;
} lookup_ctxt_t;

enum lookup_type {
	LOOKUP_TYPE_USER,
	LOOKUP_TYPE_GROUP
};


static int user_lookup_fn(lookup_ctxt_t *ctxt) {
	char		buf[16 * 1024];
	struct passwd	p, *pptr;

	if(chroot(ctxt->root) == -1) {
		goto out_err;
	}

	if (chdir("/") == -1) {
		goto out_err;
	}

	if(getpwnam_r(ctxt->name, &p, buf, sizeof(buf), &pptr) != 0) {
		if (errno != ENOENT && errno != ESRCH) {
			goto out_err;
		}
		errno = 0; /* fallthrough and treat as "not found" */
	}

	if (!pptr) {
		// successfully found nothing
		ctxt->res->name = NULL;
		return 0;
	}

	if(!(ctxt->res->name = strdup(p.pw_name))) {
		goto out_err;
	}

	if(!(ctxt->res->home = strdup(p.pw_dir))) {
		free(ctxt->res->name);
		goto out_err;
	}

	ctxt->res->uid = p.pw_uid;
	ctxt->res->gid = p.pw_gid;

	return 0;

out_err:
	ctxt->err = errno;
	ctxt->ret = -1;
	return 0;
}

static int group_lookup_fn(lookup_ctxt_t *ctxt) {
	char buf[16 * 1024];
	struct group g, *gptr;

	if(chroot(ctxt->root) == -1) {
		goto out_err;
	}

	if (chdir("/") == -1) {
		goto out_err;
	}

	if(getgrnam_r(ctxt->name, &g, buf, sizeof(buf), &gptr) != 0) {
		if (errno != ENOENT && errno != ESRCH) {
			goto out_err;
		}
		errno = 0; /* fallthrough and treat as "not found" */
	}

	if (!gptr) {
		// successfully found nothing
		ctxt->res->name = NULL;
		return 0;
{'fraction_non_alphanumeric': 0.14967637540453074, 'fraction_numerical': 0.015372168284789644, 'mean_word_length': 3.4872958257713247, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6186620', 'n_tokens_mistral': 2022, 'n_tokens_neox': 1906, 'n_words': 539}