3 can_user_push_http.c -- check project membership in group file
4 Copyright (c) 2014 Kyle J. McKay. All rights reserved.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 USAGE: can_user_push_http groupname username
24 Exit code 0 for yes, non-zero for no
25 If username is an explicit member of groupname returns yes
26 Wrong number of or bad arguments exits with non-zero
27 Directly reads compiled in GROUP_FILE location
31 #error GROUP_FILE must be defined to a string which is the full pathname to\
32 the /etc/group format file to read
39 #define BADCHARS ":,# \t\r\n"
40 #define MAX_LINE_SIZE 16384 /* includes trailing \n and \0 */
41 #define MAX_NAME_LEN 64
43 static char linebuffer
[MAX_LINE_SIZE
];
44 static char membersearch
[1+MAX_NAME_LEN
+1+1];
46 int is_valid_name(const char *name
)
53 if (len
> MAX_NAME_LEN
|| strcspn(name
, BADCHARS
) != len
)
58 int main(int argc
, char *argv
[])
65 if (!is_valid_name(argv
[1]) || !is_valid_name(argv
[2]))
68 len
= strlen(argv
[2]);
69 membersearch
[0] = ',';
70 strncpy(membersearch
+1, argv
[2], len
);
71 membersearch
[len
+1] = ',';
72 membersearch
[len
+2] = '\0';
74 groupfile
= fopen(GROUP_FILE
, "r");
77 while (fgets(linebuffer
, sizeof(linebuffer
)-1, groupfile
)) {
78 char *trimmed
, *members
;
79 size_t len
= strlen(linebuffer
);
81 if (len
>= sizeof(linebuffer
) - 2)
83 if (len
&& linebuffer
[len
-1] == '\n')
84 linebuffer
[--len
] = '\0';
85 trimmed
= linebuffer
+ strspn(linebuffer
, " \t");
86 if (!*trimmed
|| *trimmed
== '#')
88 if (trimmed
!= linebuffer
)
90 trimmed
= strchr(linebuffer
, ':');
94 if (strcmp(linebuffer
, argv
[1]))
95 continue; /* not the group we're looking for */
96 trimmed
= strchr(trimmed
+ 1, ':');
99 trimmed
= strchr(trimmed
+ 1, ':');
104 trimmed
= strchr(trimmed
+ 1, ':');
106 trimmed
= linebuffer
+ len
;
109 return strstr(members
, membersearch
) ? EXIT_SUCCESS
: EXIT_FAILURE
;