File: getuser.c

package info (click to toggle)
lcgdm 1.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 14,044 kB
  • sloc: ansic: 149,126; sh: 13,441; perl: 11,498; python: 5,778; cpp: 5,113; sql: 1,805; makefile: 1,388; fortran: 113
file content (154 lines) | stat: -rw-r--r-- 3,953 bytes parent folder | download | duplicates (8)
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
/*
 * Copyright (C) 1994-2003 by CERN/IT/ADC/CA
 * All rights reserved
 */

#ifndef lint
static char sccsid[] = "@(#)$RCSfile: getuser.c,v $ $Revision: 1.1 $ $Date: 2005/03/29 09:27:19 $ CERN IT/ADC/CA Jean-Damien Durand";
#endif /* not lint */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <pwd.h>
#include <Castor_limits.h>
#include <Cglobals.h>
#include <Cpwd.h>
#include <serrno.h>
#include <log.h>

#if defined(_WIN32)
char *getenv();
#endif

#ifndef MAPPING_FILE
#if defined(_WIN32)
#define MAPPING_FILE "%SystemRoot%\\system32\\drivers\\etc\\users.ext"
#else
#define MAPPING_FILE "/etc/ext.users"
#endif /* _WIN32 */
#endif  

#if defined(_WIN32)
/*
 * infile will be modified at runtime when %SystemRoot% is
 * resolved. Must reserv enough space to hold new pathname.
 */
static char infile[CA_MAXPATHLEN+1] = MAPPING_FILE;
#else
static char *infile = MAPPING_FILE;
#endif  /* WIN32 */


#ifndef _WIN32
#if defined(_REENTRANT) || defined(_THREAD_SAFE)
#define strtok(X,Y) strtok_r(X,Y,&last)
#endif /* _REENTRANT || _THREAD_SAFE */
#endif

/*
 * function finds the corresponding entry in the
 * mapping table;
 * returns 0 if entry was found
 *         1 on error (errno is set)
 *         -1 if entry was not found
 */

int DLL_DECL get_user(from_node,from_user,from_uid,from_gid,to_user,to_uid,to_gid)
char *from_node;
char *from_user;
int from_uid;
int from_gid;
char *to_user;
int *to_uid ;
int *to_gid ;
{
    char *p ;
    struct passwd *pw ;
    FILE *fsin ;
    int uid,gid ;
    char buf[BUFSIZ+1] ;
    char maphostnam[CA_MAXHOSTNAMELEN+1];
    char mapuser[CA_MAXUSRNAMELEN+1] ;
    char mapuid[6] ;
    char mapgid[6] ;
    int counter ;
#if defined(_WIN32)
    char path[CA_MAXPATHLEN+1];
#endif
#if defined(_REENTRANT) || defined(_THREAD_SAFE)
    char *last = NULL;
#endif /* _REENTRANT || _THREAD_SAFE */

#if defined(_WIN32)
    strcpy(path, infile);
    if( (strncmp(path, "%SystemRoot%\\", 13) == 0) && ((p = getenv ("SystemRoot")) != NULL) ) {
        sprintf(infile, "%s\\%s", p, strchr (path, '\\'));
    }
#endif
   
    if  ( (fsin=fopen(infile,"r"))==NULL ) {
       log(LOG_ERR, "Could not open file %s, errno %d\n", infile, errno);
       serrno = ENOENT;
       return -ENOENT ;
    }

    while( fgets(buf,BUFSIZ,fsin)!=NULL ) {
        if ( buf[0] == '#') continue ;
        if ( ( p = strrchr( buf,'\n') ) != NULL ) *p='\0' ;

        log(LOG_DEBUG,"Entry >%s< in %s\n",buf,infile) ;

        p = strtok( buf, " \t");
        if ( p != NULL ) strcpy (maphostnam, p) ;
        else continue ;

        p = strtok( NULL, " \t");
        if ( p != NULL ) strcpy (mapuser, p) ;
        else continue ;

        p = strtok( NULL, " \t");
        if ( p != NULL ) strcpy (mapuid, p);
        else continue ;

        p = strtok( NULL, " \t");
        if ( p != NULL ) strcpy (mapgid, p);
        else continue ;

        p = strtok( NULL, " \t");
        if ( p != NULL ) strcpy (to_user,p);
        else continue ;

        /* Checking maphostnam */
        counter = 0 ;
        if ( strcmp( maphostnam, from_node) && 
            !( (p=strchr(maphostnam,'*'))!= NULL && strstr(from_node,p+1) != NULL ) )
            continue ;

        if ( strcmp( mapuser , from_user ) && strcmp( mapuser ,"*" ) )
            continue ;

        if ( strcmp( mapuid, "*" ) && ( (uid=atoi(mapuid)) <= 0 || uid != from_uid ) ) 
            continue ;

        if ( strcmp( mapgid, "*" ) && ( (gid=atoi(mapgid)) <= 0 || gid != from_gid ) )
            continue ;

        log( LOG_DEBUG,"Found a possible entry: %s\n",to_user);

        if ( (pw = Cgetpwnam(to_user))==NULL) {
            log(LOG_INFO,"Cgetpwnam(): %s\n",sstrerror(serrno));
            continue ;
        } else {
            *to_uid = pw->pw_uid;
            *to_gid = pw->pw_gid;
        }
        fclose( fsin ) ;
        return 0 ;
    }
    fclose( fsin ) ;
    serrno = ENOENT;
    return -1 ;
}