annotate lib/password.c @ 1324:3191aa9490aa draft

strings - print the strings in the file.
author Ashwini Sharma <ak.ashwini1981@gmail.com>
date Thu, 29 May 2014 08:18:50 -0500
parents 1493a21160a4
children e65f9a9ba62d
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
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
9 int get_salt(char *salt, char *algo)
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
10 {
1090
7c62d5db4484 Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents: 1089
diff changeset
11 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
12 char buf[12];
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
13
1090
7c62d5db4484 Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents: 1089
diff changeset
14 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
15 else {
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
16 *salt++ = '$';
1090
7c62d5db4484 Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents: 1089
diff changeset
17 if (!strcmp(algo,"md5")) {
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
18 *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
19 len = 8;
7c62d5db4484 Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents: 1089
diff changeset
20 } else if (!strcmp(algo,"sha256")) {
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
21 *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
22 len = 16;
7c62d5db4484 Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents: 1089
diff changeset
23 } else if (!strcmp(algo,"sha512")) {
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
24 *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
25 len = 16;
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
26 } else return -1;
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
27
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
28 *salt++ = '$';
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
29 offset = 3;
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
30 }
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
31
1090
7c62d5db4484 Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents: 1089
diff changeset
32 // 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
33 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
34 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
35 close(i);
7c62d5db4484 Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents: 1089
diff changeset
36
7c62d5db4484 Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents: 1089
diff changeset
37 // 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
38 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
39 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
40
7c62d5db4484 Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents: 1089
diff changeset
41 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
42 bits += 46;
1107
bbed38cf7236 Fix off by one, pointed out by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 1090
diff changeset
43 if (bits > 57) bits += 7;
bbed38cf7236 Fix off by one, pointed out by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 1090
diff changeset
44 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
45
7c62d5db4484 Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents: 1089
diff changeset
46 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
47 }
7c62d5db4484 Minor lib/password.c cleanup, described on the list. (Inline two functions.)
Rob Landley <rob@landley.net>
parents: 1089
diff changeset
48 salt[i] = 0;
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
49
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
50 return offset;
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
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
53 static void handle(int signo)
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
54 {
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
55 //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
56 //instead of the applocation exit
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
57 }
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
58
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
59 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
60 {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing 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 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
62 struct termios termio, oldtermio;
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
63 struct sigaction sa, oldsa;
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
64
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
65 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
66 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
67 termio = oldtermio;
626
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
68
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
69 memset(&sa, 0, sizeof(sa));
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
70 sa.sa_handler = handle;
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
71 sigaction(SIGINT, &sa, &oldsa);
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
72
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
73 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
74 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
75 tcsetattr(0, TCSANOW, &termio);
626
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
76
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
77 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
78 fflush(stdout);
626
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
79
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
80 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
81 int ret = read(0, &buff[i], 1);
1308
1493a21160a4 patch from ashwini sharma: treat 0 length read at the start of password read as EOF.
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
82 if ( ret < 0 || (!ret && !i)) {
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
83 buff[0] = 0;
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
84 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
85 tcsetattr(0, TCSANOW, &oldtermio);
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
86 xputc('\n');
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
87 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
88 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
89 } 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
90 {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing 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 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
92 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
93 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing 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 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
95 }
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
96 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
97 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
98 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
99 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
100 return 0;
626
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
101 }
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
102
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
103 static char *get_nextcolon(char *line, int cnt)
626
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
104 {
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
105 while (cnt--) {
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
106 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
107 line++; //jump past the colon
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
108 }
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
109 return line;
626
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
110 }
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
111
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
112 /*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
113 * /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
114 * which are used as user, group databeses
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
115 * entry can be
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
116 * 1. encrypted password, when updating user password.
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
117 * 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
118 * 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
119 * 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
120 * 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
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");
1183
0752b2d58909 Rename xmsprintf() to just xmprintf().
Rob Landley <rob@landley.net>
parents: 1159
diff changeset
131 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
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;
1183
0752b2d58909 Rename xmsprintf() to just xmprintf().
Rob Landley <rob@landley.net>
parents: 1159
diff changeset
165 namesfx = xmprintf("%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);
1159
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents: 1107
diff changeset
170 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
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") ||
1159
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents: 1107
diff changeset
183 !strcmp(toys.which->name, "addgroup") ||
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents: 1107
diff changeset
184 !strcmp(toys.which->name, "delgroup") ||
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents: 1107
diff changeset
185 !strcmp(toys.which->name, "groupdel")){
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
186 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
187 *current_ptr = '\0';
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
188 fprintf(newfp, "%s", line);
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
189 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
190 }
626
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
191 }
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
192 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
193 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing 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 free(namesfx);
1159
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents: 1107
diff changeset
195 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
196 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
197 fclose(exfp);
626
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
198
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
199 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
200 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
201 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
202 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
203 rename(filenamesfx, filename);
1089
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
204 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
205 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
206 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
207 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
208 }
626
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
209
77d94b36aff0 Add passwd by Kyungwan Han.
Rob Landley <rob@landley.net>
parents:
diff changeset
210 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
211 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
212 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
213 }
1089
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 void is_valid_username(const char *name)
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
216 {
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
217 regex_t rp;
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
218 regmatch_t rm[1];
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
219 int eval;
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
220 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
221
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
222 xregcomp(&rp, regex, REG_NEWLINE);
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
223
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
224 /* compare string against pattern -- remember that patterns
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
225 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
226 eval = regexec(&rp, name, 1, rm, 0);
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
227 regfree(&rp);
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
228 if (!eval && !rm[0].rm_so) {
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
229 int len = strlen(name);
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
230 if ((rm[0].rm_eo == len) ||
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
231 (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
232 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
233 else return;
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
234 }
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
235 }
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
236 error_exit("'%s', not valid %sname",name,
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
237 (((toys.which->name[3] == 'g') ||
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
238 (toys.which->name[0] == 'g'))? "group" : "user"));
77235d224b1d Prep work for useradd by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
239 }