annotate lib/password.c @ 1107:bbed38cf7236 draft

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