fa6bc11997-10-15Henrik Grubbström (Grubba) #!/usr/local/bin/pike #!NO_MODULE
0917d32013-03-04Anders Johansson /* $Id$
bb55f71998-03-05Henrik Grubbström (Grubba)  * * name = "SQL Add user"; * doc = "Add a user to an SQL user-database."; */
141a0a1998-03-02Henrik Grubbström (Grubba) 
fa6bc11997-10-15Henrik Grubbström (Grubba) /*
8643d11998-07-15Henrik Grubbström (Grubba)  * This program is (C) 1997 Francesco Chemolli <kinkie@kame.usr.dsi.unimi.it>
1f5d642009-05-26Martin Stjernholm  * See COPYING in the server directory for license information.
fa6bc11997-10-15Henrik Grubbström (Grubba)  * This program comes on an AS-IS basis, WITHOUT ANY WARRANTY of any kind, * either implicit or esplicit. * By using it you implicitly state that you are aware of the risks, and * that take upon yourself all the responsabilities for any damage, * direct or indirect including loss of profict from the use of this software. * Don tell me I hadn't warned you.. */ #include <sql.h> string readline_until_got (string query) { string retval; while (!retval || !sizeof(retval)) retval=readline("(mandatory) "+query); return retval; }
850c282001-01-10Per Hedbor int main() { mapping data=([]); Sql.Sql sql=Sql.Sql("localhost","passwd"); mixed tmp,err; string query; data->username=readline_until_got("username: "); data->passwd=crypt(readline_until_got("password: ")); data->uid=readline("(deprecated) user ID: "); data->gid=readline("(deprecated) group ID: "); data->homedir=readline("home directory: "); data->shell=readline("login shell: ");
fa6bc11997-10-15Henrik Grubbström (Grubba) 
850c282001-01-10Per Hedbor  foreach(indices(data),tmp) { if (!sizeof(data[tmp])) data-=([tmp:0]); }
fa6bc11997-10-15Henrik Grubbström (Grubba) 
850c282001-01-10Per Hedbor  if(data->uid) data->uid=(int)data->uid; if(data->gid) data->gid=(int)data->gid;
fa6bc11997-10-15Henrik Grubbström (Grubba) 
850c282001-01-10Per Hedbor  query="insert into passwd (" + (indices(data)*",") +
fa6bc11997-10-15Henrik Grubbström (Grubba)  ") values (";
850c282001-01-10Per Hedbor  foreach (values(data),tmp) { if (stringp(tmp)) query += sprintf ("'%s',",sql->quote(tmp)); else query += tmp+","; }
fa6bc11997-10-15Henrik Grubbström (Grubba) 
850c282001-01-10Per Hedbor  query=query[..sizeof(query)-2]; query += ")";
fa6bc11997-10-15Henrik Grubbström (Grubba) 
850c282001-01-10Per Hedbor  tmp=sql->query("select * from passwd where username = '"+data->username+"'"); if (sizeof(tmp)) sql->query("delete from passwd where username = '"+data->username+"'");
fa6bc11997-10-15Henrik Grubbström (Grubba) 
850c282001-01-10Per Hedbor  err= catch { sql->query(query); }; if (err) { write("SQL query error: "+sql->error()+"\n"); write("query was: "+query+"\n"); return 1; }
fa6bc11997-10-15Henrik Grubbström (Grubba) }