File: getusershell.c

package info (click to toggle)
wu-ftpd 2.6.2-32
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,700 kB
  • ctags: 1,362
  • sloc: ansic: 18,125; sh: 3,535; yacc: 1,742; perl: 649; makefile: 228
file content (193 lines) | stat: -rw-r--r-- 4,054 bytes parent folder | download | duplicates (5)
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
/****************************************************************************  
 
  Copyright (c) 1999,2000 WU-FTPD Development Group.  
  All rights reserved.
  
  Portions Copyright (c) 1980, 1985, 1988, 1989, 1990, 1991, 1993, 1994
    The Regents of the University of California.
  Portions Copyright (c) 1993, 1994 Washington University in Saint Louis.
  Portions Copyright (c) 1996, 1998 Berkeley Software Design, Inc.
  Portions Copyright (c) 1989 Massachusetts Institute of Technology.
  Portions Copyright (c) 1998 Sendmail, Inc.
  Portions Copyright (c) 1983, 1995, 1996, 1997 Eric P.  Allman.
  Portions Copyright (c) 1997 by Stan Barber.
  Portions Copyright (c) 1997 by Kent Landfield.
  Portions Copyright (c) 1991, 1992, 1993, 1994, 1995, 1996, 1997
    Free Software Foundation, Inc.  
 
  Use and distribution of this software and its source code are governed 
  by the terms and conditions of the WU-FTPD Software License ("LICENSE").
 
  If you did not receive a copy of the license, it may be obtained online
  at http://www.wu-ftpd.org/license.html.
 
  $Id: getusershell.c,v 1.6 2000/07/01 18:36:29 wuftpd Exp $
 
****************************************************************************/
#ifndef _AIX

#include "../src/config.h"
#include <sys/types.h>
#include <sys/param.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>


#define SHELLS "/etc/shells"

/*
 * Do not add local shells here.  They should be added in /etc/shells
 */
static char *okshells[] =
{"/bin/sh", "/bin/csh", 0};

static char **shells, *strings;
static char **curshell = NULL;
static char **initshells();

/*
 * Get a list of shells from SHELLS, if it exists.
 */
char *getusershell(void)
{
    char *ret;

    if (curshell == NULL)
	curshell = initshells();
    ret = *curshell;
    if (ret != NULL)
	curshell++;
    return (ret);
}

void endusershell(void)
{
    if (shells != NULL)
	free((char *) shells);
    shells = NULL;
    if (strings != NULL)
	free(strings);
    strings = NULL;
    curshell = NULL;
}

void setusershell(void)
{
    curshell = initshells();
}

static char **initshells(void)
{
    register char **sp, *cp;
    register FILE *fp;
    struct stat statb;

    if (shells != NULL)
	free((char *) shells);
    shells = NULL;
    if (strings != NULL)
	free(strings);
    strings = NULL;
    if ((fp = fopen(SHELLS, "r")) == (FILE *) 0)
	return (okshells);
    if (fstat(fileno(fp), &statb) == -1) {
	(void) fclose(fp);
	return (okshells);
    }
    if ((strings = (char *) malloc((unsigned) statb.st_size + 1)) == NULL) {
	(void) fclose(fp);
	return (okshells);
    }
    shells = (char **) calloc((unsigned) statb.st_size / 3, sizeof(char *));

    if (shells == NULL) {
	(void) fclose(fp);
	free(strings);
	strings = NULL;
	return (okshells);
    }
    sp = shells;
    cp = strings;
    while (fgets(cp, MAXPATHLEN + 1, fp) != NULL) {
	while (*cp != '#' && *cp != '/' && *cp != '\0')
	    cp++;
	if (*cp == '#' || *cp == '\0')
	    continue;
	*sp++ = cp;
	while (!isspace(*cp) && *cp != '#' && *cp != '\0')
	    cp++;
	*cp++ = '\0';
    }
    *sp = (char *) 0;
    (void) fclose(fp);
    return (shells);
}

#else /* it is AIX */


/* emulate getusershell for AIX */

#include <userconf.h>
#include <userpw.h>
#include <sys/audit.h>
#include <usersec.h>

static int GETUSERSHELL_opened = 0;
static char **GETUSERSHELL_shells;
static int GETUSERSHELL_current;


char *getusershell()
{
    static char *val;
    static char *list;
    static char *retVal;
    int n;

    if (!GETUSERSHELL_opened) {
	if (getconfattr(SC_SYS_LOGIN, SC_SHELLS, (void *) &val, SEC_LIST)) {
	    return (NULL);
	}
	GETUSERSHELL_opened = 1;
	GETUSERSHELL_current = 0;
	list = val;
    }

    if ((list != NULL) && (*list != NULL)) {
	while (list && *list)
	    list++;

	*list = '\0';

	retVal = val;
	list++;
	val = list;
    }
    else
	retVal = NULL;

    return (retVal);

}

void setusershell()
{
    GETUSERSHELL_opened = 0;
}

void endusershell()
{
    GETUSERSHELL_opened = 0;
}







#endif