s3/lib: add parent_smb_fname()
[vlendec/samba-autobuild/.git] / source3 / printing / printing_db.c
index adea10dfa691b5277152a65f1e63ad1f5a5e3677..3fa85579f20b6e0da3943ce3a83f97a07814e1c2 100644 (file)
@@ -7,7 +7,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
+#include "system/passwd.h" /* uid_wrapper */
+#include "system/filesys.h"
 #include "printing.h"
+#include "util_tdb.h"
 
 static struct tdb_print_db *print_db_head;
 
@@ -33,9 +35,11 @@ static struct tdb_print_db *print_db_head;
 struct tdb_print_db *get_print_db_byname(const char *printername)
 {
        struct tdb_print_db *p = NULL, *last_entry = NULL;
-       int num_open = 0;
-       pstring printdb_path;
-       BOOL done_become_root = False;
+       size_t num_open = 0;
+       char *printdb_path = NULL;
+       bool done_become_root = False;
+       char *print_cache_path;
+       int ret;
 
        SMB_ASSERT(printername != NULL);
 
@@ -55,15 +59,17 @@ struct tdb_print_db *get_print_db_byname(const char *printername)
        /* Not found. */
        if (num_open >= MAX_PRINT_DBS_OPEN) {
                /* Try and recycle the last entry. */
-               DLIST_PROMOTE(print_db_head, last_entry);
+               if (print_db_head && last_entry) {
+                       DLIST_PROMOTE(print_db_head, last_entry);
+               }
 
                for (p = print_db_head; p; p = p->next) {
                        if (p->ref_count)
                                continue;
                        if (p->tdb) {
-                               if (tdb_close(print_db_head->tdb)) {
+                               if (tdb_close(p->tdb)) {
                                        DEBUG(0,("get_print_db: Failed to close tdb for printer %s\n",
-                                                               print_db_head->printer_name ));
+                                                               p->printer_name ));
                                        return NULL;
                                }
                        }
@@ -72,12 +78,12 @@ struct tdb_print_db *get_print_db_byname(const char *printername)
                        memset(p->printer_name, '\0', sizeof(p->printer_name));
                        break;
                }
-               if (p) {
+               if (p && print_db_head) {
                        DLIST_PROMOTE(print_db_head, p);
                        p = print_db_head;
                }
        }
-       
+
        if (!p) {
                /* Create one. */
                p = SMB_MALLOC_P(struct tdb_print_db);
@@ -89,11 +95,22 @@ struct tdb_print_db *get_print_db_byname(const char *printername)
                DLIST_ADD(print_db_head, p);
        }
 
-       pstrcpy(printdb_path, lock_path("printing/"));
-       pstrcat(printdb_path, printername);
-       pstrcat(printdb_path, ".tdb");
+       print_cache_path = cache_path(talloc_tos(), "printing/");
+       if (print_cache_path == NULL) {
+               DLIST_REMOVE(print_db_head, p);
+               SAFE_FREE(p);
+               return NULL;
+       }
+       ret = asprintf(&printdb_path, "%s%s.tdb",
+                      print_cache_path, printername);
+       TALLOC_FREE(print_cache_path);
+       if (ret < 0) {
+               DLIST_REMOVE(print_db_head, p);
+               SAFE_FREE(p);
+               return NULL;
+       }
 
-       if (geteuid() != 0) {
+       if (geteuid() != sec_initial_uid()) {
                become_root();
                done_become_root = True;
        }
@@ -108,9 +125,11 @@ struct tdb_print_db *get_print_db_byname(const char *printername)
                DEBUG(0,("get_print_db: Failed to open printer backend database %s.\n",
                                        printdb_path ));
                DLIST_REMOVE(print_db_head, p);
+               SAFE_FREE(printdb_path);
                SAFE_FREE(p);
                return NULL;
        }
+       SAFE_FREE(printdb_path);
        fstrcpy(p->printer_name, printername);
        p->ref_count++;
        return p;
@@ -151,7 +170,7 @@ void close_all_print_db(void)
  messages. data needs freeing on exit.
 ****************************************************************************/
 
-TDB_DATA get_printer_notify_pid_list(TDB_CONTEXT *tdb, const char *printer_name, BOOL cleanlist)
+TDB_DATA get_printer_notify_pid_list(struct tdb_context *tdb, const char *printer_name, bool cleanlist)
 {
        TDB_DATA data;
        size_t i;
@@ -183,7 +202,7 @@ TDB_DATA get_printer_notify_pid_list(TDB_CONTEXT *tdb, const char *printer_name,
        for( i = 0; i < data.dsize; i += 8) {
                pid_t pid = (pid_t)IVAL(data.dptr, i);
 
-               if (pid == sys_getpid())
+               if (pid == getpid())
                        continue;
 
                /* Entry is dead if process doesn't exist or refcount is zero. */