annotate lib/password.c @ 1387:a0d26c5e3271 draft

Release notes for 0.4.9.
author Rob Landley <rob@landley.net>
date Mon, 07 Jul 2014 07:32:56 -0500
parents 4cf313dbd885
children cf77c4939385
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * Copyright 2012 Ashwini Kumar <ak.ashwini@gmail.com>
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 */
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
5
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 #include "toys.h"
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
7 #include <time.h>
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
8
1363
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
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
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
12 struct {
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
13 char *type, id, len;
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
14 } al[] = {{"des", 0, 2}, {"md5", 1, 8}, {"sha256", 5, 16}, {"sha512", 6, 16}};
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
15 int i;
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
16
1363
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
17 for (i = 0; i < ARRAY_LEN(al); i++) {
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
18 if (!strcmp(algo, al[i].type)) {
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
19 int len = al[i].len;
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
20 char *s = salt;
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
21
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
22 if (al[i].id) {
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
23 *s++ = '$';
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
24 *s++ = '0'+al[i].id;
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
25 *s++ = '$';
1363
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
26 }
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
27
1363
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
28 // Read appropriate number of random bytes for salt
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
29 i = xopen("/dev/urandom", O_RDONLY);
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
30 xreadall(i, libbuf, ((len*6)+7)/8);
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
31 close(i);
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
32
1363
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
33 // Grab 6 bit chunks and convert to characters in ./0-9a-zA-Z
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
34 for (i=0; i<len; i++) {
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
35 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
36
1363
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
37 bits = ((libbuf[i]+(libbuf[i+1]<<8)) >> (bitpos&7)) & 0x3f;
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
38 bits += 46;
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
39 if (bits > 57) bits += 7;
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
40 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
41
1363
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
42 s[i] = bits;
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
43 }
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
44 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
45
1363
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
46 return s-salt;
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
47 }
1090
7c62d5db4484 Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents: 1089
diff changeset
48 }
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
49
1363
e65f9a9ba62d Cleanup pass on mkpasswd.c
Rob Landley <rob@landley.net>
parents: 1308
diff changeset
50 return -1;
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
51 }
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
52
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
53 // 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
54 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
55 {
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
56 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
57
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 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
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 // 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
61 // 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
62 // (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
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 // 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
65 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
66 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
67
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 // 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
69 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
70
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 // 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
72 // 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
73
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 // 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
75 // 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
76 // 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
77 // "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
78 // 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
79 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
80
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
81 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
82
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 return tcsetattr(fd, TCSANOW, &termio);
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
84 }
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
85
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
86 // 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
87 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
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 struct termios oldtermio;
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
90 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
91 int i, ret = 1;
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
92
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
93 // 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
94 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
95 sa.sa_handler = generic_signal;
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
96 sigaction(SIGINT, &sa, &oldsa);
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
97
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
98 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
99 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
100
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 xprintf("%s", mesg);
626
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
102
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
103 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
104 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
105 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
106 ret = 1;
626
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
107
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
108 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
109 } 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
110 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
111
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
112 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
113 } 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
114 }
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
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 // Restore terminal/signal state, terminate string
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
117 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
118 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
119 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
120 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
121
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
122 return ret;
626
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
123 }
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
124
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
125 static char *get_nextcolon(char *line, int cnt)
626
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
126 {
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
127 while (cnt--) {
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
128 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
129 line++; //jump past the colon
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
130 }
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
131 return line;
626
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
132 }
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
133
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
134 /*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
135 * /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
136 * which are used as user, group databeses
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
137 * entry can be
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
138 * 1. encrypted password, when updating user password.
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
139 * 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
140 * 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
141 * 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
142 * 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
143 */
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
144 int update_password(char *filename, char* username, char* entry)
626
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
145 {
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
146 char *filenamesfx = NULL, *namesfx = NULL, *shadow = NULL,
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
147 *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
148 FILE *exfp, *newfp;
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
149 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
150 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
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 shadow = strstr(filename, "shadow");
1183
0752b2d58909 Rename xmsprintf() to just xmprintf().
Rob Landley <rob@landley.net>
parents: 1159
diff changeset
153 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
154 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
155
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 exfp = fopen(filename, "r+");
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
157 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
158 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
159 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
160 }
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
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 *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
163 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
164 ret = link(filename, filenamesfx);
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
165 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
166
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 *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
168 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
169 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
170 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
171 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
172
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 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
174 if (ret < 0) perror_msg("Couldn't lock file %s",filename);
626
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
175
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 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
177
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 newfp = fopen(filenamesfx, "w+");
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
179 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
180 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
181 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
182 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
183 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
184 }
626
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
185
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
186 ret = 0;
1183
0752b2d58909 Rename xmsprintf() to just xmprintf().
Rob Landley <rob@landley.net>
parents: 1159
diff changeset
187 namesfx = xmprintf("%s:",username);
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
188 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
189 {
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
190 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
191 fprintf(newfp, "%s\n", line);
1159
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents: 1107
diff changeset
192 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
193 char *current_ptr = NULL;
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
194
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
195 found = 1;
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
196 if (!strcmp(toys.which->name, "passwd")) {
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
197 fprintf(newfp, "%s%s:",namesfx, entry);
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
198 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
199 if (shadow) {
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
200 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
201 current_ptr = get_nextcolon(current_ptr, 1);
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
202 fprintf(newfp, "%s\n",current_ptr);
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
203 } else fprintf(newfp, "%s\n",current_ptr);
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
204 } 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
205 !strcmp(toys.which->name, "addgroup") ||
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents: 1107
diff changeset
206 !strcmp(toys.which->name, "delgroup") ||
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents: 1107
diff changeset
207 !strcmp(toys.which->name, "groupdel")){
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
208 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
209 *current_ptr = '\0';
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
210 fprintf(newfp, "%s", line);
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
211 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
212 }
626
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
213 }
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 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
215 }
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
216 free(namesfx);
1159
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents: 1107
diff changeset
217 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
218 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
219 fclose(exfp);
626
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
220
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
221 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
222 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
223 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
224 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
225 rename(filenamesfx, filename);
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
226 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
227 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
228 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
229 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
230 }
626
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
231
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
232 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
233 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
234 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
235 }
1089
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 void is_valid_username(const char *name)
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
238 {
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
239 regex_t rp;
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
240 regmatch_t rm[1];
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
241 int eval;
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
242 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
243
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
244 xregcomp(&rp, regex, REG_NEWLINE);
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
245
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
246 /* compare string against pattern -- remember that patterns
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
247 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
248 eval = regexec(&rp, name, 1, rm, 0);
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
249 regfree(&rp);
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
250 if (!eval && !rm[0].rm_so) {
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
251 int len = strlen(name);
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
252 if ((rm[0].rm_eo == len) ||
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
253 (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
254 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
255 else return;
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
256 }
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
257 }
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
258 error_exit("'%s', not valid %sname",name,
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
259 (((toys.which->name[3] == 'g') ||
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
260 (toys.which->name[0] == 'g'))? "group" : "user"));
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
261 }