ReactOS  0.4.14-dev-98-gb0d4763
netnamer.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009, Sun Microsystems, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * - Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * - Neither the name of Sun Microsystems, Inc. nor the names of its
13  * contributors may be used to endorse or promote products derived
14  * from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * netname utility routines convert from unix names to network names and
31  * vice-versa This module is operating system dependent! What we define here
32  * will work with any unix system that has adopted the sun NIS domain
33  * architecture.
34  */
35 #include <wintirpc.h>
36 #ifndef _WIN32
37 #include <sys/param.h>
38 #include <rpc/rpc.h>
39 #include "rpc_com.h"
40 #ifdef YP
41 #include <rpcsvc/yp_prot.h>
42 #include <rpcsvc/ypclnt.h>
43 #endif
44 #include <ctype.h>
45 #include <stdio.h>
46 #include <grp.h>
47 #include <pwd.h>
48 #include <string.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 
52 static char *OPSYS = "unix";
53 static char *NETID = "netid.byname";
54 static char *NETIDFILE = "/etc/netid";
55 
56 static int getnetid( char *, char * );
57 static int _getgroups( char *, gid_t * );
58 
59 #ifndef NGROUPS
60 #define NGROUPS 16
61 #endif
62 
63 /*
64  * Convert network-name into unix credential
65  */
66 int
67 netname2user(netname, uidp, gidp, gidlenp, gidlist)
68  char netname[MAXNETNAMELEN + 1];
69  uid_t *uidp;
70  gid_t *gidp;
71  int *gidlenp;
72  gid_t *gidlist;
73 {
74  char *p;
75  int gidlen;
76  uid_t uid;
77  long luid;
78  struct passwd *pwd;
79  char val[1024];
80  char *val1, *val2;
81  char *domain;
82  int vallen;
83  int err;
84 
85  if (getnetid(netname, val)) {
86  char *res = val;
87 
88  p = strsep(&res, ":");
89  if (p == NULL)
90  return (0);
91  *uidp = (uid_t) atol(p);
92  p = strsep(&res, "\n,");
93  if (p == NULL) {
94  return (0);
95  }
96  *gidp = (gid_t) atol(p);
97  gidlen = 0;
98  for (gidlen = 0; gidlen < NGROUPS; gidlen++) {
99  p = strsep(&res, "\n,");
100  if (p == NULL)
101  break;
102  gidlist[gidlen] = (gid_t) atol(p);
103  }
104  *gidlenp = gidlen;
105 
106  return (1);
107  }
108  val1 = strchr(netname, '.');
109  if (val1 == NULL)
110  return (0);
111  if (strncmp(netname, OPSYS, (val1-netname)))
112  return (0);
113  val1++;
114  val2 = strchr(val1, '@');
115  if (val2 == NULL)
116  return (0);
117  vallen = val2 - val1;
118  if (vallen > (1024 - 1))
119  vallen = 1024 - 1;
120  (void) strncpy(val, val1, 1024);
121  val[vallen] = 0;
122 
123  err = __rpc_get_default_domain(&domain); /* change to rpc */
124  if (err)
125  return (0);
126 
127  if (strcmp(val2 + 1, domain))
128  return (0); /* wrong domain */
129 
130  if (sscanf(val, "%ld", &luid) != 1)
131  return (0);
132  uid = luid;
133 
134  /* use initgroups method */
135  pwd = getpwuid(uid);
136  if (pwd == NULL)
137  return (0);
138  *uidp = pwd->pw_uid;
139  *gidp = pwd->pw_gid;
140  *gidlenp = _getgroups(pwd->pw_name, gidlist);
141  return (1);
142 }
143 
144 /*
145  * initgroups
146  */
147 
148 static int
150  char *uname;
152 {
153  gid_t ngroups = 0;
154  struct group *grp;
155  int i;
156  int j;
157  int filter;
158 
159  setgrent();
160  while ((grp = getgrent())) {
161  for (i = 0; grp->gr_mem[i]; i++)
162  if (!strcmp(grp->gr_mem[i], uname)) {
163  if (ngroups == NGROUPS) {
164 #ifdef DEBUG
165  fprintf(stderr,
166  "initgroups: %s is in too many groups\n", uname);
167 #endif
168  goto toomany;
169  }
170  /* filter out duplicate group entries */
171  filter = 0;
172  for (j = 0; j < ngroups; j++)
173  if (groups[j] == grp->gr_gid) {
174  filter++;
175  break;
176  }
177  if (!filter)
178  groups[ngroups++] = grp->gr_gid;
179  }
180  }
181 toomany:
182  endgrent();
183  return (ngroups);
184 }
185 
186 /*
187  * Convert network-name to hostname
188  */
189 int
190 netname2host(netname, hostname, hostlen)
191  char netname[MAXNETNAMELEN + 1];
192  char *hostname;
193  int hostlen;
194 {
195  int err;
196  char valbuf[1024];
197  char *val;
198  char *val2;
199  int vallen;
200  char *domain;
201 
202  if (getnetid(netname, valbuf)) {
203  val = valbuf;
204  if ((*val == '0') && (val[1] == ':')) {
205  (void) strncpy(hostname, val + 2, hostlen);
206  return (1);
207  }
208  }
209  val = strchr(netname, '.');
210  if (val == NULL)
211  return (0);
212  if (strncmp(netname, OPSYS, (val - netname)))
213  return (0);
214  val++;
215  val2 = strchr(val, '@');
216  if (val2 == NULL)
217  return (0);
218  vallen = val2 - val;
219  if (vallen > (hostlen - 1))
220  vallen = hostlen - 1;
221  (void) strncpy(hostname, val, vallen);
222  hostname[vallen] = 0;
223 
224  err = __rpc_get_default_domain(&domain); /* change to rpc */
225  if (err)
226  return (0);
227 
228  if (strcmp(val2 + 1, domain))
229  return (0); /* wrong domain */
230  else
231  return (1);
232 }
233 
234 /*
235  * reads the file /etc/netid looking for a + to optionally go to the
236  * network information service.
237  */
238 int
240  char *key, *ret;
241 {
242  char buf[1024]; /* big enough */
243  char *res;
244  char *mkey;
245  char *mval;
246  FILE *fd;
247 #ifdef YP
248  char *domain;
249  int err;
250  char *lookup;
251  int len;
252 #endif
253 
254  fd = fopen(NETIDFILE, "r");
255  if (fd == NULL) {
256 #ifdef YP
257  res = "+";
258  goto getnetidyp;
259 #else
260  return (0);
261 #endif
262  }
263  for (;;) {
264  if (fd == NULL)
265  return (0); /* getnetidyp brings us here */
266  res = fgets(buf, sizeof(buf), fd);
267  if (res == NULL) {
268  fclose(fd);
269  return (0);
270  }
271  if (res[0] == '#')
272  continue;
273  else if (res[0] == '+') {
274 #ifdef YP
275  getnetidyp:
276  err = yp_get_default_domain(&domain);
277  if (err) {
278  continue;
279  }
280  lookup = NULL;
281  err = yp_match(domain, NETID, key,
282  strlen(key), &lookup, &len);
283  if (err) {
284 #ifdef DEBUG
285  fprintf(stderr, "match failed error %d\n", err);
286 #endif
287  continue;
288  }
289  lookup[len] = 0;
290  strcpy(ret, lookup);
291  free(lookup);
292  if (fd != NULL)
293  fclose(fd);
294  return (2);
295 #else /* YP */
296 #ifdef DEBUG
297  fprintf(stderr,
298 "Bad record in %s '+' -- NIS not supported in this library copy\n",
299  NETIDFILE);
300 #endif
301  continue;
302 #endif /* YP */
303  } else {
304  mkey = strsep(&res, "\t ");
305  if (mkey == NULL) {
306  fprintf(stderr,
307  "Bad record in %s -- %s", NETIDFILE, buf);
308  continue;
309  }
310  do {
311  mval = strsep(&res, " \t#\n");
312  } while (mval != NULL && !*mval);
313  if (mval == NULL) {
314  fprintf(stderr,
315  "Bad record in %s val problem - %s", NETIDFILE, buf);
316  continue;
317  }
318  if (strcmp(mkey, key) == 0) {
319  strcpy(ret, mval);
320  fclose(fd);
321  return (1);
322 
323  }
324  }
325  }
326 }
327 #endif /* !_WIN32 */
static int getnetid(char *, char *)
Definition: netnamer.c:239
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
static char * NETIDFILE
Definition: netnamer.c:54
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
int __rpc_get_default_domain(char **)
Definition: rpcdname.c:72
char * strncpy(char *DstString, const char *SrcString, ACPI_SIZE Count)
Definition: utclib.c:427
#define free
Definition: debug_ros.c:5
int netname2host(netname, char *hostname, int hostlen)
Definition: netnamer.c:190
static ULONG lookup[16]
Definition: vga.c:46
static char * OPSYS
Definition: netnamer.c:52
static int fd
Definition: io.c:51
long uid_t
Definition: various.h:8
char * hostname
Definition: ftp.c:88
static int _getgroups(char *, gid_t *)
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
GLsizei GLuint * groups
Definition: glext.h:11113
void pwd(int argc, const char *argv[])
Definition: cmds.c:1401
UINT gid_t
Definition: types.h:89
#define NGROUPS
Definition: netnamer.c:60
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
smooth NULL
Definition: ftsmooth.c:416
#define gid_t
Definition: types.h:67
_Check_return_ _CRTIMP int __cdecl sscanf(_In_z_ const char *_Src, _In_z_ _Scanf_format_string_ const char *_Format,...)
GLuint GLfloat * val
Definition: glext.h:7180
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint j
Definition: glfuncs.h:250
#define uid_t
Definition: types.h:66
GLboolean GLuint group
Definition: glext.h:11120
int strncmp(const char *String1, const char *String2, ACPI_SIZE Count)
Definition: utclib.c:534
_Check_return_ long __cdecl atol(_In_z_ const char *_Str)
int ret
HKEY key
Definition: reg.c:42
GLenum GLsizei len
Definition: glext.h:6722
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
#define err(...)
#define MAXNETNAMELEN
Definition: auth.h:78
_Check_return_opt_ _CRTIMP char *__cdecl fgets(_Out_writes_z_(_MaxCount) char *_Buf, _In_ int _MaxCount, _Inout_ FILE *_File)
char * strchr(const char *String, int ch)
Definition: utclib.c:501
GLuint res
Definition: glext.h:9613
static char * NETID
Definition: netnamer.c:53
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
FILE * stderr
int netname2user(netname, uid_t *uidp, gid_t *gidp, int *gidlenp, gid_t *gidlist)
Definition: netnamer.c:67
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
GLfloat GLfloat p
Definition: glext.h:8902
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: glext.h:7005
Definition: path.c:42