Mercurial > hg > toybox
annotate lib/password.c @ 1551:1264029d7989 draft
Remove unnecessary assignment spotted by Cynt Rynt.
author | Rob Landley <rob@landley.net> |
---|---|
date | Tue, 11 Nov 2014 15:08:25 -0600 |
parents | cf77c4939385 |
children | 10922d83392a |
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" | |
7 #include <time.h> | |
8 | |
1363 | 9 // generate appropriate random salt string for given encryption algorithm. |
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 { |
1363 | 12 struct { |
13 char *type, id, len; | |
14 } al[] = {{"des", 0, 2}, {"md5", 1, 8}, {"sha256", 5, 16}, {"sha512", 6, 16}}; | |
15 int i; | |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
16 |
1363 | 17 for (i = 0; i < ARRAY_LEN(al); i++) { |
18 if (!strcmp(algo, al[i].type)) { | |
19 int len = al[i].len; | |
20 char *s = salt; | |
21 | |
1500
cf77c4939385
The only illegal characters in a username are ":" (field separator), "\n" (line separator), and "/" (filename separator).
Rob Landley <rob@landley.net>
parents:
1366
diff
changeset
|
22 if (al[i].id) s += sprintf(s, "$%c$", '0'+al[i].id); |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
23 |
1363 | 24 // Read appropriate number of random bytes for salt |
25 i = xopen("/dev/urandom", O_RDONLY); | |
26 xreadall(i, libbuf, ((len*6)+7)/8); | |
27 close(i); | |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
28 |
1363 | 29 // Grab 6 bit chunks and convert to characters in ./0-9a-zA-Z |
30 for (i=0; i<len; i++) { | |
31 int bitpos = i*6, bits = bitpos/8; | |
1090
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
32 |
1363 | 33 bits = ((libbuf[i]+(libbuf[i+1]<<8)) >> (bitpos&7)) & 0x3f; |
34 bits += 46; | |
35 if (bits > 57) bits += 7; | |
36 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
|
37 |
1363 | 38 s[i] = bits; |
39 } | |
40 salt[len] = 0; | |
1090
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
41 |
1363 | 42 return s-salt; |
43 } | |
1090
7c62d5db4484
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents:
1089
diff
changeset
|
44 } |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
45 |
1363 | 46 return -1; |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
47 } |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
48 |
1366
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
49 // Reset terminal to known state, returning old state if old != NULL. |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
50 int set_terminal(int fd, int raw, struct termios *old) |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
51 { |
1366
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
52 struct termios termio; |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
53 |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
54 if (!tcgetattr(fd, &termio) && old) *old = termio; |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
55 |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
56 // the following are the bits set for an xterm. Linux text mode TTYs by |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
57 // default add two additional bits that only matter for serial processing |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
58 // (turn serial line break into an interrupt, and XON/XOFF flow control) |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
59 |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
60 // Any key unblocks output, swap CR and NL on input |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
61 termio.c_iflag = IXANY|ICRNL|INLCR; |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
62 if (toys.which->flags & TOYFLAG_LOCALE) termio.c_iflag |= IUTF8; |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
63 |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
64 // Output appends CR to NL, does magic undocumented postprocessing |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
65 termio.c_oflag = ONLCR|OPOST; |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
66 |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
67 // Leave serial port speed alone |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
68 // termio.c_cflag = C_READ|CS8|EXTB; |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
69 |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
70 // Generate signals, input entire line at once, echo output |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
71 // erase, line kill, escape control characters with ^ |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
72 // erase line char at a time |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
73 // "extended" behavior: ctrl-V quotes next char, ctrl-R reprints unread chars, |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
74 // ctrl-W erases word |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
75 termio.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN; |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
76 |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
77 if (raw) cfmakeraw(&termio); |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
78 |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
79 return tcsetattr(fd, TCSANOW, &termio); |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
80 } |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
81 |
1366
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
82 // Prompt with mesg, read password into buf, return 0 for success 1 for fail |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
83 int read_password(char *buf, int buflen, char *mesg) |
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
|
84 { |
1366
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
85 struct termios oldtermio; |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
86 struct sigaction sa, oldsa; |
1366
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
87 int i, ret = 1; |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
88 |
1366
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
89 // NOP signal handler to return from the read |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
90 memset(&sa, 0, sizeof(sa)); |
1366
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
91 sa.sa_handler = generic_signal; |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
92 sigaction(SIGINT, &sa, &oldsa); |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
93 |
1366
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
94 tcflush(0, TCIFLUSH); |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
95 set_terminal(0, 1, &oldtermio); |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
96 |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
97 xprintf("%s", mesg); |
626 | 98 |
1366
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
99 for (i=0; i < buflen-1; i++) { |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
100 if ((ret = read(0, buf+i, 1)) < 0 || (!ret && !i)) { |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
101 i = 0; |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
102 ret = 1; |
626 | 103 |
1366
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
104 break; |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
105 } else if (!ret || buf[i] == '\n' || buf[i] == '\r') { |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
106 ret = 0; |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
107 |
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
|
108 break; |
1366
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
109 } else if (buf[i] == 8 || buf[i] == 127) i -= i ? 2 : 1; |
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
|
110 } |
1366
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
111 |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
112 // Restore terminal/signal state, terminate string |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
113 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
|
114 tcsetattr(0, TCSANOW, &oldtermio); |
1366
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
115 buf[i] = 0; |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
116 xputc('\n'); |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
117 |
4cf313dbd885
Cleanup read_password(), factor out set_terminal(), fix salt bug (des wants the salt raw, no $ at the beginning).
Rob Landley <rob@landley.net>
parents:
1363
diff
changeset
|
118 return ret; |
626 | 119 } |
120 | |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
121 static char *get_nextcolon(char *line, int cnt) |
626 | 122 { |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
123 while (cnt--) { |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
124 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
|
125 line++; //jump past the colon |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
126 } |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
127 return line; |
626 | 128 } |
129 | |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
130 /*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
|
131 * /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
|
132 * which are used as user, group databeses |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
133 * entry can be |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
134 * 1. encrypted password, when updating user password. |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
135 * 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
|
136 * 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
|
137 * 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
|
138 * 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
|
139 */ |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
140 int update_password(char *filename, char* username, char* entry) |
626 | 141 { |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
142 char *filenamesfx = NULL, *namesfx = NULL, *shadow = NULL, |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
143 *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
|
144 FILE *exfp, *newfp; |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
145 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
|
146 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
|
147 |
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 shadow = strstr(filename, "shadow"); |
1183
0752b2d58909
Rename xmsprintf() to just xmprintf().
Rob Landley <rob@landley.net>
parents:
1159
diff
changeset
|
149 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
|
150 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
|
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 exfp = fopen(filename, "r+"); |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
153 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
|
154 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
|
155 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
|
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 |
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
|
158 *sfx = '-'; |
1551
1264029d7989
Remove unnecessary assignment spotted by Cynt Rynt.
Rob Landley <rob@landley.net>
parents:
1500
diff
changeset
|
159 unlink(filenamesfx); |
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
|
160 ret = link(filename, filenamesfx); |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
161 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
|
162 |
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 *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
|
164 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
|
165 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
|
166 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
|
167 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
|
168 |
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
|
169 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
|
170 if (ret < 0) perror_msg("Couldn't lock file %s",filename); |
626 | 171 |
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 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
|
173 |
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
|
174 newfp = fopen(filenamesfx, "w+"); |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
175 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
|
176 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
|
177 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
|
178 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
|
179 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
|
180 } |
626 | 181 |
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
|
182 ret = 0; |
1183
0752b2d58909
Rename xmsprintf() to just xmprintf().
Rob Landley <rob@landley.net>
parents:
1159
diff
changeset
|
183 namesfx = xmprintf("%s:",username); |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
184 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
|
185 { |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
186 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
|
187 fprintf(newfp, "%s\n", line); |
1159
f1c924e08e63
Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1107
diff
changeset
|
188 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
|
189 char *current_ptr = NULL; |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
190 |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
191 found = 1; |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
192 if (!strcmp(toys.which->name, "passwd")) { |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
193 fprintf(newfp, "%s%s:",namesfx, entry); |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
194 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
|
195 if (shadow) { |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
196 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
|
197 current_ptr = get_nextcolon(current_ptr, 1); |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
198 fprintf(newfp, "%s\n",current_ptr); |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
199 } else fprintf(newfp, "%s\n",current_ptr); |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
200 } 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
|
201 !strcmp(toys.which->name, "addgroup") || |
f1c924e08e63
Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1107
diff
changeset
|
202 !strcmp(toys.which->name, "delgroup") || |
f1c924e08e63
Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1107
diff
changeset
|
203 !strcmp(toys.which->name, "groupdel")){ |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
204 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
|
205 *current_ptr = '\0'; |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
206 fprintf(newfp, "%s", line); |
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
207 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
|
208 } |
626 | 209 } |
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
|
210 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
|
211 } |
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(namesfx); |
1159
f1c924e08e63
Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1107
diff
changeset
|
213 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
|
214 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
|
215 fclose(exfp); |
626 | 216 |
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
|
217 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
|
218 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
|
219 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
|
220 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
|
221 rename(filenamesfx, filename); |
1089
77235d224b1d
Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
222 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
|
223 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
|
224 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
|
225 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
|
226 } |
626 | 227 |
228 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
|
229 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
|
230 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
|
231 } |