File: conf.c

package info (click to toggle)
inetutils 2%3A2.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,684 kB
  • sloc: ansic: 132,371; sh: 12,498; yacc: 1,651; makefile: 725; perl: 72
file content (149 lines) | stat: -rw-r--r-- 3,353 bytes parent folder | download | duplicates (2)
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
/*
  Copyright (C) 2000-2025 Free Software Foundation, Inc.

  This file is part of GNU Inetutils.

  GNU Inetutils is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or (at
  your option) any later version.

  GNU Inetutils is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see `http://www.gnu.org/licenses/'. */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <pwd.h>
#include <grp.h>
#include <mgetgroups.h>
#include "extern.h"

#ifndef LINE_MAX
# define LINE_MAX 2048
#endif

int
display_file (const char *name, int code)
{
  char *cp, line[LINE_MAX];
  FILE *fp = fopen (name, "r");
  if (fp != NULL)
    {
      while (fgets (line, sizeof (line), fp) != NULL)
	{
	  cp = strchr (line, '\n');
	  if (cp != NULL)
	    *cp = '\0';
	  lreply (code, "%s", line);
	}
      fflush (stdout);
      fclose (fp);
      return 0;
    }
  return errno;
}

/*
 * Check if a user is in the file `filename',
 * typically PATH_FTPUSERS or PATH_FTPCHROOT.
 * Return 1 if yes, 0 otherwise.
 */
int
checkuser (const char *filename, const char *name)
{
  FILE *fp;
  int found = 0, ngroups = 0;
  char *p, line[BUFSIZ];
  gid_t *groups = NULL;
  struct passwd *pwd = NULL;

  fp = fopen (filename, "r");
  if (fp != NULL)
    {
      while (fgets (line, sizeof (line), fp) != NULL)
	{
	  /* Properly terminate input.  */
	  p = strchr (line, '\n');
	  if (p != NULL)
	    *p = '\0';

	  /* Disregard initial blank characters.  */
	  p = line;
	  while (isblank (*p))
	    p++;

	  /* Skip comments, and empty lines.  */
	  if (*p == '#' || *p == 0)
	    continue;

	  /* Wildcard entry, a single '@'.  */
	  if (p[0] == '@' && (p[1] == 0 || isblank (p[1])))
	    {
	      found = 1;
	      break;
	    }

	  /* Group entries begin with '@' and are non-trivial.  */
	  if (p[0] == '@' && p[1] && !isblank (p[1]))
	    {
	      /* The group list is generated only if needed,
	       * and only once.
	       */
	      if (!groups)
		{
		  pwd = getpwnam (name);
		  if (pwd)
		    ngroups = mgetgroups (name, pwd->pw_gid, &groups);
		}

	      /* Check for group membership.  */
	      if ((ngroups > 0) && groups && pwd)
		{
		  struct group *grp;
		  char *gname;

		  /* Identify valid group name.  */
		  gname = ++p;
		  while (*p && (isalnum (*p) || *p == '_' || *p == '-'))
		    p++;

		  *p = '\0';	/* Group name ends here.  */

		  grp = getgrnam (gname);
		  if (grp)
		    {
		      int j;

		      for (j = 0; j < ngroups; j++)
			if (groups[j] == grp->gr_gid)
			  {
			    found = 1;
			    break;
			  }
		    }
		}
	      continue;		/* No match, or failure.  */
	    }

	  /* User name ends at the first blank character.  */
	  if (strncmp (p, name, strlen (name)) == 0
	      && (p[strlen (name)] == 0 || isblank (p[strlen (name)])))
	    {
	      found = 1;
	      break;
	    }
	}
      free (groups);
      fclose (fp);
    }
  return (found);
}