55 *
66 * Copyright (c) 1994, Regents of the University of California
77 *
8- *
8+ * $Id: user.c,v 1.21 1998/12/14 06:50:18 scrappy Exp $
99 *
1010 *-------------------------------------------------------------------------
1111 */
3535
3636static void CheckPgUserAclNotNull (void );
3737
38+ #define SQL_LENGTH 512
39+
3840/*---------------------------------------------------------------------
3941 * UpdatePgPwdFile
4042 *
4749UpdatePgPwdFile (char * sql )
4850{
4951
50- char * filename ;
51- char * tempname ;
52+ char * filename ,
53+ * tempname ;
54+ int bufsize ;
5255
5356/*
5457 * Create a temporary filename to be renamed later. This prevents the
5558 * backend from clobbering the pg_pwd file while the postmaster might
5659 * be reading from it.
5760 */
5861filename = crypt_getpwdfilename ();
59- tempname = (char * )malloc (strlen (filename )+ 12 );
60- sprintf (tempname ,"%s.%d" ,filename ,MyProcPid );
62+ bufsize = strlen (filename )+ 12 ;
63+ tempname = (char * )palloc (bufsize );
64+ snprintf (tempname ,bufsize ,"%s.%d" ,filename ,MyProcPid );
6165
6266/*
6367 * Copy the contents of pg_shadow to the pg_pwd ASCII file using a the
6468 * SEPCHAR character as the delimiter between fields. Then rename the
6569 * file to its final name.
6670 */
67- sprintf (sql ,"copy %s to '%s' using delimiters %s" ,ShadowRelationName ,tempname ,CRYPT_PWD_FILE_SEPCHAR );
71+ snprintf (sql ,QRY_LENGTH ,
72+ "copy %s to '%s' using delimiters %s" ,
73+ ShadowRelationName ,tempname ,CRYPT_PWD_FILE_SEPCHAR );
6874pg_exec_query (sql );
6975rename (tempname ,filename );
70- free ((void * )tempname );
76+ pfree ((void * )tempname );
7177
7278/*
7379 * Create a flag file the postmaster will detect the next time it
8995DefineUser (CreateUserStmt * stmt )
9096{
9197
92- char * pg_shadow ;
93- Relation pg_shadow_rel ;
94- TupleDesc pg_shadow_dsc ;
95- HeapScanDesc scan ;
96- HeapTuple tuple ;
97- Datum datum ;
98- char sql [512 ];
99- char * sql_end ;
100- bool exists = false,
101- n ,
102- inblock ;
103- int max_id = -1 ;
98+ char * pg_shadow ,
99+ sql [SQL_LENGTH ];
100+ Relation pg_shadow_rel ;
101+ TupleDesc pg_shadow_dsc ;
102+ HeapScanDesc scan ;
103+ HeapTuple tuple ;
104+ Datum datum ;
105+ bool exists = false,
106+ n ,
107+ inblock ;
108+ int max_id = -1 ;
104109
105110if (stmt -> password )
106111CheckPgUserAclNotNull ();
@@ -152,46 +157,23 @@ DefineUser(CreateUserStmt *stmt)
152157RelationUnsetLockForWrite (pg_shadow_rel );
153158heap_close (pg_shadow_rel );
154159UserAbortTransactionBlock ();
155- elog (ERROR ,"defineUser: user \"%s\" has already been created" ,stmt -> user );
160+ elog (ERROR ,
161+ "defineUser: user \"%s\" has already been created" ,stmt -> user );
156162return ;
157163}
158164
159165/*
160166 * Build the insert statment to be executed.
161167 */
162- sprintf (sql ,"insert into %s(usename,usesysid,usecreatedb,usetrace,usesuper,usecatupd,passwd" ,ShadowRelationName );
163- /*if (stmt->password)
164- strcat(sql, ",passwd"); -- removed so that insert empty string when no password */
165- if (stmt -> validUntil )
166- strcat (sql ,",valuntil" );
167-
168- sql_end = sql + strlen (sql );
169- sprintf (sql_end ,") values('%s',%d" ,stmt -> user ,max_id + 1 );
170- if (stmt -> createdb && * stmt -> createdb )
171- strcat (sql_end ,",'t','t'" );
172- else
173- strcat (sql_end ,",'f','t'" );
174- if (stmt -> createuser && * stmt -> createuser )
175- strcat (sql_end ,",'t','t'" );
176- else
177- strcat (sql_end ,",'f','t'" );
178- sql_end += strlen (sql_end );
179- if (stmt -> password )
180- {
181- sprintf (sql_end ,",'%s'" ,stmt -> password );
182- sql_end += strlen (sql_end );
183- }
184- else
185- {
186- strcpy (sql_end ,",''" );
187- sql_end += strlen (sql_end );
188- }
189- if (stmt -> validUntil )
190- {
191- sprintf (sql_end ,",'%s'" ,stmt -> validUntil );
192- sql_end += strlen (sql_end );
193- }
194- strcat (sql_end ,")" );
168+ snprintf (sql ,SQL_LENGTH ,
169+ "insert into %s(usename,usesysid,usecreatedb,usetrace,usesuper,"
170+ "usecatupd,passwd,valuntil) values('%s',%d%s%s,'%s','%s')" ,
171+ ShadowRelationName ,
172+ stmt -> user ,max_id + 1 ,
173+ (stmt -> createdb && * stmt -> createdb ) ?",'t','t'" :",'f','t'" ,
174+ (stmt -> createuser && * stmt -> createuser ) ?",'t','t'" :",'f','t'" ,
175+ stmt -> password ?stmt -> password :"''" ,
176+ stmt -> validUntil ?stmt -> valudUntil :"" );
195177
196178pg_exec_query (sql );
197179
@@ -217,13 +199,12 @@ extern void
217199AlterUser (AlterUserStmt * stmt )
218200{
219201
220- char * pg_shadow ;
202+ char * pg_shadow ,
203+ sql [SQL_LENGTH ];
221204Relation pg_shadow_rel ;
222205TupleDesc pg_shadow_dsc ;
223206HeapTuple tuple ;
224- char sql [512 ];
225- char * sql_end ;
226- bool inblock ;
207+ bool inblock ;
227208
228209if (stmt -> password )
229210CheckPgUserAclNotNull ();
@@ -271,47 +252,38 @@ AlterUser(AlterUserStmt *stmt)
271252/*
272253 * Create the update statement to modify the user.
273254 */
274- sprintf (sql ,"update %s set" ,ShadowRelationName );
275- sql_end = sql ;
255+ snprintf (sql , SQL_LENGTH ,"update %s set" ,ShadowRelationName );
256+
276257if (stmt -> password )
277258{
278- sql_end += strlen (sql_end );
279- sprintf (sql_end ," passwd = '%s'" ,stmt -> password );
259+ snprintf (sql ,SQL_LENGTH ,"%s passwd = '%s'" ,sql ,stmt -> password );
280260}
261+
281262if (stmt -> createdb )
282263{
283- if (sql_end != sql )
284- strcat (sql_end ,"," );
285- sql_end += strlen (sql_end );
286- if (* stmt -> createdb )
287- strcat (sql_end ," usecreatedb = 't'" );
288- else
289- strcat (sql_end ," usecreatedb = 'f'" );
264+ snprintf (sql ,SQL_LENGTH ,"%s %susecreatedb='%s'" ,
265+ stmt -> password ?"," :"" ,
266+ * stmt -> createdb ?"t" :"f" );
290267}
268+
291269if (stmt -> createuser )
292270{
293- if (sql_end != sql )
294- strcat (sql_end ,"," );
295- sql_end += strlen (sql_end );
296- if (* stmt -> createuser )
297- strcat (sql_end ," usesuper = 't'" );
298- else
299- strcat (sql_end ," usesuper = 'f'" );
271+ snprintf (sql ,SQL_LENGTH ,"%s %susesuper='%s'" ,
272+ (stmt -> password || stmt -> createdb ) ?"," :"" ,
273+ * stmt -> createuser ?"t" :"f" );
300274}
275+
301276if (stmt -> validUntil )
302277{
303- if (sql_end != sql )
304- strcat (sql_end ,"," );
305- sql_end += strlen (sql_end );
306- sprintf (sql_end ," valuntil = '%s'" ,stmt -> validUntil );
307- }
308- if (sql_end != sql )
309- {
310- sql_end += strlen (sql_end );
311- sprintf (sql_end ," where usename = '%s'" ,stmt -> user );
312- pg_exec_query (sql );
278+ snprintf (sql ,SQL_LENGTH ,"%s %svaluntil='%s'" ,
279+ (stmt -> password || stmt -> createdb || stmt -> createuser ) ?"," :"" ,
280+ stmt -> validUntil );
313281}
314282
283+ snprintf (sql ,SQL_LENGTH ,"%s where usename = '%s'" ,sql ,stmt -> user );
284+
285+ pg_exec_query (sql );
286+
315287/* do the pg_group stuff here */
316288
317289UpdatePgPwdFile (sql );
@@ -402,8 +374,9 @@ RemoveUser(char *user)
402374datum = heap_getattr (tuple ,Anum_pg_database_datname ,pg_dsc ,& n );
403375if (memcmp ((void * )datum ,"template1" ,9 ))
404376{
405- dbase = (char * * )realloc ((void * )dbase ,sizeof (char * )* (ndbase + 1 ));
406- dbase [ndbase ]= (char * )malloc (NAMEDATALEN + 1 );
377+ dbase =
378+ (char * * )repalloc ((void * )dbase ,sizeof (char * )* (ndbase + 1 ));
379+ dbase [ndbase ]= (char * )palloc (NAMEDATALEN + 1 );
407380memcpy ((void * )dbase [ndbase ], (void * )datum ,NAMEDATALEN );
408381dbase [ndbase ++ ][NAMEDATALEN ]= '\0' ;
409382}
@@ -415,12 +388,12 @@ RemoveUser(char *user)
415388while (ndbase -- )
416389{
417390elog (NOTICE ,"Dropping database %s" ,dbase [ndbase ]);
418- sprintf (sql ,"drop database %s" ,dbase [ndbase ]);
419- free ((void * )dbase [ndbase ]);
391+ snprintf (sql , SQL_LENGTH ,"drop database %s" ,dbase [ndbase ]);
392+ pfree ((void * )dbase [ndbase ]);
420393pg_exec_query (sql );
421394}
422395if (dbase )
423- free ((void * )dbase );
396+ pfree ((void * )dbase );
424397
425398/*
426399 * Since pg_shadow is global over all databases, one of two things
@@ -443,7 +416,8 @@ RemoveUser(char *user)
443416/*
444417 * Remove the user from the pg_shadow table
445418 */
446- sprintf (sql ,"delete from %s where usename = '%s'" ,ShadowRelationName ,user );
419+ snprintf (sql ,SQL_LENGTH ,
420+ "delete from %s where usename = '%s'" ,ShadowRelationName ,user );
447421pg_exec_query (sql );
448422
449423UpdatePgPwdFile (sql );