clite.commands.data = function() { clite.commands.load('su',function(args,env,io) { var stdio = io.include('stdio'); var stdlib = io.include('stdlib'); var clite = io.include('clite'); var auth = io.include('auth'); var term = io.include('term'); var pw = null; var preserve = true; var shell = null; function help() { stdio.printf(` su - switch users Usage: su [OPTION] Options: -? Print this help information -l Simulate a full login -m Preserve the environment -s Use the shell at -c Automatically accept cookies when switching users `); } function changeUser() { if (!stdlib.setgid(pw.pw_gid)) { stdio.write(io.stderr,'internal error (1)\n'); io.exit(1); return; } if (!stdlib.setuid(pw.pw_uid)) { stdio.write(io.stderr,'internal error (2)\n'); io.exit(1); return; } var e = null; if (preserve) { e = structuredClone(env); }else{ e = {}; e.USER = pw.pw_name; e.PWD = pw.pw_dir; e.HOME = pw.pw_dir; e.SHELL = pw.pw_shell; } if (shell == null) shell = e.SHELL; var args = [shell]; var r = stdlib.exec(shell,args,e,io); if (r == 0) return; stdio.write(io.stderr,'internal error (3)\n'); io.exit(1); } function doLogin(str) { term.ttyctrl('echo',true); if (!auth.checkpassuid(pw.pw_uid,str)) { stdio.printf('authentication error\n'); io.exit(1); return; } changeUser(); } function preLogin(str) { if (str == 'no') { clite.setcookiestate(false); io.exit(0); return; } if (str != 'yes') { cookies(); return; } clite.setcookiestate(true); if (stdlib.getuid() != 0) { stdio.write(io.stdout,'Password: '); term.ttyctrl('echo',false); if (!stdio.read(io.stdin,doLogin)) { term.ttyctrl('echo',true); stdio.write(io.stderr,'authentication error\n'); io.exit(1); } return; } changeUser(); } function cookies() { clite.cookienotice(); stdio.printf("Enter 'yes' to agree, or 'no' to cancel: "); if (!stdio.read(io.stdin,preLogin)) { stdio.write(io.stderr,'internal error\n'); io.exit(1); } } function main(argc,argv) { var user = 'root'; var shellnext = false; var accept = false; for (var i=1; i [username] Options: -? Print this help information -a Add a new user account -d Delete a user account -s Set the user's default shell to -g Set the user's primary group to `); } function validateName(n) { const char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_'; if (n.length < 2 || n.length > 12) return false; if (char.indexOf(n[0]) < 0) return false; for (var i=0; i