5
5
*
6
6
* Copyright (c) 1994, Regents of the University of California
7
7
*
8
- *
8
+ * $Id: user.c,v 1.21 1998/12/14 06:50:18 scrappy Exp $
9
9
*
10
10
*-------------------------------------------------------------------------
11
11
*/
35
35
36
36
static void CheckPgUserAclNotNull (void );
37
37
38
+ #define SQL_LENGTH 512
39
+
38
40
/*---------------------------------------------------------------------
39
41
* UpdatePgPwdFile
40
42
*
47
49
UpdatePgPwdFile (char * sql )
48
50
{
49
51
50
- char * filename ;
51
- char * tempname ;
52
+ char * filename ,
53
+ * tempname ;
54
+ int bufsize ;
52
55
53
56
/*
54
57
* Create a temporary filename to be renamed later. This prevents the
55
58
* backend from clobbering the pg_pwd file while the postmaster might
56
59
* be reading from it.
57
60
*/
58
61
filename = 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 );
61
65
62
66
/*
63
67
* Copy the contents of pg_shadow to the pg_pwd ASCII file using a the
64
68
* SEPCHAR character as the delimiter between fields. Then rename the
65
69
* file to its final name.
66
70
*/
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 );
68
74
pg_exec_query (sql );
69
75
rename (tempname ,filename );
70
- free ((void * )tempname );
76
+ pfree ((void * )tempname );
71
77
72
78
/*
73
79
* Create a flag file the postmaster will detect the next time it
89
95
DefineUser (CreateUserStmt * stmt )
90
96
{
91
97
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 ;
104
109
105
110
if (stmt -> password )
106
111
CheckPgUserAclNotNull ();
@@ -152,46 +157,23 @@ DefineUser(CreateUserStmt *stmt)
152
157
RelationUnsetLockForWrite (pg_shadow_rel );
153
158
heap_close (pg_shadow_rel );
154
159
UserAbortTransactionBlock ();
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 );
156
162
return ;
157
163
}
158
164
159
165
/*
160
166
* Build the insert statment to be executed.
161
167
*/
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 :"" );
195
177
196
178
pg_exec_query (sql );
197
179
@@ -217,13 +199,12 @@ extern void
217
199
AlterUser (AlterUserStmt * stmt )
218
200
{
219
201
220
- char * pg_shadow ;
202
+ char * pg_shadow ,
203
+ sql [SQL_LENGTH ];
221
204
Relation pg_shadow_rel ;
222
205
TupleDesc pg_shadow_dsc ;
223
206
HeapTuple tuple ;
224
- char sql [512 ];
225
- char * sql_end ;
226
- bool inblock ;
207
+ bool inblock ;
227
208
228
209
if (stmt -> password )
229
210
CheckPgUserAclNotNull ();
@@ -271,47 +252,38 @@ AlterUser(AlterUserStmt *stmt)
271
252
/*
272
253
* Create the update statement to modify the user.
273
254
*/
274
- sprintf (sql ,"update %s set" ,ShadowRelationName );
275
- sql_end = sql ;
255
+ snprintf (sql , SQL_LENGTH ,"update %s set" ,ShadowRelationName );
256
+
276
257
if (stmt -> password )
277
258
{
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 );
280
260
}
261
+
281
262
if (stmt -> createdb )
282
263
{
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" );
290
267
}
268
+
291
269
if (stmt -> createuser )
292
270
{
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" );
300
274
}
275
+
301
276
if (stmt -> validUntil )
302
277
{
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 );
313
281
}
314
282
283
+ snprintf (sql ,SQL_LENGTH ,"%s where usename = '%s'" ,sql ,stmt -> user );
284
+
285
+ pg_exec_query (sql );
286
+
315
287
/* do the pg_group stuff here */
316
288
317
289
UpdatePgPwdFile (sql );
@@ -402,8 +374,9 @@ RemoveUser(char *user)
402
374
datum = heap_getattr (tuple ,Anum_pg_database_datname ,pg_dsc ,& n );
403
375
if (memcmp ((void * )datum ,"template1" ,9 ))
404
376
{
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 );
407
380
memcpy ((void * )dbase [ndbase ], (void * )datum ,NAMEDATALEN );
408
381
dbase [ndbase ++ ][NAMEDATALEN ]= '\0' ;
409
382
}
@@ -415,12 +388,12 @@ RemoveUser(char *user)
415
388
while (ndbase -- )
416
389
{
417
390
elog (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 ]);
420
393
pg_exec_query (sql );
421
394
}
422
395
if (dbase )
423
- free ((void * )dbase );
396
+ pfree ((void * )dbase );
424
397
425
398
/*
426
399
* Since pg_shadow is global over all databases, one of two things
@@ -443,7 +416,8 @@ RemoveUser(char *user)
443
416
/*
444
417
* Remove the user from the pg_shadow table
445
418
*/
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 );
447
421
pg_exec_query (sql );
448
422
449
423
UpdatePgPwdFile (sql );