Mercurial > hg > toybox
annotate lib/password.c @ 1211:40e0f7b09b77 draft
Fix two bugs reported by Ashwini Sharma.
author | Rob Landley <rob@landley.net> |
---|---|
date | Fri, 28 Feb 2014 23:04:57 -0600 |
parents | 0752b2d58909 |
children | 63db77909fc8 |
rev | line source |
---|---|
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
1 /* password.c - password read/update helper functions. |
626 | 2 * |
3 * Copyright 2012 Ashwini Kumar <ak.ashwini@gmail.com> | |
4 */ | |
5 | |
6 #include "toys.h" | |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
7 #include "xregcomp.h" |
626 | 8 #include <time.h> |
9 | |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
10 int get_salt(char *salt, char *algo) |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
11 { |
1090
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
12 int i, len = 0, offset = 0; |
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
13 char buf[12]; |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
14 |
1090
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
15 if (!strcmp(algo,"des")) len = 2; |
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
16 else { |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
17 *salt++ = '$'; |
1090
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
18 if (!strcmp(algo,"md5")) { |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
19 *salt++ = '1'; |
1090
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
20 len = 8; |
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
21 } else if (!strcmp(algo,"sha256")) { |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
22 *salt++ = '5'; |
1090
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
23 len = 16; |
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
24 } else if (!strcmp(algo,"sha512")) { |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
25 *salt++ = '6'; |
1090
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
26 len = 16; |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
27 } else return -1; |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
28 |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
29 *salt++ = '$'; |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
30 offset = 3; |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
31 } |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
32 |
1090
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
33 // Read appropriate number of random bytes for salt |
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
34 i = xopen("/dev/urandom", O_RDONLY); |
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
35 xreadall(i, buf, ((len*6)+7)/8); |
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
36 close(i); |
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
37 |
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
38 // Grab 6 bit chunks and convert to characters in ./0-9a-zA-Z |
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
39 for (i=0; i<len; i++) { |
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
40 int bitpos = i*6, bits = bitpos/8; |
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
41 |
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
42 bits = ((buf[i]+(buf[i+1]<<8)) >> (bitpos&7)) & 0x3f; |
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
43 bits += 46; |
1107
bbed38cf7236
Fix off by one, pointed out by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
1090
diff
changeset
|
44 if (bits > 57) bits += 7; |
bbed38cf7236
Fix off by one, pointed out by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
1090
diff
changeset
|
45 if (bits > 90) bits += 6; |
1090
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
46 |
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
47 salt[i] = bits; |
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
48 } |
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
49 salt[i] = 0; |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
50 |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
51 return offset; |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
52 } |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
53 |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
54 static void handle(int signo) |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
55 { |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
56 //Dummy.. so that read breaks on the signal, |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
57 //instead of the applocation exit |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
58 } |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
59 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
60 int read_password(char * buff, int buflen, char* mesg) |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
61 { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
62 int i = 0; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
63 struct termios termio, oldtermio; |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
64 struct sigaction sa, oldsa; |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
65 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
66 tcgetattr(0, &oldtermio); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
67 tcflush(0, TCIFLUSH); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
68 termio = oldtermio; |
626 | 69 |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
70 memset(&sa, 0, sizeof(sa)); |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
71 sa.sa_handler = handle; |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
72 sigaction(SIGINT, &sa, &oldsa); |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
73 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
74 termio.c_iflag &= ~(IUCLC|IXON|IXOFF|IXANY); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
75 termio.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|TOSTOP); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
76 tcsetattr(0, TCSANOW, &termio); |
626 | 77 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
78 fputs(mesg, stdout); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
79 fflush(stdout); |
626 | 80 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
81 while (1) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
82 int ret = read(0, &buff[i], 1); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
83 if ( ret < 0 ) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
84 buff[0] = 0; |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
85 sigaction(SIGINT, &oldsa, NULL); |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
86 tcsetattr(0, TCSANOW, &oldtermio); |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
87 xputc('\n'); |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
88 fflush(stdout); |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
89 return 1; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
90 } else if (ret == 0 || buff[i] == '\n' || buff[i] == '\r' || buflen == i+1) |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
91 { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
92 buff[i] = '\0'; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
93 break; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
94 } |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
95 i++; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
96 } |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
97 sigaction(SIGINT, &oldsa, NULL); |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
98 tcsetattr(0, TCSANOW, &oldtermio); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
99 puts(""); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
100 fflush(stdout); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
101 return 0; |
626 | 102 } |
103 | |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
104 static char *get_nextcolon(char *line, int cnt) |
626 | 105 { |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
106 while (cnt--) { |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
107 if (!(line = strchr(line, ':'))) error_exit("Invalid Entry\n"); |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
108 line++; //jump past the colon |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
109 } |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
110 return line; |
626 | 111 } |
112 | |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
113 /*update_password is used by multiple utilities to update /etc/passwd, |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
114 * /etc/shadow, /etc/group and /etc/gshadow files, |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
115 * which are used as user, group databeses |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
116 * entry can be |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
117 * 1. encrypted password, when updating user password. |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
118 * 2. complete entry for user details, when creating new user |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
119 * 3. group members comma',' separated list, when adding user to group |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
120 * 4. complete entry for group details, when creating new group |
1159
f1c924e08e63
Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1107
diff
changeset
|
121 * 5. entry = NULL, delete the named entry user/group |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
122 */ |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
123 int update_password(char *filename, char* username, char* entry) |
626 | 124 { |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
125 char *filenamesfx = NULL, *namesfx = NULL, *shadow = NULL, |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
126 *sfx = NULL, *line = NULL; |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
127 FILE *exfp, *newfp; |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
128 int ret = -1, found = 0; |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
129 struct flock lock; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
130 |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
131 shadow = strstr(filename, "shadow"); |
1183
0752b2d58909
Rename xmsprintf() to just xmprintf().
Rob Landley <rob@landley.net>
parents:
1159
diff
changeset
|
132 filenamesfx = xmprintf("%s+", filename); |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
133 sfx = strchr(filenamesfx, '+'); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
134 |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
135 exfp = fopen(filename, "r+"); |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
136 if (!exfp) { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
137 perror_msg("Couldn't open file %s",filename); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
138 goto free_storage; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
139 } |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
140 |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
141 *sfx = '-'; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
142 ret = unlink(filenamesfx); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
143 ret = link(filename, filenamesfx); |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
144 if (ret < 0) error_msg("can't create backup file"); |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
145 |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
146 *sfx = '+'; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
147 lock.l_type = F_WRLCK; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
148 lock.l_whence = SEEK_SET; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
149 lock.l_start = 0; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
150 lock.l_len = 0; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
151 |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
152 ret = fcntl(fileno(exfp), F_SETLK, &lock); |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
153 if (ret < 0) perror_msg("Couldn't lock file %s",filename); |
626 | 154 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
155 lock.l_type = F_UNLCK; //unlocking at a later stage |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
156 |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
157 newfp = fopen(filenamesfx, "w+"); |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
158 if (!newfp) { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
159 error_msg("couldn't open file for writing"); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
160 ret = -1; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
161 fclose(exfp); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
162 goto free_storage; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
163 } |
626 | 164 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
165 ret = 0; |
1183
0752b2d58909
Rename xmsprintf() to just xmprintf().
Rob Landley <rob@landley.net>
parents:
1159
diff
changeset
|
166 namesfx = xmprintf("%s:",username); |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
167 while ((line = get_line(fileno(exfp))) != NULL) |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
168 { |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
169 if (strncmp(line, namesfx, strlen(namesfx))) |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
170 fprintf(newfp, "%s\n", line); |
1159
f1c924e08e63
Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1107
diff
changeset
|
171 else if (entry) { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
172 char *current_ptr = NULL; |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
173 |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
174 found = 1; |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
175 if (!strcmp(toys.which->name, "passwd")) { |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
176 fprintf(newfp, "%s%s:",namesfx, entry); |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
177 current_ptr = get_nextcolon(line, 2); //past passwd |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
178 if (shadow) { |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
179 fprintf(newfp, "%u:",(unsigned)(time(NULL))/(24*60*60)); |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
180 current_ptr = get_nextcolon(current_ptr, 1); |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
181 fprintf(newfp, "%s\n",current_ptr); |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
182 } else fprintf(newfp, "%s\n",current_ptr); |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
183 } else if (!strcmp(toys.which->name, "groupadd") || |
1159
f1c924e08e63
Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1107
diff
changeset
|
184 !strcmp(toys.which->name, "addgroup") || |
f1c924e08e63
Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1107
diff
changeset
|
185 !strcmp(toys.which->name, "delgroup") || |
f1c924e08e63
Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1107
diff
changeset
|
186 !strcmp(toys.which->name, "groupdel")){ |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
187 current_ptr = get_nextcolon(line, 3); //past gid/admin list |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
188 *current_ptr = '\0'; |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
189 fprintf(newfp, "%s", line); |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
190 fprintf(newfp, "%s\n", entry); |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
191 } |
626 | 192 } |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
193 free(line); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
194 } |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
195 free(namesfx); |
1159
f1c924e08e63
Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1107
diff
changeset
|
196 if (!found && entry) fprintf(newfp, "%s\n", entry); |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
197 fcntl(fileno(exfp), F_SETLK, &lock); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
198 fclose(exfp); |
626 | 199 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
200 errno = 0; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
201 fflush(newfp); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
202 fsync(fileno(newfp)); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
203 fclose(newfp); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
204 rename(filenamesfx, filename); |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
205 if (errno) { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
206 perror_msg("File Writing/Saving failed: "); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
207 unlink(filenamesfx); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
208 ret = -1; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
209 } |
626 | 210 |
211 free_storage: | |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
212 free(filenamesfx); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
213 return ret; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
637
diff
changeset
|
214 } |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
215 |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
216 void is_valid_username(const char *name) |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
217 { |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
218 regex_t rp; |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
219 regmatch_t rm[1]; |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
220 int eval; |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
221 char *regex = "^[_.A-Za-z0-9][-_.A-Za-z0-9]*"; //User name REGEX |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
222 |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
223 xregcomp(&rp, regex, REG_NEWLINE); |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
224 |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
225 /* compare string against pattern -- remember that patterns |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
226 are anchored to the beginning of the line */ |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
227 eval = regexec(&rp, name, 1, rm, 0); |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
228 regfree(&rp); |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
229 if (!eval && !rm[0].rm_so) { |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
230 int len = strlen(name); |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
231 if ((rm[0].rm_eo == len) || |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
232 (rm[0].rm_eo == len - 1 && name[len - 1] == '$')) { |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
233 if (len >= LOGIN_NAME_MAX) error_exit("name is too long"); |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
234 else return; |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
235 } |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
236 } |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
237 error_exit("'%s', not valid %sname",name, |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
238 (((toys.which->name[3] == 'g') || |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
239 (toys.which->name[0] == 'g'))? "group" : "user")); |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
240 } |