Convert save_serial() to work like save_index(), and add a
[openssl.git] / apps / apps.c
index 475e47e5701e91adefd6bf65d95338a361f60115..ac9e3daa5e9e3245a24df81165457e535b820be1 100644 (file)
@@ -1519,19 +1519,44 @@ BIGNUM *load_serial(char *serialfile, int create, ASN1_INTEGER **retai)
        return(ret);
        }
 
-int save_serial(char *serialfile, BIGNUM *serial, ASN1_INTEGER **retai)
+int save_serial(char *serialfile, char *suffix, BIGNUM *serial, ASN1_INTEGER **retai)
        {
-       BIO *out;
+       char buf[1][BSIZE];
+       BIO *out = NULL;
        int ret=0;
        ASN1_INTEGER *ai=NULL;
+       int j;
+
+       if (suffix == NULL)
+               j = strlen(serialfile);
+       else
+               j = strlen(serialfile) + strlen(suffix) + 1;
+       if (j >= BSIZE)
+               {
+               BIO_printf(bio_err,"file name too long\n");
+               goto err;
+               }
 
+       if (suffix == NULL)
+               BUF_strlcpy(buf[0], serialfile, BSIZE);
+       else
+               {
+#ifndef OPENSSL_SYS_VMS
+               j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s", serialfile, suffix);
+#else
+               j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s", serialfile, suffix);
+#endif
+               }
+#ifdef RL_DEBUG
+       BIO_printf(bio_err, "DEBUG: writing \"%s\"\n", buf[0]);
+#endif
        out=BIO_new(BIO_s_file());
        if (out == NULL)
                {
                ERR_print_errors(bio_err);
                goto err;
                }
-       if (BIO_write_filename(out,serialfile) <= 0)
+       if (BIO_write_filename(out,buf[0]) <= 0)
                {
                perror(serialfile);
                goto err;
@@ -1556,6 +1581,76 @@ err:
        return(ret);
        }
 
+int rotate_serial(char *serialfile, char *new_suffix, char *old_suffix)
+       {
+       char buf[5][BSIZE];
+       int i,j;
+       struct stat sb;
+
+       i = strlen(serialfile) + strlen(old_suffix);
+       j = strlen(serialfile) + strlen(new_suffix);
+       if (i > j) j = i;
+       if (j + 1 >= BSIZE)
+               {
+               BIO_printf(bio_err,"file name too long\n");
+               goto err;
+               }
+
+#ifndef OPENSSL_SYS_VMS
+       j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s",
+               serialfile, new_suffix);
+#else
+       j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s",
+               serialfile, new_suffix);
+#endif
+#ifndef OPENSSL_SYS_VMS
+       j = BIO_snprintf(buf[1], sizeof buf[1], "%s.%s",
+               serialfile, old_suffix);
+#else
+       j = BIO_snprintf(buf[1], sizeof buf[1], "%s-%s",
+               serialfile, old_suffix);
+#endif
+       if (stat(serialfile,&sb) < 0)
+               {
+               if (errno != ENOENT 
+#ifdef ENOTDIR
+                       && errno != ENOTDIR)
+#endif
+                       goto err;
+               }
+       else
+               {
+#ifdef RL_DEBUG
+               BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
+                       serialfile, buf[1]);
+#endif
+               if (rename(serialfile,buf[1]) < 0)
+                       {
+                       BIO_printf(bio_err,
+                               "unable to rename %s to %s\n",
+                               serialfile, buf[1]);
+                       perror("reason");
+                       goto err;
+                       }
+               }
+#ifdef RL_DEBUG
+       BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
+               buf[0],serialfile);
+#endif
+       if (rename(buf[0],serialfile) < 0)
+               {
+               BIO_printf(bio_err,
+                       "unable to rename %s to %s\n",
+                       buf[0],serialfile);
+               perror("reason");
+               rename(buf[1],serialfile);
+               goto err;
+               }
+       return 1;
+ err:
+       return 0;
+       }
+
 CA_DB *load_index(char *dbfile, DB_ATTR *db_attr)
        {
        CA_DB *retdb = NULL;
@@ -1624,7 +1719,9 @@ CA_DB *load_index(char *dbfile, DB_ATTR *db_attr)
                char *p = NCONF_get_string(dbattr_conf,NULL,"unique_subject");
                if (p)
                        {
+#ifdef RL_DEBUG
                        BIO_printf(bio_err, "DEBUG[load_index]: unique_subject = \"%s\"\n", p);
+#endif
                        switch(*p)
                                {
                        case 'f': /* false */
@@ -1709,7 +1806,9 @@ int save_index(char *dbfile, char *suffix, CA_DB *db)
 #else
        j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s", dbfile, suffix);
 #endif
+#ifdef RL_DEBUG
        BIO_printf(bio_err, "DEBUG: writing \"%s\"\n", buf[0]);
+#endif
        if (BIO_write_filename(out,buf[0]) <= 0)
                {
                perror(dbfile);
@@ -1722,7 +1821,9 @@ int save_index(char *dbfile, char *suffix, CA_DB *db)
        BIO_free(out);
 
        out = BIO_new(BIO_s_file());
+#ifdef RL_DEBUG
        BIO_printf(bio_err, "DEBUG: writing \"%s\"\n", buf[1]);
+#endif
        if (BIO_write_filename(out,buf[1]) <= 0)
                {
                perror(buf[2]);
@@ -1796,8 +1897,10 @@ int rotate_index(char *dbfile, char *new_suffix, char *old_suffix)
                }
        else
                {
+#ifdef RL_DEBUG
                BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
                        dbfile, buf[1]);
+#endif
                if (rename(dbfile,buf[1]) < 0)
                        {
                        BIO_printf(bio_err,
@@ -1807,8 +1910,10 @@ int rotate_index(char *dbfile, char *new_suffix, char *old_suffix)
                        goto err;
                        }
                }
+#ifdef RL_DEBUG
        BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
                buf[0],dbfile);
+#endif
        if (rename(buf[0],dbfile) < 0)
                {
                BIO_printf(bio_err,
@@ -1828,8 +1933,10 @@ int rotate_index(char *dbfile, char *new_suffix, char *old_suffix)
                }
        else
                {
+#ifdef RL_DEBUG
                BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
                        buf[4],buf[3]);
+#endif
                if (rename(buf[4],buf[3]) < 0)
                        {
                        BIO_printf(bio_err,
@@ -1841,8 +1948,10 @@ int rotate_index(char *dbfile, char *new_suffix, char *old_suffix)
                        goto err;
                        }
                }
+#ifdef RL_DEBUG
        BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
                buf[2],buf[4]);
+#endif
        if (rename(buf[2],buf[4]) < 0)
                {
                BIO_printf(bio_err,
@@ -1861,6 +1970,9 @@ int rotate_index(char *dbfile, char *new_suffix, char *old_suffix)
 
 void free_index(CA_DB *db)
        {
-       TXT_DB_free(db->db);
-       OPENSSL_free(db);
+       if (db)
+               {
+               if (db->db) TXT_DB_free(db->db);
+               OPENSSL_free(db);
+               }
        }