~ubuntu-branches/ubuntu/precise/krb5/precise-updates

« back to all changes in this revision

Viewing changes to src/util/wshelper/hespwnam.c

  • Committer: Package Import Robot
  • Author(s): Sam Hartman
  • Date: 2011-12-01 19:34:41 UTC
  • mfrom: (28.1.14 sid)
  • Revision ID: package-import@ubuntu.com-20111201193441-9tipg3aru1jsidyv
Tags: 1.10+dfsg~alpha1-6
* Fix segfault with unknown hostnames in krb5_sname_to_principal,
  Closes: #650671
* Indicate that this library breaks libsmbclient versions that depend on
  krb5_locate_kdc, Closes: #650603, #650611

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *      @doc HESIOD
 
3
 *
 
4
 * @module hespwnam.c |
 
5
 *
 
6
 * This file contains hes_getpwnam, for retrieving passwd information about
 
7
 * a user.
 
8
 *
 
9
 * For copying and distribution information, see the file
 
10
 * <lt> mit-copyright.h <gt>
 
11
 *
 
12
 * Original version by Steve Dyer, IBM/Project Athena.
 
13
 *
 
14
 *        WSHelper DNS/Hesiod Library for WINSOCK
 
15
 *
 
16
 *
 
17
 */
 
18
 
 
19
/* This file contains hes_getpwnam, for retrieving passwd information about
 
20
 * a user.
 
21
 *
 
22
 * For copying and distribution information, see the file <mit-copyright.h>
 
23
 *
 
24
 * Original version by Steve Dyer, IBM/Project Athena.
 
25
 *
 
26
 */
 
27
 
 
28
#include <stdio.h>
 
29
#include <string.h> /*s*/
 
30
 
 
31
#include <stdlib.h>
 
32
 
 
33
#include <windows.h>
 
34
#include <hesiod.h>
 
35
 
 
36
#include "pwd.h"
 
37
 
 
38
extern DWORD dwHesPwNamIndex;
 
39
extern DWORD dwHesPwUidIndex;
 
40
 
 
41
#define MAX_PW_BUFFER_LENGTH  64
 
42
 
 
43
static char *
 
44
_NextPWField(char *ptr);
 
45
 
 
46
struct passwd *  GetPasswdStruct(struct passwd* pw, char* buf);
 
47
 
 
48
 
 
49
 
 
50
 
 
51
/*
 
52
        Given a UID this function will return the pwd information, eg username, uid,
 
53
        gid, fullname, office location, phone number, home directory, and default shell
 
54
 
 
55
        defined in hespwnam.c
 
56
        \param  uid                     The user ID
 
57
        \retval                         NULL if there was an error or a pointer to the passwd structure. The caller must
 
58
                                                never attempt to modify this structure or to free any of its components.
 
59
                                                Furthermore, only one copy of this structure is allocated per call per thread, so the application should copy any information it needs before
 
60
                                                issuing another hes_getpwuid call
 
61
*/
 
62
struct passwd *
 
63
WINAPI
 
64
hes_getpwuid(int uid)
 
65
{
 
66
    char **pp;
 
67
    struct passwd* pw = NULL;
 
68
    char buf[256];
 
69
 
 
70
    char nam[8];
 
71
    sprintf(nam, "%d", uid);
 
72
 
 
73
    pp = hes_resolve(nam, "uid");
 
74
    if (pp == NULL || *pp == NULL)
 
75
        return(NULL);
 
76
 
 
77
    pw = (struct passwd*)(TlsGetValue(dwHesPwUidIndex));
 
78
    if (pw == NULL) {
 
79
        LPVOID lpvData = (LPVOID) LocalAlloc(LPTR, sizeof(struct passwd));
 
80
        if (lpvData != NULL) {
 
81
            TlsSetValue(dwHesPwUidIndex, lpvData);
 
82
            pw = (struct passwd*)lpvData;
 
83
        } else
 
84
            return NULL;
 
85
    }
 
86
 
 
87
    strcpy(buf, pp[0]);
 
88
    hes_free(pp);
 
89
    return GetPasswdStruct(pw, buf);
 
90
}
 
91
 
 
92
 
 
93
/*
 
94
        Given a username this function will return the pwd information, eg
 
95
        username, uid, gid, fullname, office location, phone number, home
 
96
        directory, and default shell
 
97
 
 
98
        defined in hespwnam.c
 
99
 
 
100
        \param  nam                     a pointer to the username
 
101
 
 
102
        \retval                         NULL if there was an error or a pointer to the passwd structure. The caller must
 
103
                                                never attempt to modify this structure or to free any of its components.
 
104
                                                Furthermore, only one copy of this structure is allocated per call per thread, so the application should copy any information it needs before
 
105
                                                issuing another hes_getpwnam call
 
106
 
 
107
*/
 
108
struct passwd *
 
109
WINAPI
 
110
hes_getpwnam(char *nam)
 
111
{
 
112
 
 
113
   char **pp;
 
114
   struct passwd* pw = NULL;
 
115
   char buf[256];
 
116
 
 
117
    pp = hes_resolve(nam, "passwd");
 
118
    if (pp == NULL || *pp == NULL)
 
119
        return(NULL);
 
120
 
 
121
    pw = (struct passwd*)(TlsGetValue(dwHesPwNamIndex));
 
122
    if (pw == NULL) {
 
123
        LPVOID lpvData = (LPVOID) LocalAlloc(LPTR, sizeof(struct passwd));
 
124
        if (lpvData != NULL) {
 
125
            TlsSetValue(dwHesPwNamIndex, lpvData);
 
126
            pw = (struct passwd*)lpvData;
 
127
        } else
 
128
            return NULL;
 
129
    }
 
130
 
 
131
    strcpy(buf, pp[0]);
 
132
    hes_free(pp);
 
133
    return GetPasswdStruct(pw, buf);
 
134
}
 
135
 
 
136
 
 
137
struct passwd*  GetPasswdStruct(struct passwd* pw, char* buf)
 
138
{
 
139
    char* temp;
 
140
    char* p;
 
141
 
 
142
    if (pw->pw_name == NULL)
 
143
        pw->pw_name = LocalAlloc(LPTR, MAX_PW_BUFFER_LENGTH);
 
144
    if (pw->pw_passwd == NULL)
 
145
        pw->pw_passwd = LocalAlloc(LPTR, MAX_PW_BUFFER_LENGTH);
 
146
    if (pw->pw_comment == NULL)
 
147
        pw->pw_comment = LocalAlloc(LPTR, MAX_PW_BUFFER_LENGTH);
 
148
    if (pw->pw_gecos == NULL)
 
149
        pw->pw_gecos = LocalAlloc(LPTR, MAX_PW_BUFFER_LENGTH);
 
150
    if (pw->pw_dir == NULL)
 
151
        pw->pw_dir = LocalAlloc(LPTR, MAX_PW_BUFFER_LENGTH);
 
152
    if (pw->pw_shell == NULL)
 
153
        pw->pw_shell = LocalAlloc(LPTR, MAX_PW_BUFFER_LENGTH);
 
154
    /* choose only the first response (only 1 expected) */
 
155
    p = buf;
 
156
    temp = p;
 
157
    p = _NextPWField(p);
 
158
    strcpy(pw->pw_name, temp);
 
159
    temp = p;
 
160
    p = _NextPWField(p);
 
161
    strcpy(pw->pw_passwd, temp);
 
162
    pw->pw_uid = atoi(p);
 
163
    p = _NextPWField(p);
 
164
    pw->pw_gid = atoi(p);
 
165
    pw->pw_quota = 0;
 
166
    strcpy(pw->pw_comment, "");
 
167
    p = _NextPWField(p);
 
168
    temp = p;
 
169
    p = _NextPWField(p);
 
170
    strcpy(pw->pw_gecos, temp);
 
171
    temp = p;
 
172
    p = _NextPWField(p);
 
173
    strcpy(pw->pw_dir,  temp);
 
174
    temp = p;
 
175
    while (*p && *p != '\n')
 
176
        p++;
 
177
    *p = '\0';
 
178
    strcpy(pw->pw_shell, temp);
 
179
    return pw;
 
180
 
 
181
 
 
182
}
 
183
 
 
184
/* Move the pointer forward to the next colon-separated field in the
 
185
 * password entry.
 
186
 */
 
187
 
 
188
static char *
 
189
_NextPWField(char *ptr)
 
190
{
 
191
    while (*ptr && *ptr != '\n' && *ptr != ':')
 
192
        ptr++;
 
193
    if (*ptr)
 
194
        *ptr++ = '\0';
 
195
    return(ptr);
 
196
}