3 get_user_uuid.c -- get user UUID from passwd 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: get_user_uuid username
24 Exit code 0 for yes, non-zero for no
25 If username has a UUID returns yes and echos it to stdout
26 Wrong number of or bad arguments exits with non-zero
27 Directly reads compiled in PASSWD_FILE location
31 #error PASSWD_FILE must be defined to a string which is the full pathname to\
32 the /etc/passwd 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
];
45 int is_valid_name(const char *name
)
52 if (len
> MAX_NAME_LEN
|| strcspn(name
, BADCHARS
) != len
)
57 int main(int argc
, char *argv
[])
64 if (!is_valid_name(argv
[1]))
67 len
= strlen(argv
[2]);
69 passwdfile
= fopen(PASSWD_FILE
, "r");
72 while (fgets(linebuffer
, sizeof(linebuffer
)-1, passwdfile
)) {
74 size_t len
= strlen(linebuffer
);
76 if (len
>= sizeof(linebuffer
) - 2)
78 if (len
&& linebuffer
[len
-1] == '\n')
79 linebuffer
[--len
] = '\0';
80 trimmed
= linebuffer
+ strspn(linebuffer
, " \t");
81 if (!*trimmed
|| *trimmed
== '#')
83 if (trimmed
!= linebuffer
)
85 trimmed
= strchr(linebuffer
, ':');
89 if (strcmp(linebuffer
, argv
[1]))
90 continue; /* not the user we're looking for */
91 trimmed
= strchr(trimmed
+ 1, ':');
94 trimmed
= strchr(trimmed
+ 1, ':');
97 trimmed
= strchr(trimmed
+ 1, ':');
101 trimmed
= strchr(trimmed
+ 1, ':');
104 uuid
= strchr(uuid
+ 1, ',');
107 trimmed
= strchr(++uuid
, ',');
111 printf("%s\n", uuid
);
112 return *uuid
? EXIT_SUCCESS
: EXIT_FAILURE
;