summaryrefslogtreecommitdiffstats
path: root/src/util/wshelper/hespwnam.c
blob: 55ddf01c1bc6486ec48cfb786c90c3e9991a3a6c (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 *	@doc HESIOD
 *
 * @module hespwnam.c |
 *
 * This file contains hes_getpwnam, for retrieving passwd information about
 * a user.
 *
 * For copying and distribution information, see the file
 * <lt> mit-copyright.h <gt>
 *
 * Original version by Steve Dyer, IBM/Project Athena.
 *
 *	  WSHelper DNS/Hesiod Library for WINSOCK
 *
 *
 */

/* This file contains hes_getpwnam, for retrieving passwd information about
 * a user.
 *
 * For copying and distribution information, see the file <mit-copyright.h>
 *
 * Original version by Steve Dyer, IBM/Project Athena.
 *
 */

#include <stdio.h>
#include <string.h> /*s*/

#include <stdlib.h>

#include <windows.h>
#include <hesiod.h>

#include "pwd.h"

extern DWORD dwHesPwNamIndex;
extern DWORD dwHesPwUidIndex;

#define MAX_PW_BUFFER_LENGTH  64

static char *
_NextPWField(char *ptr);

struct passwd *  GetPasswdStruct(struct passwd* pw, char* buf);




/*
	Given a UID this function will return the pwd information, eg username, uid,
	gid, fullname, office location, phone number, home directory, and default shell

	defined in hespwnam.c
	\param	uid			The user ID
	\retval				NULL if there was an error or a pointer to the passwd structure. The caller must
						never attempt to modify this structure or to free any of its components.
						Furthermore, only one copy of this structure is allocated per call per thread, so the application should copy any information it needs before
						issuing another hes_getpwuid call
*/
struct passwd *
WINAPI
hes_getpwuid(int uid)
{
    char **pp;
    struct passwd* pw = NULL;
    char buf[256];

    char nam[8];
    sprintf(nam, "%d", uid);

    pp = hes_resolve(nam, "uid");
    if (pp == NULL || *pp == NULL)
        return(NULL);

    pw = (struct passwd*)(TlsGetValue(dwHesPwUidIndex));
    if (pw == NULL) {
	LPVOID lpvData = (LPVOID) LocalAlloc(LPTR, sizeof(struct passwd));
	if (lpvData != NULL) {
	    TlsSetValue(dwHesPwUidIndex, lpvData);
	    pw = (struct passwd*)lpvData;
	} else
	    return NULL;
    }

    strcpy(buf, pp[0]);
    hes_free(pp);
    return GetPasswdStruct(pw, buf);
}


/*
	Given a username this function will return the pwd information, eg
	username, uid, gid, fullname, office location, phone number, home
	directory, and default shell

	defined in hespwnam.c

	\param	nam			a pointer to the username

	\retval				NULL if there was an error or a pointer to the passwd structure. The caller must
						never attempt to modify this structure or to free any of its components.
						Furthermore, only one copy of this structure is allocated per call per thread, so the application should copy any information it needs before
						issuing another hes_getpwnam call

*/
struct passwd *
WINAPI
hes_getpwnam(char *nam)
{

   char **pp;
   struct passwd* pw = NULL;
   char buf[256];

    pp = hes_resolve(nam, "passwd");
    if (pp == NULL || *pp == NULL)
        return(NULL);

    pw = (struct passwd*)(TlsGetValue(dwHesPwNamIndex));
    if (pw == NULL) {
	LPVOID lpvData = (LPVOID) LocalAlloc(LPTR, sizeof(struct passwd));
	if (lpvData != NULL) {
	    TlsSetValue(dwHesPwNamIndex, lpvData);
	    pw = (struct passwd*)lpvData;
	} else
	    return NULL;
    }

    strcpy(buf, pp[0]);
    hes_free(pp);
    return GetPasswdStruct(pw, buf);
}


struct passwd*  GetPasswdStruct(struct passwd* pw, char* buf)
{
    char* temp;
    char* p;

    if (pw->pw_name == NULL)
	pw->pw_name = LocalAlloc(LPTR, MAX_PW_BUFFER_LENGTH);
    if (pw->pw_passwd == NULL)
	pw->pw_passwd = LocalAlloc(LPTR, MAX_PW_BUFFER_LENGTH);
    if (pw->pw_comment == NULL)
	pw->pw_comment = LocalAlloc(LPTR, MAX_PW_BUFFER_LENGTH);
    if (pw->pw_gecos == NULL)
	pw->pw_gecos = LocalAlloc(LPTR, MAX_PW_BUFFER_LENGTH);
    if (pw->pw_dir == NULL)
	pw->pw_dir = LocalAlloc(LPTR, MAX_PW_BUFFER_LENGTH);
    if (pw->pw_shell == NULL)
	pw->pw_shell = LocalAlloc(LPTR, MAX_PW_BUFFER_LENGTH);
    /* choose only the first response (only 1 expected) */
    p = buf;
    temp = p;
    p = _NextPWField(p);
    strcpy(pw->pw_name, temp);
    temp = p;
    p = _NextPWField(p);
    strcpy(pw->pw_passwd, temp);
    pw->pw_uid = atoi(p);
    p = _NextPWField(p);
    pw->pw_gid = atoi(p);
    pw->pw_quota = 0;
    strcpy(pw->pw_comment, "");
    p = _NextPWField(p);
    temp = p;
    p = _NextPWField(p);
    strcpy(pw->pw_gecos, temp);
    temp = p;
    p = _NextPWField(p);
    strcpy(pw->pw_dir,  temp);
    temp = p;
    while (*p && *p != '\n')
        p++;
    *p = '\0';
    strcpy(pw->pw_shell, temp);
    return pw;


}

/* Move the pointer forward to the next colon-separated field in the
 * password entry.
 */

static char *
_NextPWField(char *ptr)
{
    while (*ptr && *ptr != '\n' && *ptr != ':')
        ptr++;
    if (*ptr)
        *ptr++ = '\0';
    return(ptr);
}