summaryrefslogtreecommitdiff
path: root/src/pwd/getpwnam.c
blob: f5b1b97912ce17abe19b8077a482855561031fe6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "sys/types.h"
#include <pwd.h>
#include "stddef.h"
#include "stdio.h"
#include "limits.h"
#include "string.h"
#include "_config.h"

#ifndef LINE_MAX
#define LINE_MAX _POSIX2_LINE_MAX
#endif

struct passwd * getpwnam(const char * name)
{
	static char buf[LINE_MAX + 1];
	static struct passwd pwd = { 0 };
	char *user, *password, *uid, *gid, *gecos, *home, *shell, *nl;

	/* TODO: attempt calling first _PWD_CMD */

	FILE *db = fopen(_PWD_DB, "r");
	if (db == NULL) {
		return NULL;
	}

	while (fgets(buf, sizeof(buf), db) != NULL) {
		user = buf;
		if ((password = strchr(buf, ':')) != NULL) {
			*password = '\0';
			password++;
		} else {
			continue;
		}

		if (strcmp(user, name) != 0) {
			continue;
		}

		if ((uid = strchr(password, ':')) != NULL) {
			*uid = '\0';
			uid++;
		} else {
			continue;
		}

		if ((gid = strchr(uid, ':')) != NULL) {
			*gid = '\0';
			gid++;
		} else {
			continue;
		}

		if ((gecos = strchr(gid, ':')) != NULL) {
			*gecos = '\0';
			gecos++;
		} else {
			continue;
		}

		if ((home = strchr(gecos, ':')) != NULL) {
			*home = '\0';
			home++;
		} else {
			continue;
		}

		if ((shell = strchr(home, ':')) != NULL) {
			*shell = '\0';
			shell++;
			if ((nl = strchr(shell, '\n')) != NULL) {
				*nl = '\0';
			}
		} else {
			continue;
		}

		pwd.pw_name = user;
		pwd.pw_uid = strtoul(gid, NULL, 10);
		pwd.pw_gid = strtoul(gid, NULL, 10);
		pwd.pw_dir = home;
		pwd.pw_shell = shell;
		break;
	}

	fclose(db);

	if (strcmp(name, pwd.pw_name) == 0) {
		return &pwd;
	}

	return NULL;
}

/*
POSIX(1)
*/