| 1 |
/* |
| 2 |
Copyright (C) 2000-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Inetutils. |
| 5 |
|
| 6 |
GNU Inetutils is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Inetutils is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see `http://www.gnu.org/licenses/'. */ |
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
#include <stdio.h> |
| 21 |
#include <stdlib.h> |
| 22 |
#include <errno.h> |
| 23 |
#include <string.h> |
| 24 |
#include <ctype.h> |
| 25 |
#include <pwd.h> |
| 26 |
#include <grp.h> |
| 27 |
#include <mgetgroups.h> |
| 28 |
#include "extern.h" |
| 29 |
|
| 30 |
#ifndef LINE_MAX |
| 31 |
# define LINE_MAX 2048 |
| 32 |
#endif |
| 33 |
|
| 34 |
int |
| 35 |
display_file (const char *name, int code) |
| 36 |
{ |
| 37 |
char *cp, line[LINE_MAX]; |
| 38 |
FILE *fp = fopen (name, "r"); |
| 39 |
if (fp != NULL) |
| 40 |
{ |
| 41 |
while (fgets (line, sizeof (line), fp) != NULL) |
| 42 |
{ |
| 43 |
cp = strchr (line, '\n'); |
| 44 |
if (cp != NULL) |
| 45 |
*cp = '\0'; |
| 46 |
lreply (code, "%s", line); |
| 47 |
} |
| 48 |
fflush (stdout); |
| 49 |
fclose (fp); |
| 50 |
return 0; |
| 51 |
} |
| 52 |
return errno; |
| 53 |
} |
| 54 |
|
| 55 |
/* |
| 56 |
* Check if a user is in the file `filename', |
| 57 |
* typically PATH_FTPUSERS or PATH_FTPCHROOT. |
| 58 |
* Return 1 if yes, 0 otherwise. |
| 59 |
*/ |
| 60 |
int |
| 61 |
checkuser (const char *filename, const char *name) |
| 62 |
{ |
| 63 |
FILE *fp; |
| 64 |
int found = 0, ngroups = 0; |
| 65 |
char *p, line[BUFSIZ]; |
| 66 |
gid_t *groups = NULL; |
| 67 |
struct passwd *pwd = NULL; |
| 68 |
|
| 69 |
fp = fopen (filename, "r"); |
| 70 |
if (fp != NULL) |
| 71 |
{ |
| 72 |
while (fgets (line, sizeof (line), fp) != NULL) |
| 73 |
{ |
| 74 |
/* Properly terminate input. */ |
| 75 |
p = strchr (line, '\n'); |
| 76 |
if (p != NULL) |
| 77 |
*p = '\0'; |
| 78 |
|
| 79 |
/* Disregard initial blank characters. */ |
| 80 |
p = line; |
| 81 |
while (isblank (*p)) |
| 82 |
p++; |
| 83 |
|
| 84 |
/* Skip comments, and empty lines. */ |
| 85 |
if (*p == '#' || *p == 0) |
| 86 |
continue; |
| 87 |
|
| 88 |
/* Wildcard entry, a single '@'. */ |
| 89 |
if (p[0] == '@' && (p[1] == 0 || isblank (p[1]))) |
| 90 |
{ |
| 91 |
found = 1; |
| 92 |
break; |
| 93 |
} |
| 94 |
|
| 95 |
/* Group entries begin with '@' and are non-trivial. */ |
| 96 |
if (p[0] == '@' && p[1] && !isblank (p[1])) |
| 97 |
{ |
| 98 |
/* The group list is generated only if needed, |
| 99 |
* and only once. |
| 100 |
*/ |
| 101 |
if (!groups) |
| 102 |
{ |
| 103 |
pwd = getpwnam (name); |
| 104 |
if (pwd) |
| 105 |
ngroups = mgetgroups (name, pwd->pw_gid, &groups); |
| 106 |
} |
| 107 |
|
| 108 |
/* Check for group membership. */ |
| 109 |
if ((ngroups > 0) && groups && pwd) |
| 110 |
{ |
| 111 |
struct group *grp; |
| 112 |
char *gname; |
| 113 |
|
| 114 |
/* Identify valid group name. */ |
| 115 |
gname = ++p; |
| 116 |
while (*p && (isalnum (*p) || *p == '_' || *p == '-')) |
| 117 |
p++; |
| 118 |
|
| 119 |
*p = '\0'; /* Group name ends here. */ |
| 120 |
|
| 121 |
grp = getgrnam (gname); |
| 122 |
if (grp) |
| 123 |
{ |
| 124 |
int j; |
| 125 |
|
| 126 |
for (j = 0; j < ngroups; j++) |
| 127 |
if (groups[j] == grp->gr_gid) |
| 128 |
{ |
| 129 |
found = 1; |
| 130 |
break; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
continue; /* No match, or failure. */ |
| 135 |
} |
| 136 |
|
| 137 |
/* User name ends at the first blank character. */ |
| 138 |
if (strncmp (p, name, strlen (name)) == 0 |
| 139 |
&& (p[strlen (name)] == 0 || isblank (p[strlen (name)]))) |
| 140 |
{ |
| 141 |
found = 1; |
| 142 |
break; |
| 143 |
} |
| 144 |
} |
| 145 |
free (groups); |
| 146 |
fclose (fp); |
| 147 |
} |
| 148 |
return (found); |
| 149 |
} |